Repository navigation
fix(ts): fold an inclusive range through std's adaptors as core does - #301
Conversation
Quest-Claim: b14ca40bfe2de79e
A RangeInclusive borrowed by by_ref and folded through map, filter, rev, chain, peekable, take, skip, and the other adaptors whose std's folds their iterator now ends exhausted with start == end, as core's try_fold and try_rfold leave it. IntoIterator::into_iter of a &mut iterator is the reference itself, so for over r.by_ref() type-checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/expected/core/option.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 23 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: eddd6df
No actionable bugs found in the changed runtime, lowering, and regression fixtures. The direction is sound: preserving shared adaptor state while distinguishing next from fold traversal addresses observable range-state differences without replacing the existing iterator model. The explicit non-TrustedLen collection mode and borrowed-iterator lowering fit that approach; the separately documented TrustedLen zip gap is not a new finding here.
Verification limits: GitHub-based static review of the diff and relevant surrounding code only. I did not run the differential fixtures, TypeScript checks, benchmarks, or CI, and have not independently verified the reported local results.
folded() allocated a closure on every call, which filter's next makes once per item, so iter/map-filter-count took twice as long in Bun. Each adaptor now checks its iterator inline, and a flatten reuses one view, which each fold resets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/expected/collections.ts
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 614ff2c
Compared with my review of eddd6df, accounting for the merged main changes. No new actionable bugs found in the substantive update: inline folded-view selection and the reusable, resettable FlattenFold, including its integration with the size-hint collection paths. There were no earlier actionable findings to close.
Overall direction remains sound: avoiding closure allocation on the unchanged-iterator path is a focused simplification, and the cached flatten view resets its transient traversal state while preserving the underlying iterator state.
Verification limits: static GitHub review only; I did not run tests, TypeScript checks, CI, or benchmarks and have not independently verified the performance claims.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* chore(quest): claim quest/m3/ts-zero-sized.md Quest-Claim: 57911bd6ae413a20 * feat(ts): translate zero-sized values MIR never assigns A zero-sized result or local that nothing assigns, the return place included, is made as a constant of it is, and a constant of std's iterators of zero-sized fields, as `from_fn` of a closure capturing nothing makes, is the call that makes it. A VecDeque reserves at the first item it extends by, as std's does for zero-sized elements. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(ts): drop the refusals zero-sized constants made of own iterators Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(ts): update the lang snapshots past #301 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Problem
A
RangeInclusiveborrowed byby_refand read through an adaptor whose std's folds its iterator (map,filter,rev,chain,peekable,take,skip, ...) ended in the statenextleaves (startpastend), where core'stry_fold/try_rfoldleave it exhausted withstart == end. On the new fixture, 141,098 printed states differed from native wasm32 before this change.for _ in r.by_ref()failedtsc(Iter<u8>assigned toRangeInclusive<u8>).Approach
Audited every adaptor in the pinned nightly's
core::iter::adaptersthat forwardsfold/try_fold/rfold/try_rfoldto its iterator:rt.Iter.folded()now steps from the back too: the inclusive range's stepper gainsnextBackas core'stry_rfold, read byrfold,rfind,rposition, andfilter's andfilter_map'snext_back.map/copied/cloned,inspect,enumerate,chain,rev,skip(nth(n - 1)first),take(counting only items read; from the back, trimmed bynth_back),take_while,map_while,fuse,peekable, andflatten/flat_map(its source and each iterator it makes, in std'siter_try_foldorder).filter_map'snext/next_back(viafind_map) and thecollectshunts'next(viatry_for_each) fold their iterator, asfilteralready did.advance_by/advance_back_byfold, as core's sized specializations do (traits/iterator.rs:337,traits/double_ended.rs:216), which also coverstake'scountandnth/nth_backthroughmap. So doesflatten'sadvance_byof its source.zipandstep_byread bynext/nth, as std's do: unchanged.IntoIterator::into_iterof a&mutiterator is the reference itself (only by-value consumers wrap it initerBorrow), soforoverby_ref()of a range, aPeekable, or the crate's own iterator type-checks.collect/extendof a non-TrustedLeniterator now passes"next"even when nothing drops, since std reads it bynextand itsfolded()may now fold. The runtime's collectors read such an iterator bynext, as main's do since feat(ts): translate size_hint and reserve by it in collect and extend #294.Tests: new
folds::foldscase printsstart,end,is_empty(), andDebugafter every read, foru8andi64ranges that are empty, single, full, and atu8::MAX,i64::MIN, andi64::MAX, including partial folds (position,any,find,take,rfind,rposition,breakout offor).iters::ownaddsforoverby_ref()of the crate's own iterator.Choices made without asking:
flatten'sfolded()always returns a view, since whether an iterator it will make folds differently isn't known up front.zipof twoTrustedLeniterators (std'sSpecFoldreads only as many pairs as its size hint) is a separate gap inzip's own fold, not a fold of the range. Recorded as a quest and kept out of this PR.Impact
forover a&mutiterator holds the iterator itself instead ofrt.iterBorrow(...);collect/extendof a non-TrustedLeniterator passes"next".rt/iter.ts): adaptors'folded(), the range stepper'snextBack, andadvanceBy/advanceBackBybehave as above. No exported signature changes, nothing breaking, noMIGRATE.mdentry.Benchmark
I ran
just ts benchwith the base as a second route (--route base=...) against the merge-base1d3a8e0f. Rounds were interleaved and randomized, and an A/A route (a second sample of the head) measured noise. The host was loaded, so each number is the median of 4 runs, in ns per op (translated / A/A / base).On
eddd6df9, the head this was asked on, the bench regressed:The cause was a closure each adaptor's
folded()allocated through a shared helper, even when its iterator folds as it reads. JavaScriptCore paid about 2x for it.5cf8bd95builds the view only where the iterator folds differently and otherwise returns the adaptor itself. After that fix:The median over all workloads changed by -0.2% on Node, +0.5% on Bun, -0.8% on Chromium, and +0.6% on Firefox. The A/A route changed by +0.3%, +0.5%, -0.6%, and +1.9%, so every engine is within noise. On Bun, the translated and A/A routes run the same code yet differ by 35% on map-filter-count, so its gap is noise. Firefox's map-filter-count gap is the route bias: its A/A sample is just as far from the base, and in workloads this PR leaves alone, Firefox's routes differ by up to 20%. The bundle grows from 39531 to 40543 bytes raw, and from 12291 to 12460 gzipped.
The merge of main after
5cf8bd95takes main's collectors unchanged and was not benched again.Alternatives
try_foldmethod per adaptor: fix(ts): drain an inclusive range by a fold as core's try_fold does #179 measured up to 58% slower onjust ts bench, so this stays onfolded()views.iterBorrowfor ranges: would leavePeekableand the crate's own iterators failingtscthe same way.Follow-ups
Review
Codex
gpt-6.1-sol, medium effort, viaquest review, head614ff2cded0a5b665c76fb0b5c6d807ec1940f29.advanceBackByshould read bynext_back" was refuted. On the pinned nightly, core's sizedadvance_back_byistry_rfold(library/core/src/iter/traits/double_ended.rs:216-231), and the reported case natively printsNone 1..=1 (exhausted), as the runtime does. Addednth/nth_backthroughmapto the fixture.Local checks:
just cipassed on 614ff2c.Merge of main at
98d262f3into614ff2cd, as0f1b99a1: took main'sts/fixtures/lang/expected/collections.tsand regenerated it, which adds this PR's fold argument to main's new collects. Recorded skip, too trivial to review;just cipassed on0f1b99a1.(written by Claude Opus 5.5)
🤖 Generated with Claude Code