[api] Hoist pos_ to local in encode_varint_raw_64 to avoid reload per byte

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.
This commit is contained in:
J. Nick Koston
2026-03-28 17:43:11 -10:00
parent 52897fd067
commit 2137e4600a
+6 -4
View File
@@ -224,13 +224,15 @@ 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) {
this->debug_check_bounds_(1);
*this->pos_++ = static_cast<uint8_t>(value | 0x80);
*pos++ = static_cast<uint8_t>(value | 0x80);
value >>= 7;
}
this->debug_check_bounds_(1);
*this->pos_++ = static_cast<uint8_t>(value);
*pos++ = static_cast<uint8_t>(value);
this->pos_ = pos;
}
/**
* Encode a field key (tag/wire type combination).