mirror of
https://github.com/esphome/esphome.git
synced 2026-09-20 19:48:39 +00:00
[packet_transport] Use FixedVector for sensor lists to eliminate realloc machinery
The sensor and binary sensor counts are known at config time, so pre-allocate with FixedVector::init() instead of using std::vector which pulls in _M_realloc_insert template instantiation code.
This commit is contained in:
@@ -177,13 +177,19 @@ async def register_packet_transport(var, config):
|
||||
cg.add(var.set_provider_encryption(name, hash_encryption_key(encryption)))
|
||||
|
||||
is_provider = False
|
||||
for sens_conf in config.get(CONF_SENSORS, ()):
|
||||
sensors = config.get(CONF_SENSORS, ())
|
||||
binary_sensors = config.get(CONF_BINARY_SENSORS, ())
|
||||
if sensors:
|
||||
cg.add(var.set_sensor_count(len(sensors)))
|
||||
if binary_sensors:
|
||||
cg.add(var.set_binary_sensor_count(len(binary_sensors)))
|
||||
for sens_conf in sensors:
|
||||
is_provider = True
|
||||
sens_id = sens_conf[CONF_ID]
|
||||
sensor = await cg.get_variable(sens_id)
|
||||
bcst_id = sens_conf.get(CONF_BROADCAST_ID, sens_id.id)
|
||||
cg.add(var.add_sensor(bcst_id, sensor))
|
||||
for sens_conf in config.get(CONF_BINARY_SENSORS, ()):
|
||||
for sens_conf in binary_sensors:
|
||||
is_provider = True
|
||||
sens_id = sens_conf[CONF_ID]
|
||||
sensor = await cg.get_variable(sens_id)
|
||||
|
||||
@@ -221,7 +221,7 @@ void PacketTransport::setup() {
|
||||
}
|
||||
#ifdef USE_SENSOR
|
||||
for (auto &sensor : this->sensors_) {
|
||||
// [&sensor] is safe: sensor refers to a vector element that is never modified after setup(),
|
||||
// [&sensor] is safe: sensor refers to a FixedVector element that never reallocates,
|
||||
// so the reference remains valid for the component's lifetime.
|
||||
sensor.sensor->add_on_state_callback([&sensor](float x) {
|
||||
sensor.parent->updated_ = true;
|
||||
@@ -231,7 +231,7 @@ void PacketTransport::setup() {
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
for (auto &sensor : this->binary_sensors_) {
|
||||
// [&sensor] is safe: sensor refers to a vector element that is never modified after setup(),
|
||||
// [&sensor] is safe: sensor refers to a FixedVector element that never reallocates,
|
||||
// so the reference remains valid for the component's lifetime.
|
||||
sensor.sensor->add_on_state_callback([&sensor](bool value) {
|
||||
sensor.parent->updated_ = true;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#ifdef USE_SENSOR
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
@@ -71,6 +72,7 @@ class PacketTransport : public PollingComponent {
|
||||
void dump_config() override;
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void set_sensor_count(size_t count) { this->sensors_.init(count); }
|
||||
void add_sensor(const char *id, sensor::Sensor *sensor) {
|
||||
Sensor st{sensor, id, true, this};
|
||||
this->sensors_.push_back(st);
|
||||
@@ -81,6 +83,7 @@ class PacketTransport : public PollingComponent {
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void set_binary_sensor_count(size_t count) { this->binary_sensors_.init(count); }
|
||||
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
|
||||
BinarySensor st{sensor, id, true, this};
|
||||
this->binary_sensors_.push_back(st);
|
||||
@@ -152,11 +155,11 @@ class PacketTransport : public PollingComponent {
|
||||
std::vector<uint8_t> encryption_key_{};
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
std::vector<Sensor> sensors_{};
|
||||
FixedVector<Sensor> sensors_{};
|
||||
string_map_t<string_map_t<sensor::Sensor *>> remote_sensors_{};
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
std::vector<BinarySensor> binary_sensors_{};
|
||||
FixedVector<BinarySensor> binary_sensors_{};
|
||||
string_map_t<string_map_t<binary_sensor::BinarySensor *>> remote_binary_sensors_{};
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user