From 83e4478bc266e89a96966b8fdeb5af7105459578 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 28 Mar 2026 19:45:49 -1000 Subject: [PATCH] [api] Apply __restrict__ local hoist to encode_varint_raw_slow_ Same optimization as encode_varint_raw_64: hoist pos_ into a __restrict__ local so the compiler keeps it in a register across the loop instead of reloading from memory each iteration. --- esphome/components/api/proto.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/proto.cpp b/esphome/components/api/proto.cpp index 4f5b3f0918f..6e348dde44e 100644 --- a/esphome/components/api/proto.cpp +++ b/esphome/components/api/proto.cpp @@ -11,13 +11,15 @@ static const char *const TAG = "api.proto"; uint32_t ProtoSize::varint_slow(uint32_t value) { return varint_wide(value); } void ProtoWriteBuffer::encode_varint_raw_slow_(uint32_t value) { + // Use __restrict__ so the compiler knows pos doesn't alias this-> + // and can keep it in a register across the loop. + uint8_t *__restrict__ pos = this->pos_; do { - this->debug_check_bounds_(1); - *this->pos_++ = static_cast(value | 0x80); + *pos++ = static_cast(value | 0x80); value >>= 7; } while (value > 0x7F); - this->debug_check_bounds_(1); - *this->pos_++ = static_cast(value); + *pos++ = static_cast(value); + this->pos_ = pos; } ProtoVarIntResult ProtoVarInt::parse_slow(const uint8_t *buffer, uint32_t len) {