From 541c697a42e5ff46434a3f4fdcf4e8e1cb6c6777 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 18:52:49 -1000 Subject: [PATCH 1/4] [mdns] Use FixedVector for txt_records to reduce flash usage --- esphome/components/mdns/mdns_component.cpp | 4 ++-- esphome/components/mdns/mdns_component.h | 2 +- esphome/core/helpers.h | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index fea3ced99f..ef585db51b 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -83,7 +83,7 @@ void MDNSComponent::compile_records_(StaticVector port; - std::vector txt_records; + FixedVector txt_records; }; class MDNSComponent : public Component { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e352c9c415..b94826629f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -197,6 +197,15 @@ template class FixedVector { public: FixedVector() = default; + /// Constructor from initializer list - allocates exact size needed + /// This enables brace initialization: FixedVector v = {1, 2, 3}; + FixedVector(std::initializer_list init) { + init(init.size()); + for (const auto &item : init) { + push_back(item); + } + } + ~FixedVector() { cleanup_(); } // Disable copy operations (avoid accidental expensive copies) From ac35c97a44a339dd6e69bcc503a5aa9edec5e156 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 18:59:46 -1000 Subject: [PATCH 2/4] we need copy now --- esphome/core/helpers.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index b94826629f..c0e73b70e0 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -208,9 +208,30 @@ template class FixedVector { ~FixedVector() { cleanup_(); } - // Disable copy operations (avoid accidental expensive copies) - FixedVector(const FixedVector &) = delete; - FixedVector &operator=(const FixedVector &) = delete; + // Copy constructor - performs deep copy + FixedVector(const FixedVector &other) { + if (other.size_ > 0) { + init(other.size_); + for (size_t i = 0; i < other.size_; i++) { + push_back(other.data_[i]); + } + } + } + + // Copy assignment operator - performs deep copy + FixedVector &operator=(const FixedVector &other) { + if (this != &other) { + cleanup_(); + reset_(); + if (other.size_ > 0) { + init(other.size_); + for (size_t i = 0; i < other.size_; i++) { + push_back(other.data_[i]); + } + } + } + return *this; + } // Enable move semantics (allows use in move-only containers like std::vector) FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { From 45014db02796aad16e103f649f30f955baa6c242 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 19:05:26 -1000 Subject: [PATCH 3/4] preen --- esphome/components/mdns/mdns_component.cpp | 4 +-- esphome/core/helpers.h | 29 +++------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index ef585db51b..7b36fce229 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -175,8 +175,8 @@ void MDNSComponent::compile_records_(StaticVectorservices_ = services; + // Move to member variable if storage is enabled (verbose logging, OpenThread, or extra services) + this->services_ = std::move(services); #endif } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index c0e73b70e0..a9c0427917 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -208,32 +208,11 @@ template class FixedVector { ~FixedVector() { cleanup_(); } - // Copy constructor - performs deep copy - FixedVector(const FixedVector &other) { - if (other.size_ > 0) { - init(other.size_); - for (size_t i = 0; i < other.size_; i++) { - push_back(other.data_[i]); - } - } - } + // Disable copy operations - use std::move() to transfer ownership + FixedVector(const FixedVector &) = delete; + FixedVector &operator=(const FixedVector &) = delete; - // Copy assignment operator - performs deep copy - FixedVector &operator=(const FixedVector &other) { - if (this != &other) { - cleanup_(); - reset_(); - if (other.size_ > 0) { - init(other.size_); - for (size_t i = 0; i < other.size_; i++) { - push_back(other.data_[i]); - } - } - } - return *this; - } - - // Enable move semantics (allows use in move-only containers like std::vector) + // Enable move semantics FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { other.reset_(); } From fc30326e603834154703b66e8a373bc2cf1ad498 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 19:06:02 -1000 Subject: [PATCH 4/4] preen --- esphome/core/helpers.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index a9c0427917..b94826629f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -208,11 +208,11 @@ template class FixedVector { ~FixedVector() { cleanup_(); } - // Disable copy operations - use std::move() to transfer ownership + // Disable copy operations (avoid accidental expensive copies) FixedVector(const FixedVector &) = delete; FixedVector &operator=(const FixedVector &) = delete; - // Enable move semantics + // Enable move semantics (allows use in move-only containers like std::vector) FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { other.reset_(); }