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) {