[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.
This commit is contained in:
J. Nick Koston
2026-03-07 13:32:23 -10:00
parent 9e082edecb
commit 51cff74f03
+3 -4
View File
@@ -259,13 +259,12 @@ inline std::unique_ptr<uint8_t[]> 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(); }