[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.
This commit is contained in:
J. Nick Koston
2026-02-18 18:07:23 -06:00
parent bb6600985f
commit d967f47f3e
2 changed files with 4 additions and 4 deletions

View File

@@ -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;

View File

@@ -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<size_t STACK_SIZE = 512> class SerializationBuffer {
template<size_t STACK_SIZE = 640> class SerializationBuffer {
public:
static constexpr size_t BUFFER_SIZE = STACK_SIZE; ///< Stack buffer size for this instantiation