From d3c5d91469b012fa3c62dfe92c9978ac6ac0180b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 7 Mar 2026 09:03:55 -1000 Subject: [PATCH] [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; }