From 8bf3d52fb0b0059c6216ba09f6a189314ca69eca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:25:10 -1000 Subject: [PATCH 1/6] tidy --- esphome/core/application.h | 42 +++++++++++++++++++------------------- esphome/core/helpers.h | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/esphome/core/application.h b/esphome/core/application.h index c91cba8d19..b7824a254b 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -493,68 +493,68 @@ class Application { std::vector areas_{}; #endif #ifdef USE_BINARY_SENSOR - static_vector binary_sensors_{}; + StaticVector binary_sensors_{}; #endif #ifdef USE_SWITCH - static_vector switches_{}; + StaticVector switches_{}; #endif #ifdef USE_BUTTON - static_vector buttons_{}; + StaticVector buttons_{}; #endif #ifdef USE_EVENT - static_vector events_{}; + StaticVector events_{}; #endif #ifdef USE_SENSOR - static_vector sensors_{}; + StaticVector sensors_{}; #endif #ifdef USE_TEXT_SENSOR - static_vector text_sensors_{}; + StaticVector text_sensors_{}; #endif #ifdef USE_FAN - static_vector fans_{}; + StaticVector fans_{}; #endif #ifdef USE_COVER - static_vector covers_{}; + StaticVector covers_{}; #endif #ifdef USE_CLIMATE - static_vector climates_{}; + StaticVector climates_{}; #endif #ifdef USE_LIGHT - static_vector lights_{}; + StaticVector lights_{}; #endif #ifdef USE_NUMBER - static_vector numbers_{}; + StaticVector numbers_{}; #endif #ifdef USE_DATETIME_DATE - static_vector dates_{}; + StaticVector dates_{}; #endif #ifdef USE_DATETIME_TIME - static_vector times_{}; + StaticVector times_{}; #endif #ifdef USE_DATETIME_DATETIME - static_vector datetimes_{}; + StaticVector datetimes_{}; #endif #ifdef USE_SELECT - static_vector selects_{}; + StaticVector selects_{}; #endif #ifdef USE_TEXT - static_vector texts_{}; + StaticVector texts_{}; #endif #ifdef USE_LOCK - static_vector locks_{}; + StaticVector locks_{}; #endif #ifdef USE_VALVE - static_vector valves_{}; + StaticVector valves_{}; #endif #ifdef USE_MEDIA_PLAYER - static_vector media_players_{}; + StaticVector media_players_{}; #endif #ifdef USE_ALARM_CONTROL_PANEL - static_vector + StaticVector alarm_control_panels_{}; #endif #ifdef USE_UPDATE - static_vector updates_{}; + StaticVector updates_{}; #endif #ifdef USE_SOCKET_SELECT_SUPPORT diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 05e57a267e..e937c3bdf2 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -95,7 +95,7 @@ template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); ///@{ /// Minimal static vector - saves memory by avoiding std::vector overhead -template class static_vector { +template class StaticVector { public: using value_type = T; using iterator = typename std::array::iterator; From 13c749ceda7c0de4fd5ccfcd68a4c2c5a71d3125 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:26:04 -1000 Subject: [PATCH 2/6] preen --- esphome/core/component_iterator.cpp | 16 ++++++---------- esphome/core/helpers.h | 2 ++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index e012412ab6..583f9c39af 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -20,18 +20,14 @@ void ComponentIterator::begin(bool include_internal) { template void ComponentIterator::process_platform_item_(const Container &items, bool (ComponentIterator::*on_item)(typename Container::value_type)) { - // Since static_vector doesn't have size(), we need to iterate differently - size_t index = 0; - for (auto *item : items) { - if (index++ == this->at_) { - if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { - this->at_++; - } - return; + if (this->at_ >= items.size()) { + this->advance_platform_(); + } else { + auto *item = items[this->at_]; + if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { + this->at_++; } } - // If we get here, we've reached the end - this->advance_platform_(); } void ComponentIterator::advance_platform_() { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e937c3bdf2..04cac5072f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -113,6 +113,8 @@ template class StaticVector { } } + size_t size() const { return count_; } + // For range-based for loops iterator begin() { return data_.begin(); } iterator end() { return data_.begin() + count_; } From a25edf93d68943c7995bf000d1beb1804ebd677d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:26:13 -1000 Subject: [PATCH 3/6] preen --- esphome/core/helpers.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 04cac5072f..b05cc11029 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -115,6 +115,9 @@ template class StaticVector { size_t size() const { return count_; } + T &operator[](size_t i) { return data_[i]; } + const T &operator[](size_t i) const { return data_[i]; } + // For range-based for loops iterator begin() { return data_.begin(); } iterator end() { return data_.begin() + count_; } From 7e25846cada60d9795840f158bd036de2381ab70 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:26:51 -1000 Subject: [PATCH 4/6] preen --- esphome/core/component_iterator.cpp | 8 ++++---- esphome/core/component_iterator.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index 583f9c39af..1e8f670d8b 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -17,13 +17,13 @@ void ComponentIterator::begin(bool include_internal) { this->include_internal_ = include_internal; } -template -void ComponentIterator::process_platform_item_(const Container &items, - bool (ComponentIterator::*on_item)(typename Container::value_type)) { +template +void ComponentIterator::process_platform_item_(const std::vector &items, + bool (ComponentIterator::*on_item)(PlatformItem *)) { if (this->at_ >= items.size()) { this->advance_platform_(); } else { - auto *item = items[this->at_]; + PlatformItem *item = items[this->at_]; if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { this->at_++; } diff --git a/esphome/core/component_iterator.h b/esphome/core/component_iterator.h index 6b44990529..7a9771b8f2 100644 --- a/esphome/core/component_iterator.h +++ b/esphome/core/component_iterator.h @@ -172,9 +172,9 @@ class ComponentIterator { uint16_t at_{0}; // Supports up to 65,535 entities per type bool include_internal_{false}; - template - void process_platform_item_(const Container &items, - bool (ComponentIterator::*on_item)(typename Container::value_type)); + template + void process_platform_item_(const std::vector &items, + bool (ComponentIterator::*on_item)(PlatformItem *)); void advance_platform_(); }; From 591b9ce87b468de86a5119c5fafd524c50d934e6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:28:48 -1000 Subject: [PATCH 5/6] preen --- esphome/core/component_iterator.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/core/component_iterator.h b/esphome/core/component_iterator.h index 7a9771b8f2..6b44990529 100644 --- a/esphome/core/component_iterator.h +++ b/esphome/core/component_iterator.h @@ -172,9 +172,9 @@ class ComponentIterator { uint16_t at_{0}; // Supports up to 65,535 entities per type bool include_internal_{false}; - template - void process_platform_item_(const std::vector &items, - bool (ComponentIterator::*on_item)(PlatformItem *)); + template + void process_platform_item_(const Container &items, + bool (ComponentIterator::*on_item)(typename Container::value_type)); void advance_platform_(); }; From 4de68ded794acfb80b4c86aa1167447c6487b52c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 15:31:01 -1000 Subject: [PATCH 6/6] preen --- esphome/core/component_iterator.cpp | 13 ------------- esphome/core/component_iterator.h | 12 +++++++++++- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index 1e8f670d8b..668c4a1fda 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -17,19 +17,6 @@ void ComponentIterator::begin(bool include_internal) { this->include_internal_ = include_internal; } -template -void ComponentIterator::process_platform_item_(const std::vector &items, - bool (ComponentIterator::*on_item)(PlatformItem *)) { - if (this->at_ >= items.size()) { - this->advance_platform_(); - } else { - PlatformItem *item = items[this->at_]; - if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { - this->at_++; - } - } -} - void ComponentIterator::advance_platform_() { this->state_ = static_cast(static_cast(this->state_) + 1); this->at_ = 0; diff --git a/esphome/core/component_iterator.h b/esphome/core/component_iterator.h index 6b44990529..fdc30485bc 100644 --- a/esphome/core/component_iterator.h +++ b/esphome/core/component_iterator.h @@ -174,7 +174,17 @@ class ComponentIterator { template void process_platform_item_(const Container &items, - bool (ComponentIterator::*on_item)(typename Container::value_type)); + bool (ComponentIterator::*on_item)(typename Container::value_type)) { + if (this->at_ >= items.size()) { + this->advance_platform_(); + } else { + typename Container::value_type item = items[this->at_]; + if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { + this->at_++; + } + } + } + void advance_platform_(); };