Remove len==0 guard from parse(), add debug assert

All callers guarantee len > 0 (decode loop checks ptr < end,
header parse checks minimum bytes). Replace runtime check with
ESPHOME_DEBUG_API assert. This removes the len check from the
inline entirely, saving one branch per call site.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
J. Nick Koston
2026-03-08 17:42:37 -10:00
co-authored by Claude Opus 4.6
parent 8b9c4d050d
commit 7185c66779
2 changed files with 6 additions and 6 deletions
-2
View File
@@ -21,8 +21,6 @@ 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
+6 -4
View File
@@ -130,13 +130,15 @@ class ProtoVarInt {
ProtoVarInt() : value_(0) {}
explicit ProtoVarInt(uint64_t value) : value_(value) {}
/// Parse a varint from buffer. Returns result with consumed=0 on failure.
/// Parse a varint from buffer. Caller must ensure len >= 1.
/// Returns result with consumed=0 on failure (truncated multi-byte varint).
static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse(const uint8_t *buffer, uint32_t len) {
#ifdef ESPHOME_DEBUG_API
assert(len > 0); // All callers guarantee len > 0
#endif
// Fast path: single-byte varints (0-127) are the most common case
// (booleans, small enums, field tags, small message sizes/types).
// 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]]
if ((buffer[0] & 0x80) == 0) [[likely]]
return {buffer[0], 1};
return parse_slow(buffer, len);
}