Reduce parse() inline size by folding len==0 into slow path

Move the len==0 check from the inlined fast path into parse_slow(),
saving one branch + one return-value setup per inline site (~6-8
bytes per call site on Xtensa/xtensa-lx106).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
J. Nick Koston
2026-03-08 17:38:12 -10:00
co-authored by Claude Opus 4.6
parent 3653e0cf9f
commit 8b9c4d050d
2 changed files with 5 additions and 3 deletions
+2
View File
@@ -21,6 +21,8 @@ void ProtoWriteBuffer::encode_varint_raw_slow_(uint32_t value) {
}
ProtoVarIntResult ProtoVarInt::parse_slow(const uint8_t *buffer, uint32_t len) {
if (len == 0)
return {0, 0};
// Multi-byte varint: first byte already checked to have high bit set
uint32_t result32 = buffer[0] & 0x7F;
#ifdef USE_API_VARINT64
+3 -3
View File
@@ -132,11 +132,11 @@ class ProtoVarInt {
/// Parse a varint from buffer. Returns result with consumed=0 on failure.
static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse(const uint8_t *buffer, uint32_t len) {
if (len == 0)
return {0, 0};
// Fast path: single-byte varints (0-127) are the most common case
// (booleans, small enums, field tags, small message sizes/types).
if ((buffer[0] & 0x80) == 0) [[likely]]
// len==0 check is folded into the condition to minimize inline size;
// parse_slow() handles len==0.
if (len != 0 && (buffer[0] & 0x80) == 0) [[likely]]
return {buffer[0], 1};
return parse_slow(buffer, len);
}