mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 03:16:37 +00:00
[text_sensor][text] Avoid heap allocation when state unchanged (#13044)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user