Borrow deferred containers instead of copying them - #167
Open
jerrytron wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
json_compilerstored 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 throughcompile()→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.
ctestpasses and 77 real stories compile to byte-identical output.