perf(stdlib): drop per-call Vec alloc for List/String/Deque indexing 📇#160
Merged
Conversation
The `[]` native was uniformly `NativeFunc::WithVm`, forcing the VM to `stack.drain(args..).collect::<Vec<Value>>()` before every call so it could pass `&mut Vm`. Only the Map default-function path actually needs the VM — most indexes don't. Split off a `NativeFunc::Simple` variant for the type-specific overloads (List/String/Deque). The Map and Any/Any overloads stay on WithVm because Maps can reach the callback path. With specificity-aware overload resolution (c12ee66), typed list lookups dispatch to the Simple overload directly. Profile delta on a memoized-DFS workload (AoC 2022 day 16): - `Vec::from_iter` overhead: 4.46% → 1.81% - Wall time: 15.77s → 15.33s (1.03×) Co-Authored-By: Claude Opus 4.7 (1M context) <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.
Summary
[]indexing native was uniformly registered withNativeFunc::WithVm, which forces the VM tostack.drain(args..).collect::<Vec<Value>>()on every call so it can pass&mut Vminto the callback.&mut Vm(it can call user code when a key is missing). List/String/Deque indexes don't touch the VM at all.make_get_func_simple(NativeFunc::Simple, zero-copy slice arg) for the type-specific overloads. Map and Any/Any stay on WithVm because a Map can still reach the callback path through either.Benchmarks
Real-world memoized DFS (AoC 2022 day 16, 5 runs, 2 warmup):
Profile delta (perf record, cpu_core cycles):
Vec::from_iterrun_to_depthdispatch_call_with_memoTotal cycles drop ~4% (59.9B → 57.5B).
The
[]_simpleand original[]show up as ~1.3% / ~1.2% each in the new profile — same total time spent in the native body, but the surrounding allocation overhead is gone for the List path.Test plan
cargo test— all functional tests pass, including the 27 in007_map_and_set(covers Map default-value and default-function paths)cargo fmt,cargo clippy -p ndc_stdlib— no new warnings🤖 Generated with Claude Code