From efa6745a5e28313830e94557b4423ff857bed4ff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 May 2025 00:16:25 -0500 Subject: [PATCH] Optimize protobuf varint decoder for ESPHome use case (#8791) --- esphome/components/api/proto.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index b8ee6b7920..e110a58eda 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -20,16 +20,26 @@ class ProtoVarInt { explicit ProtoVarInt(uint64_t value) : value_(value) {} static optional parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed) { - if (consumed != nullptr) - *consumed = 0; - - if (len == 0) + if (len == 0) { + if (consumed != nullptr) + *consumed = 0; return {}; + } - uint64_t result = 0; - uint8_t bitpos = 0; + // Most common case: single-byte varint (values 0-127) + if ((buffer[0] & 0x80) == 0) { + if (consumed != nullptr) + *consumed = 1; + return ProtoVarInt(buffer[0]); + } - for (uint32_t i = 0; i < len; i++) { + // General case for multi-byte varints + // Since we know buffer[0]'s high bit is set, initialize with its value + uint64_t result = buffer[0] & 0x7F; + uint8_t bitpos = 7; + + // Start from the second byte since we've already processed the first + for (uint32_t i = 1; i < len; i++) { uint8_t val = buffer[i]; result |= uint64_t(val & 0x7F) << uint64_t(bitpos); bitpos += 7; @@ -40,7 +50,9 @@ class ProtoVarInt { } } - return {}; + if (consumed != nullptr) + *consumed = 0; + return {}; // Incomplete or invalid varint } uint32_t as_uint32() const { return this->value_; }