From 4df3bfe85d66a35c5ac68ee3aaea2269a7e956fc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 13:39:37 -0500 Subject: [PATCH 1/6] review --- esphome/components/api/api_connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index cac2fb5d83..aa323d339d 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -303,7 +303,7 @@ class APIConnection : public APIServerConnection { #ifdef USE_VOICE_ASSISTANT // Helper to check voice assistant validity and connection ownership - bool check_voice_assistant_api_connection_() const; + inline bool check_voice_assistant_api_connection_() const; #endif // Helper method to process multiple entities from an iterator in a batch From e5df43b9347b929b65b2d432c7df48bf77ca63ba Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 14:38:49 -0500 Subject: [PATCH 2/6] cleanup --- esphome/components/bluetooth_proxy/bluetooth_proxy.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index e0370328f2..a5e8ec0860 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -59,10 +59,12 @@ bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) // This achieves ~97% WiFi MTU utilization while staying under the limit static constexpr size_t FLUSH_BATCH_SIZE = 16; -// Global batch buffer to avoid guard variable (saves 8 bytes) +namespace { +// Batch buffer in anonymous namespace to avoid guard variable (saves 8 bytes) // This is initialized at program startup before any threads // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -static std::vector batch_buffer; +std::vector batch_buffer; +} // namespace static std::vector &get_batch_buffer() { return batch_buffer; } From c1a6e8232245534c9ef497780546bd47234321e2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 14:58:45 -0500 Subject: [PATCH 3/6] fix calculation --- esphome/components/logger/logger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 41aa2313b6..a7d6852c43 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -121,8 +121,8 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas if (this->baud_rate_ > 0) { this->write_msg_(this->tx_buffer_ + msg_start); } - size_t msg_length = this->tx_buffer_at_ - msg_start - 1; // -1 to exclude null terminator - this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length); + size_t msg_length = this->tx_buffer_at_ - 1; // -1 to exclude null terminator + this->log_callback_.call(level, tag, this->tx_buffer_, msg_length); global_recursion_guard_ = false; } From 73b786c22e3e5f99a0af4959272d80332e91ad05 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 15:01:15 -0500 Subject: [PATCH 4/6] fix calculation --- esphome/components/logger/logger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index a7d6852c43..41aa2313b6 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -121,8 +121,8 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas if (this->baud_rate_ > 0) { this->write_msg_(this->tx_buffer_ + msg_start); } - size_t msg_length = this->tx_buffer_at_ - 1; // -1 to exclude null terminator - this->log_callback_.call(level, tag, this->tx_buffer_, msg_length); + size_t msg_length = this->tx_buffer_at_ - msg_start - 1; // -1 to exclude null terminator + this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length); global_recursion_guard_ = false; } From 01a6b38b892ad2b02c2a5d2f9381735ba1c49d92 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 15:08:11 -0500 Subject: [PATCH 5/6] null term is already there --- esphome/components/logger/logger.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 41aa2313b6..7534a02e2e 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -121,7 +121,8 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas if (this->baud_rate_ > 0) { this->write_msg_(this->tx_buffer_ + msg_start); } - size_t msg_length = this->tx_buffer_at_ - msg_start - 1; // -1 to exclude null terminator + size_t msg_length = + this->tx_buffer_at_ - msg_start; // Don't subtract 1 - tx_buffer_at_ is already at the null terminator position this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length); global_recursion_guard_ = false; From ab993c6d5a3b008a305b79f61dd868150317855e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 15:18:27 -0500 Subject: [PATCH 6/6] add diagram --- esphome/components/logger/logger.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 7534a02e2e..db807f7e53 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -90,6 +90,25 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch #ifdef USE_STORE_LOG_STR_IN_FLASH // Implementation for ESP8266 with flash string support. // Note: USE_STORE_LOG_STR_IN_FLASH is only defined for ESP8266. +// +// This function handles format strings stored in flash memory (PROGMEM) to save RAM. +// The buffer is used in a special way to avoid allocating extra memory: +// +// Memory layout during execution: +// Step 1: Copy format string from flash to buffer +// tx_buffer_: [format_string][null][.....................] +// tx_buffer_at_: ------------------^ +// msg_start: saved here -----------^ +// +// Step 2: format_log_to_buffer_with_terminator_ reads format string from beginning +// and writes formatted output starting at msg_start position +// tx_buffer_: [format_string][null][formatted_message][null] +// tx_buffer_at_: -------------------------------------^ +// +// Step 3: Output the formatted message (starting at msg_start) +// write_msg_ and callbacks receive: this->tx_buffer_ + msg_start +// which points to: [formatted_message][null] +// void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args) { // NOLINT if (level > this->level_for(tag) || global_recursion_guard_)