From d967f47f3e5dcace3232d7fe817d4ee492296d4b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Feb 2026 18:07:23 -0600 Subject: [PATCH] [json] Increase SerializationBuffer stack size from 512 to 640 bytes The reduction from 768 to 512 was overly aggressive - the stack overflow was caused by the 1400+ byte multipart parser, not the serialization buffer. 640 bytes covers climate DETAIL_ALL payloads (~520 bytes) on the stack without heap fallback. --- esphome/components/json/json_util.cpp | 6 +++--- esphome/components/json/json_util.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/esphome/components/json/json_util.cpp b/esphome/components/json/json_util.cpp index 8734d2e1e7..6ec4d657a1 100644 --- a/esphome/components/json/json_util.cpp +++ b/esphome/components/json/json_util.cpp @@ -88,15 +88,15 @@ SerializationBuffer<> JsonBuilder::serialize() { // - Test: objdump -d -C firmware.elf | grep "SerializationBuffer.*SerializationBuffer" // Should show only destructor, NOT move constructor // - // Try stack buffer first. 512 bytes covers 99.9% of JSON payloads (sensors ~200B, - // lights ~170B, climate ~700B). Only entities with 40+ options exceed this. + // Try stack buffer first. 640 bytes covers 99.9% of JSON payloads (sensors ~200B, + // lights ~170B, climate ~500-700B). Only entities with 40+ options exceed this. // // IMPORTANT: ArduinoJson's serializeJson() with a bounded buffer returns the actual // bytes written (truncated count), NOT the would-be size like snprintf(). When the // payload exceeds the buffer, the return value equals the buffer capacity. The heap // fallback doubles the buffer size until the payload fits. This avoids instantiating // measureJson()'s DummyWriter templates (~736 bytes flash) at the cost of temporarily - // over-allocating heap (at most 2x) for the rare payloads that exceed 512 bytes. + // over-allocating heap (at most 2x) for the rare payloads that exceed 640 bytes. // // =========================================================================================== constexpr size_t buf_size = SerializationBuffer<>::BUFFER_SIZE; diff --git a/esphome/components/json/json_util.h b/esphome/components/json/json_util.h index 3ea551bc8e..eb18e7a237 100644 --- a/esphome/components/json/json_util.h +++ b/esphome/components/json/json_util.h @@ -19,7 +19,7 @@ namespace json { /// Buffer for JSON serialization that uses stack allocation for small payloads. /// Template parameter STACK_SIZE specifies the stack buffer size (default 512 bytes). /// Supports move semantics for efficient return-by-value. -template class SerializationBuffer { +template class SerializationBuffer { public: static constexpr size_t BUFFER_SIZE = STACK_SIZE; ///< Stack buffer size for this instantiation