From 51cff74f0390cdb2fc9b97decd2a9c41f3aa4ee6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 7 Mar 2026 13:32:23 -1000 Subject: [PATCH] [api] Outline ProtoByteBuffer reallocation into grow_() The capacity check in reserve()/resize() is the hot path and stays inline via ESPHOME_ALWAYS_INLINE. The actual reallocation (make_buffer + memcpy) is a cold path outlined into grow_() to avoid bloating every call site. resize() delegates to reserve() for the capacity check since both inline to the same code. --- esphome/components/api/proto.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 1312c87400..c24006ec29 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -259,13 +259,12 @@ inline std::unique_ptr make_buffer(size_t n) { class ProtoByteBuffer { public: void clear() { this->size_ = 0; } - void reserve(size_t n) { + inline void reserve(size_t n) ESPHOME_ALWAYS_INLINE { if (n > this->capacity_) this->grow_(n); } - void resize(size_t n) { - if (n > this->capacity_) - this->grow_(n); + inline void resize(size_t n) ESPHOME_ALWAYS_INLINE { + this->reserve(n); this->size_ = n; // no zero-fill } uint8_t *data() { return this->data_.get(); }