From d2e4d2c46a5cee2148372845b47ce062adf85d58 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Sep 2026 11:40:01 +0200 Subject: [PATCH] [api] Outline the fixed32 writers only where memcpy is a call On the ESP8266 the inline write was already a single store, so the outlined helper cost a call per fixed32 field: sensor state encode went from 615 to 864 ns on a d1 mini. ESP32 builds pass -fno-builtin-memcpy, where the shared copy is both smaller and faster (562 to 328 ns on an atom), so the gate is now USE_ESP32. --- esphome/components/api/proto.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index f7dd065a68..b7196089be 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -287,11 +287,12 @@ class ProtoWriteBuffer { uint8_t *pos_; }; -// Outlined on embedded targets to save flash; on the host the write is a single store. -#ifdef USE_HOST -#define PROTO_OUTLINE_FOR_SIZE inline -#else +// ESP32 builds pass -fno-builtin-memcpy, so the inline write was already a memcpy call there and +// one shared copy is both smaller and faster. Elsewhere memcpy inlines to a single store, so keep it. +#ifdef USE_ESP32 #define PROTO_OUTLINE_FOR_SIZE __attribute__((noinline)) +#else +#define PROTO_OUTLINE_FOR_SIZE inline #endif // Varint encoding thresholds — used by both proto_encode_* free functions and ProtoSize.