mirror of
https://github.com/esphome/esphome.git
synced 2026-09-03 19:46:02 +00:00
[binary_sensor] Fast-path dedup in send_state_internal before virtual dispatch
Check state == new_state directly before calling virtual set_new_state. Avoids the virtual dispatch chain (BinarySensor::set_new_state → StatefulEntityBase::set_new_state → virtual get_state()) on the common no-change path. Fixes -20% CodSpeed regression on BinarySensorPublish_NoChange benchmark.
This commit is contained in:
@@ -57,7 +57,12 @@ class BinarySensor : public StatefulEntityBase<bool> {
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
void send_state_internal(bool new_state) { this->set_new_state(new_state); }
|
||||
void send_state_internal(bool new_state) {
|
||||
// Fast path: skip virtual dispatch when state hasn't changed
|
||||
if (this->flags_.has_state && this->state == new_state)
|
||||
return;
|
||||
this->set_new_state(new_state);
|
||||
}
|
||||
|
||||
/// Return whether this binary sensor has outputted a state.
|
||||
virtual bool is_status_binary_sensor() const;
|
||||
|
||||
Reference in New Issue
Block a user