[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:
J. Nick Koston
2026-03-22 18:38:34 -10:00
parent 4a200f8a2b
commit cd5b19ab0d
@@ -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;