From 7d529485294ac9e46b9fc74a7b4a5155245e66a2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 7 Mar 2026 08:50:01 -1000 Subject: [PATCH 1/2] [api] Fix comment to include LN882x in fallback list --- esphome/components/api/proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 589fae98951..57dcc88f539 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -237,7 +237,7 @@ class Proto32Bit { // NOTE: Proto64Bit class removed - wire type 1 (64-bit fixed) not supported /// Helper to use make_unique_for_overwrite where available (skips zero-fill), -/// falling back to make_unique on older GCC (ESP8266, BK72xx). +/// falling back to make_unique on older GCC (ESP8266, BK72xx, LN882x). inline std::unique_ptr make_buffer(size_t n) { #if defined(USE_ESP8266) || defined(USE_BK72XX) || defined(USE_LN882X) return std::make_unique(n); From d3c5d91469b012fa3c62dfe92c9978ac6ac0180b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 7 Mar 2026 09:03:55 -1000 Subject: [PATCH 2/2] [api] Expand ProtoByteBuffer comment to explain why skipping zero-fill is safe --- esphome/components/api/proto.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 57dcc88f539..0e861a19d37 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -247,8 +247,15 @@ inline std::unique_ptr make_buffer(size_t n) { } /// Byte buffer that skips zero-initialization on resize(). -/// Used as the shared protobuf write buffer to avoid wasted memset -/// on bytes that will be overwritten by the encoder. +/// +/// std::vector::resize() zero-fills new bytes via memset. The shared +/// protobuf write buffer is clear()'d before every message, so resize() always +/// grows from size 0 — memsetting the entire requested region. Every byte is +/// then overwritten by the encoder, making the zero-fill pure waste. +/// +/// Safe because: the encoder writes exactly calculate_size() bytes, the frame +/// helper sends exactly those bytes, and debug_check_bounds_ validates writes +/// in debug builds. No byte is ever read before being written. class ProtoByteBuffer { public: void clear() { this->size_ = 0; }