From 311230492fe25e9f66285f1d96a623184dfab518 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 28 Oct 2025 14:55:25 -0500 Subject: [PATCH] test --- esphome/components/api/proto.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index f2d8dec3df..51353a42e6 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -221,22 +221,24 @@ class ProtoWriteBuffer { // Single implementation that all overloads delegate to void encode_varint_raw(uint64_t value) { size_t start = this->buffer_->size(); - uint8_t *p; - // Optimize for common small values - calculate size upfront to avoid double resize + // Fast paths for common cases (1-4 bytes) - inline encoding avoids loop overhead if (value < 128) { // 1 byte - very common for field IDs and small lengths this->buffer_->resize(start + 1); this->buffer_->data()[start] = static_cast(value); return; - } else if (value < 16384) { + } + uint8_t *p; + if (value < 16384) { // 2 bytes this->buffer_->resize(start + 2); p = this->buffer_->data() + start; p[0] = (value & 0x7F) | 0x80; p[1] = (value >> 7) & 0x7F; return; - } else if (value < 2097152) { + } + if (value < 2097152) { // 3 bytes this->buffer_->resize(start + 3); p = this->buffer_->data() + start; @@ -244,7 +246,8 @@ class ProtoWriteBuffer { p[1] = ((value >> 7) & 0x7F) | 0x80; p[2] = (value >> 14) & 0x7F; return; - } else if (value < 268435456) { + } + if (value < 268435456) { // 4 bytes this->buffer_->resize(start + 4); p = this->buffer_->data() + start; @@ -274,7 +277,6 @@ class ProtoWriteBuffer { this->buffer_->resize(start + size); p = this->buffer_->data() + start; - size_t bytes = 0; while (value) { uint8_t temp = value & 0x7F;