[text_sensor][text] Avoid heap allocation when state unchanged (#13044)

This commit is contained in:
J. Nick Koston
2026-01-06 19:22:04 -10:00
committed by GitHub
parent fb47bfe92a
commit c387c03944
2 changed files with 16 additions and 4 deletions
+4 -1
View File
@@ -15,7 +15,10 @@ void Text::publish_state(const char *state) { this->publish_state(state, strlen(
void Text::publish_state(const char *state, size_t len) {
this->set_has_state(true);
this->state.assign(state, len);
// Only assign if changed to avoid heap allocation
if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
this->state.assign(state, len);
}
if (this->traits.get_mode() == TEXT_MODE_PASSWORD) {
ESP_LOGD(TAG, "'%s': Sending state " LOG_SECRET("'%s'"), this->get_name().c_str(), this->state.c_str());
} else {