[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

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 {

View File

@@ -32,7 +32,10 @@ void TextSensor::publish_state(const char *state) { this->publish_state(state, s
void TextSensor::publish_state(const char *state, size_t len) {
if (this->filter_list_ == nullptr) {
// No filters: raw_state == state, store once and use for both callbacks
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);
}
this->raw_callback_.call(this->state);
ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->state.c_str());
this->notify_frontend_();
@@ -40,7 +43,10 @@ void TextSensor::publish_state(const char *state, size_t len) {
// Has filters: need separate raw storage
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
this->raw_state.assign(state, len);
// Only assign if changed to avoid heap allocation
if (len != this->raw_state.size() || memcmp(state, this->raw_state.data(), len) != 0) {
this->raw_state.assign(state, len);
}
this->raw_callback_.call(this->raw_state);
ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->raw_state.c_str());
this->filter_list_->input(this->raw_state);
@@ -101,7 +107,10 @@ void TextSensor::internal_send_state_to_frontend(const std::string &state) {
}
void TextSensor::internal_send_state_to_frontend(const char *state, size_t len) {
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);
}
this->notify_frontend_();
}