Skip to content

Borrow deferred containers instead of copying them - #167

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

Borrow deferred containers instead of copying them#167
jerrytron wants to merge 1 commit 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.

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>
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.

1 participant