From 04552e240eb335579883ff18b12979a8452718c2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Mar 2026 21:19:22 -1000 Subject: [PATCH] [combination] Use FixedVector for sensor sources to eliminate realloc machinery The source count is known at config time, so pre-allocate with FixedVector::init() instead of using std::vector which pulls in _M_realloc_insert template instantiation code. --- esphome/components/combination/combination.h | 5 +++-- esphome/components/combination/sensor.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/esphome/components/combination/combination.h b/esphome/components/combination/combination.h index f99eeb5872..6e3f7c5ece 100644 --- a/esphome/components/combination/combination.h +++ b/esphome/components/combination/combination.h @@ -1,10 +1,10 @@ #pragma once #include "esphome/core/component.h" +#include "esphome/core/helpers.h" #include "esphome/components/sensor/sensor.h" #include -#include namespace esphome { namespace combination { @@ -42,6 +42,7 @@ class CombinationNoParameterComponent : public CombinationComponent { // Base class for opertions that require one parameter to compute the combination class CombinationOneParameterComponent : public CombinationComponent { public: + void set_source_count(size_t count) { this->sensor_sources_.init(count); } void add_source(Sensor *sensor, std::function const &compute); void add_source(Sensor *sensor, float value); @@ -55,7 +56,7 @@ class CombinationOneParameterComponent : public CombinationComponent { }; protected: - std::vector sensor_sources_; + FixedVector sensor_sources_; }; class KalmanCombinationComponent : public CombinationOneParameterComponent { diff --git a/esphome/components/combination/sensor.py b/esphome/components/combination/sensor.py index 0204162e8d..327cedee1e 100644 --- a/esphome/components/combination/sensor.py +++ b/esphome/components/combination/sensor.py @@ -180,6 +180,9 @@ async def to_code(config): if proces_std_dev := config.get(CONF_PROCESS_STD_DEV): cg.add(var.set_process_std_dev(proces_std_dev)) + if config[CONF_TYPE] in (CONF_KALMAN, CONF_LINEAR): + cg.add(var.set_source_count(len(config[CONF_SOURCES]))) + for source_conf in config[CONF_SOURCES]: source = await cg.get_variable(source_conf[CONF_SOURCE]) if config[CONF_TYPE] == CONF_KALMAN: