Allocation-free for-in key enumeration#2592
Merged
Merged
Conversation
for-in previously allocated per LOOP: an unconditional HashSet visited-set (plus its entry/bucket arrays), nested per-prototype-level C# iterator state machines (ObjectInstance.GetKeys recursion), and an EnumerableIterator wrapper; and per STEP: an IteratorResult object. A dedicated IteratorInstance.ForInIterator now walks own string keys level by level, probing presence/enumerability per key at step time through the allocation-free ProbeOwnProperty (deleted-before-visit keys stay skipped). The shadow set for deeper levels materializes only when a deeper level actually produces a present AND enumerable key: exhausted level snapshots are retained as list references and re-probed once to build the set on demand. Prototype members are overwhelmingly non-enumerable, so common enumerations (plain objects, arrays) allocate no set at all. The step protocol gains IteratorInstance.TryStepValue, which skips the per-step IteratorResult for iterators that can and is byte-for-byte the old TryIteratorStep + Get(value) sequence for every other iterator; the for-in/ for-of sync loop consumes it. ObjectInstance.GetKeys (sole consumer: the for-in head) is removed. fresh-engine probes: for-in over 1000-element arrays 78.2 -> 28.5 MB/iter (-64%), -14% time; handlebars-style extend() enumeration 107 -> 58 MB/iter (-46%), -16% time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
lahma
enabled auto-merge (squash)
July 6, 2026 20:47
This was referenced Jul 13, 2026
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.
for-in previously allocated per loop: an unconditional
HashSetvisited-set (plus its entry/bucket arrays), nested per-prototype-level C# iterator state machines (ObjectInstance.GetKeysrecursion — one state machine and set per level), and anEnumerableIteratorwrapper; and per step: anIteratorResultobject. The allocation census put this machinery at 40-55% of enumeration-heavy workloads.A dedicated
IteratorInstance.ForInIteratornow walks own string keys level by level, probing presence/enumerability per key at step time through the allocation-freeProbeOwnProperty(#2585) — deleted-before-visit keys stay skipped, per-level key snapshots keep proxyownKeys/getOwnPropertyDescriptortrap ordering. The shadow set for deeper levels materializes only when a deeper level actually produces a present and enumerable key: exhausted level snapshots are retained as list references and re-probed once to build the set on demand. Prototype members are overwhelmingly non-enumerable, so common enumerations (plain objects, arrays, handlebars-styleextend) allocate no set at all.The step protocol gains
IteratorInstance.TryStepValue, which skips the per-stepIteratorResultfor iterators that can and is byte-for-byte the oldTryIteratorStep+Get(value)sequence for every other iterator; the for-in/for-of sync loop consumes it.ObjectInstance.GetKeys(sole consumer: the for-in head) is removed.Measurements
Fresh-engine probes (A/B/A/B, alternating binaries):
IteratorResult,HashSetentries/buckets and the<GetKeys>state machines disappear from the allocation profiles; what remains is member-readReferencetraffic and the key snapshots themselves.BenchmarkDotNet default jobs (A/B vs main from separate worktrees): PreparedScriptBenchmark prepared modes -1.3/-1.8% time, -2.2% alloc; ObjectEnumerationBenchmark and DromaeoBenchmark.ObjectArray flat (they use
Object.keys, not for-in); PreparedScript source mode within its observed run-to-run band with allocations down.Verification
Jint.Testsgreen (net10.0 + net472),Jint.Tests.PublicInterfacegreen🤖 Generated with Claude Code