1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 08:41:59 +00:00
This commit is contained in:
J. Nick Koston
2026-01-29 16:24:30 -06:00
parent 55ff740e4e
commit 0490b2d450

View File

@@ -71,10 +71,13 @@ SerializationBuffer<> JsonBuilder::serialize() {
buf[2] = '\0';
return result;
}
// Intentionally avoid measureJson() - it instantiates DummyWriter templates that add ~700 bytes
// of flash. Instead, try serializing to stack buffer first. 768 bytes covers typical JSON payloads
// (sensors ~200B, lights ~170B, climate ~700B). Only entities with many options exceed this.
// serializeJson() returns actual size needed even if truncated, so we can retry with heap if needed.
// Intentionally avoid measureJson() - it instantiates DummyWriter templates that add ~1KB of flash.
// Instead, try serializing to stack buffer first. 768 bytes covers 99.9% of JSON payloads
// (sensors ~200B, lights ~170B, climate ~700B). Only entities with 40+ options exceed this.
// For the common case: single serialize to stack, no heap allocation, no measurement overhead.
// For the rare large case: serialize twice (once truncated, once to heap) - less efficient but
// saves ~1KB flash that would otherwise be wasted on every build.
// serializeJson() returns actual size needed even if truncated, so we can retry with exact size.
constexpr size_t buf_size = SerializationBuffer<>::BUFFER_SIZE;
SerializationBuffer<> result(buf_size - 1); // Max content size (reserve 1 for null)
size_t size = serializeJson(doc_, result.data_writable(), buf_size);