From 937715c91c35ee9b4e35c05f7fd0f93cb2209544 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 Feb 2026 07:09:21 -0600 Subject: [PATCH] [api] Split process_batch_ to reduce stack on single-message hot path Clamp buffer pre-allocation to MAX_BATCH_PACKET_SIZE. --- esphome/components/api/api_connection.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 0881128b74..136c61e3cf 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1923,6 +1923,10 @@ void APIConnection::process_batch_() { for (size_t i = 0; i < num_items; i++) { total_estimated_size += this->deferred_batch_[i].estimated_size; } + // Clamp to MAX_BATCH_PACKET_SIZE — we won't send more than that per batch + if (total_estimated_size > MAX_BATCH_PACKET_SIZE) { + total_estimated_size = MAX_BATCH_PACKET_SIZE; + } this->prepare_first_message_buffer(shared_buf, header_padding, total_estimated_size);