Repository navigation
fix(runtime): copy deeply nested terms without native recursion - #25
Merged
Merged
Conversation
A term nested hundreds of thousands of levels deep, such as an iolist built by prepending or a chain of tuples or maps, can now be sent to another process, captured in a spawned fun, stored in ETS or persistent_term and read back, as on BEAM. The message copier and the ETS copies used to recurse on the native stack for list heads and tuple, map and closure elements, so such a term killed the program with a bus error: in ETS from about 5,000 levels, when sent or captured from about 14,000. The message copier and the ETS copies now copy depth first and keep the slots still to copy on a heap-allocated work stack, as the garbage collector does since it stopped recursing. The ETS key encoder and the word count for a stored entry use the same kind of stack. Each work stack reuses its thread's spare buffer, so copying an ordinary term allocates nothing extra. The ETS copies also stop building a temporary vector for each list, tuple or map, which makes inserting nested rows and ets:tab2list/1 faster. The copy_deep_nesting fixture sends, captures and stores 200,000-level terms and looks one up as a key, natively and on BEAM.
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.
Why
A term nested hundreds of thousands of levels deep, such as an iolist built by prepending, crashed the program with a bus error when it was sent, captured in a spawned fun or stored in ETS, while BEAM handles it.
What changed
Before and after
Before: A probe on the old version crashed in all 18 of its 200,000-level cases (send, spawn, ETS lookup, tab2list, ETS key, persistent_term; iolist, tuple, map), and ETS crashed from about 5,000 levels.
After: All 36 probe cases, at 1,000 and 200,000 levels, return the full-depth term, and the new fixture passes natively and on OTP 29.
This is a runtime change with nothing on screen; program output shows the result.
Checked
How it was checked
cargo build --release --locked -p nera-runtime and cargo build --release --locked
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo test --workspace --locked --no-fail-fast: 2,145 passed
python3 scripts/test-allocator-config.py
nera test -j 1 --timeout 30: 480 passed, 2 skipped (need external modules)
Risk
Risk above low: Blast radius, medium — Message, spawn, ETS and persistent_term copies run in every program; Novelty, medium — A new work-stack module beside the existing copiers
Full risk assessment
Not done
Comparing such terms, using one as an ordered_set key, term_to_binary/1, erlang:phash2/1 and iolist_to_binary/1 still recurse and crash at the depths known limits gives.