Skip to content

Commit 36862f6

Browse files
1technophileclaude
andcommitted
[MEM] Heap-allocate createDiscovery JsonDocuments
createDiscovery sits at the bottom of the discovery call chain (loop → launchBTDiscovery → <device>Discovery → createDiscoveryFromList (9-col, VLA) → createDiscoveryFromList(13-col) → here). Keeping 2×1024 B StaticJsonDocuments on the stack burned loopTask headroom down to ~100 B free under a full BLE + WebUI load. Moving the two buffers to the heap drops that 2 KB off the stack peak. Measured over the full test_perf + test_webui suite on esp32dev-ble-hil-fastsys (dev HEAD): freestck_min_bytes: 292 → 2324 B (+2032, target was ≥1500) minmem_soak_bytes: 18200 → 20652 B (+2452) all 17 tests still pass The docs are function-scoped, so net heap usage is unchanged long-term (free-on-return), and the soak heap floor actually improved. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 079c987 commit 36862f6

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

main/mqttDiscovery.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,13 @@ void createDiscovery(const char* sensor_type,
451451
const char* device_name, const char* device_manufacturer, const char* device_model, const char* device_id, bool retainCmd,
452452
const char* state_class, const char* state_off, const char* state_on, const char* enum_options,
453453
const char* command_template, bool diagnostic_entity) {
454-
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
454+
// Heap-allocate the two JsonDocuments: createDiscovery sits at the bottom of
455+
// a deep call chain (loop → launchBTDiscovery → <device>Discovery →
456+
// createDiscoveryFromList(9-col, VLA) → createDiscoveryFromList(13-col) →
457+
// here). Keeping 2×1024 B on the stack burned loopTask's stack headroom
458+
// down to ~100 B free under full perf load. Heap-backed docs drop that
459+
// 2 KB off the peak.
460+
DynamicJsonDocument jsonBuffer(JSON_MSG_BUFFER);
455461
JsonObject sensor = jsonBuffer.to<JsonObject>();
456462

457463
// If a component cannot render it's state (f.i. KAKU relays) no state topic
@@ -608,7 +614,7 @@ void createDiscovery(const char* sensor_type,
608614
sensor["ops"] = enum_options; // options
609615
}
610616

611-
StaticJsonDocument<JSON_MSG_BUFFER> jsonDeviceBuffer;
617+
DynamicJsonDocument jsonDeviceBuffer(JSON_MSG_BUFFER);
612618
JsonObject device = jsonDeviceBuffer.to<JsonObject>();
613619
JsonArray identifiers = device.createNestedArray("ids");
614620

0 commit comments

Comments
 (0)