From 9a896413770ef88735bbb1bcb4156670a3c4ddb3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Mar 2026 10:23:14 -1000 Subject: [PATCH] [api] Add flatten attribute to batch write functions to avoid regression The noinline attribute on the batch path prevented the compiler from inlining callees like write_plaintext_header and encrypt_noise_message_ into the loop body, causing a 5.6% regression on batch writes. Adding flatten forces all callees to be inlined within the batch function while noinline still keeps its large stack frame separate from the single-message fast path. --- esphome/components/api/api_frame_helper_noise.cpp | 2 +- esphome/components/api/api_frame_helper_plaintext.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/api/api_frame_helper_noise.cpp b/esphome/components/api/api_frame_helper_noise.cpp index 5b60cee143..0781c501e5 100644 --- a/esphome/components/api/api_frame_helper_noise.cpp +++ b/esphome/components/api/api_frame_helper_noise.cpp @@ -489,7 +489,7 @@ APIError APINoiseFrameHelper::encrypt_noise_message_(uint8_t *buf_start, const M } // Outlined multi-message path to keep the single-message fast path's stack frame small. -APIError __attribute__((noinline)) +APIError __attribute__((noinline, flatten)) APINoiseFrameHelper::write_protobuf_messages_batch_(uint8_t *buffer_data, std::span messages) { StaticVector iovs; uint16_t total_write_len = 0; diff --git a/esphome/components/api/api_frame_helper_plaintext.cpp b/esphome/components/api/api_frame_helper_plaintext.cpp index 64c8882afa..9d560a0333 100644 --- a/esphome/components/api/api_frame_helper_plaintext.cpp +++ b/esphome/components/api/api_frame_helper_plaintext.cpp @@ -288,7 +288,7 @@ static inline uint8_t *write_plaintext_header(uint8_t *buf_start, const MessageI // Outlined multi-message path to keep the single-message fast path's stack frame small. // The StaticVector would force a ~300-byte stack frame // even when only sending one message if it were in the same function. -APIError __attribute__((noinline)) +APIError __attribute__((noinline, flatten)) APIPlaintextFrameHelper::write_protobuf_messages_batch_(uint8_t *buffer_data, std::span messages) { StaticVector iovs; uint16_t total_write_len = 0;