From ac64dbfb8d3352d84d5571db4883d7fbe67f8c94 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 11 Jul 2025 07:18:15 -1000 Subject: [PATCH] encode perf --- esphome/components/api/proto.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 7be4782c2f..0346b10dd1 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -176,6 +176,18 @@ class ProtoVarInt { out.push_back(val); return; } + // Optimize for 2-byte varints (0x80-0x3FFF) + if (val <= 0x3FFF) { + out.push_back((val & 0x7F) | 0x80); + out.push_back(val >> 7); + return; + } + // For 3+ byte varints, write first 2 bytes directly + out.push_back((val & 0x7F) | 0x80); + out.push_back(((val >> 7) & 0x7F) | 0x80); + val >>= 14; + + // Continue with remaining bytes while (val) { uint8_t temp = val & 0x7F; val >>= 7;