mirror of
https://github.com/esphome/esphome.git
synced 2026-07-11 01:15:33 +00:00
[packet_transport] Store parent pointer to enable inline Callback storage
Add a back-pointer to PacketTransport in the Sensor and BinarySensor structs so the state callback lambdas can capture only [&sensor] (one pointer) instead of [this, &sensor] (two pointers). This brings the capture size within the Callback inline threshold (sizeof(void*)), avoiding a heap allocation per callback registration.
This commit is contained in:
@@ -221,16 +221,16 @@ void PacketTransport::setup() {
|
||||
}
|
||||
#ifdef USE_SENSOR
|
||||
for (auto &sensor : this->sensors_) {
|
||||
sensor.sensor->add_on_state_callback([this, &sensor](float x) {
|
||||
this->updated_ = true;
|
||||
sensor.sensor->add_on_state_callback([&sensor](float x) {
|
||||
sensor.parent->updated_ = true;
|
||||
sensor.updated = true;
|
||||
});
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
for (auto &sensor : this->binary_sensors_) {
|
||||
sensor.sensor->add_on_state_callback([this, &sensor](bool value) {
|
||||
this->updated_ = true;
|
||||
sensor.sensor->add_on_state_callback([&sensor](bool value) {
|
||||
sensor.parent->updated_ = true;
|
||||
sensor.updated = true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,11 +37,14 @@ struct Provider {
|
||||
#endif
|
||||
};
|
||||
|
||||
class PacketTransport;
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
struct Sensor {
|
||||
sensor::Sensor *sensor;
|
||||
const char *id;
|
||||
bool updated;
|
||||
PacketTransport *parent;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
@@ -49,10 +52,18 @@ struct BinarySensor {
|
||||
binary_sensor::BinarySensor *sensor;
|
||||
const char *id;
|
||||
bool updated;
|
||||
PacketTransport *parent;
|
||||
};
|
||||
#endif
|
||||
|
||||
class PacketTransport : public PollingComponent {
|
||||
#ifdef USE_SENSOR
|
||||
friend struct Sensor;
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
friend struct BinarySensor;
|
||||
#endif
|
||||
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
@@ -61,7 +72,7 @@ class PacketTransport : public PollingComponent {
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void add_sensor(const char *id, sensor::Sensor *sensor) {
|
||||
Sensor st{sensor, id, true};
|
||||
Sensor st{sensor, id, true, this};
|
||||
this->sensors_.push_back(st);
|
||||
}
|
||||
void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
|
||||
@@ -71,7 +82,7 @@ class PacketTransport : public PollingComponent {
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
|
||||
BinarySensor st{sensor, id, true};
|
||||
BinarySensor st{sensor, id, true, this};
|
||||
this->binary_sensors_.push_back(st);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user