1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00
This commit is contained in:
J. Nick Koston
2025-10-28 14:55:25 -05:00
parent 1c114093ec
commit 311230492f

View File

@@ -221,22 +221,24 @@ class ProtoWriteBuffer {
// Single implementation that all overloads delegate to
void encode_varint_raw(uint64_t value) {
size_t start = this->buffer_->size();
uint8_t *p;
// Optimize for common small values - calculate size upfront to avoid double resize
// Fast paths for common cases (1-4 bytes) - inline encoding avoids loop overhead
if (value < 128) {
// 1 byte - very common for field IDs and small lengths
this->buffer_->resize(start + 1);
this->buffer_->data()[start] = static_cast<uint8_t>(value);
return;
} else if (value < 16384) {
}
uint8_t *p;
if (value < 16384) {
// 2 bytes
this->buffer_->resize(start + 2);
p = this->buffer_->data() + start;
p[0] = (value & 0x7F) | 0x80;
p[1] = (value >> 7) & 0x7F;
return;
} else if (value < 2097152) {
}
if (value < 2097152) {
// 3 bytes
this->buffer_->resize(start + 3);
p = this->buffer_->data() + start;
@@ -244,7 +246,8 @@ class ProtoWriteBuffer {
p[1] = ((value >> 7) & 0x7F) | 0x80;
p[2] = (value >> 14) & 0x7F;
return;
} else if (value < 268435456) {
}
if (value < 268435456) {
// 4 bytes
this->buffer_->resize(start + 4);
p = this->buffer_->data() + start;
@@ -274,7 +277,6 @@ class ProtoWriteBuffer {
this->buffer_->resize(start + size);
p = this->buffer_->data() + start;
size_t bytes = 0;
while (value) {
uint8_t temp = value & 0x7F;