From 537af1fe6b26b54d949cd8bb6ac4d25d44c626d3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 28 Mar 2026 19:39:40 -1000 Subject: [PATCH] [api] Hoist pos_ to local in encode_varint_raw_64 to avoid reload per byte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use a __restrict__ local pointer for the varint write loop so the compiler can keep it in a register instead of reloading pos_ from memory on each iteration. Eliminates the store→load dependency chain that was causing 7 load/store pairs for a typical 48-bit BLE address varint. --- esphome/components/api/proto.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 3989c7c424..48b450e819 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -224,8 +224,6 @@ class ProtoWriteBuffer { this->encode_varint_raw_slow_(value); } void encode_varint_raw_64(uint64_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_; while (value > 0x7F) { *pos++ = static_cast(value | 0x80);