Skip to content

Borrow deferred containers instead of copying them - #167

Open
jerrytron wants to merge 2 commits into
JBenda:masterfrom
choosatron:fix/defer-entry-no-copy
Open

Borrow deferred containers instead of copying them#167
jerrytron wants to merge 2 commits into
JBenda:masterfrom
choosatron:fix/defer-entry-no-copy

Conversation

@jerrytron

Copy link
Copy Markdown

json_compiler stored each deferred named child by value (std::tuple<json, std::string>), so a container nested d levels deep was copied d times over: compile_container() recursed into the copy and deferred its children the same way.

The source document outlives the whole compile — it is the caller's json, reached by reference through compile()compile_container()handle_container_metadata() — so a pointer is safe and the copy was never serving a purpose.

Measured

On an ESP32-S3, peak heap for a compile fell from 16.4x the source JSON's size to 12.05x. On a memory-constrained target that was most of what made compiling on-device impractical; it matters less on a desktop, but it is pure overhead everywhere.

ctest passes and 77 real stories compile to byte-identical output.

Jerry Belich and others added 2 commits September 10, 2026 15:00
json_compiler stored each deferred named child by value, so a container
nested d levels deep was copied d times over: compile_container()
recursed into the copy and deferred its children the same way. On a
memory-constrained target that dominated the cost of a compile - it was
most of why one needed roughly 16x the source JSON in heap.

The source document outlives the whole compile. It is the caller's json,
reached by reference through compile() -> compile_container() ->
handle_container_metadata(), so a pointer is safe and the copy was never
serving a purpose.

Measured on an ESP32-S3 across a range of story sizes, peak heap fell
from 16.4x the source size to 12.05x.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jerrytron

Copy link
Copy Markdown
Author

Pulled in — the branch now points at your 3d5c7a7.

Verified the reference change is output-neutral: built the compiler at bb08955 and at 3d5c7a7 and compiled 70 real stories with each — byte-identical .cdam in every case. ctest passes.

One note in case it saves you a puzzle later: comparing against our own vendored build showed 68 of those 70 differing, which turned out to be the unstable sort in container_hash_t (the subject of #168), not anything here. The differing bytes are pairs swapping places in the container hash table. Independent confirmation that #168 is worth having.

@jerrytron
jerrytron force-pushed the fix/defer-entry-no-copy branch from 155025a to 3d5c7a7 Compare September 10, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants