Skip to content

fix(ts): fold an inclusive range through std's adaptors as core does - #301

Merged
kixelated merged 9 commits into
mainfrom
quest/m3/ts-range-fold
Oct 30, 2026
Merged

kixelated merged 9 commits into
mainfrom
quest/m3/ts-range-fold

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A RangeInclusive borrowed by by_ref and read through an adaptor whose std's folds its iterator (map, filter, rev, chain, peekable, take, skip, ...) ended in the state next leaves (start past end), where core's try_fold/try_rfold leave it exhausted with start == end. On the new fixture, 141,098 printed states differed from native wasm32 before this change. for _ in r.by_ref() failed tsc (Iter<u8> assigned to RangeInclusive<u8>).

Approach

Audited every adaptor in the pinned nightly's core::iter::adapters that forwards fold/try_fold/rfold/try_rfold to its iterator:

  • rt.Iter.folded() now steps from the back too: the inclusive range's stepper gains nextBack as core's try_rfold, read by rfold, rfind, rposition, and filter's and filter_map's next_back.
  • Each adaptor whose std's folds its iterator returns a view of its iterator folded, sharing its own state, only when that iterator folds differently (else itself, so nothing else pays): map/copied/cloned, inspect, enumerate, chain, rev, skip (nth(n - 1) first), take (counting only items read; from the back, trimmed by nth_back), take_while, map_while, fuse, peekable, and flatten/flat_map (its source and each iterator it makes, in std's iter_try_fold order).
  • filter_map's next/next_back (via find_map) and the collect shunts' next (via try_for_each) fold their iterator, as filter already did.
  • The default advance_by/advance_back_by fold, as core's sized specializations do (traits/iterator.rs:337, traits/double_ended.rs:216), which also covers take's count and nth/nth_back through map. So does flatten's advance_by of its source.
  • zip and step_by read by next/nth, as std's do: unchanged.
  • Translator: IntoIterator::into_iter of a &mut iterator is the reference itself (only by-value consumers wrap it in iterBorrow), so for over by_ref() of a range, a Peekable, or the crate's own iterator type-checks. collect/extend of a non-TrustedLen iterator now passes "next" even when nothing drops, since std reads it by next and its folded() may now fold. The runtime's collectors read such an iterator by next, as main's do since feat(ts): translate size_hint and reserve by it in collect and extend #294.

Tests: new folds::folds case prints start, end, is_empty(), and Debug after every read, for u8 and i64 ranges that are empty, single, full, and at u8::MAX, i64::MIN, and i64::MAX, including partial folds (position, any, find, take, rfind, rposition, break out of for). iters::own adds for over by_ref() of the crate's own iterator.

Choices made without asking:

  • flatten's folded() always returns a view, since whether an iterator it will make folds differently isn't known up front.
  • zip of two TrustedLen iterators (std's SpecFold reads only as many pairs as its size hint) is a separate gap in zip's own fold, not a fold of the range. Recorded as a quest and kept out of this PR.

Impact

  • Translator output: for over a &mut iterator holds the iterator itself instead of rt.iterBorrow(...); collect/extend of a non-TrustedLen iterator passes "next".
  • Runtime (rt/iter.ts): adaptors' folded(), the range stepper's nextBack, and advanceBy/advanceBackBy behave as above. No exported signature changes, nothing breaking, no MIGRATE.md entry.
  • Consumer: moq translates through this runtime. The codec bench's numbers are below.

Benchmark

I ran just ts bench with the base as a second route (--route base=...) against the merge-base 1d3a8e0f. 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:

map-filter-count translated A/A base
Bun 25.1 21.8 10.7 (+133%)
Firefox 34.4 33.8 27.2 (+26%)
Node +2.6%

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. 5cf8bd95 builds the view only where the iterator folds differently and otherwise returns the adaptor itself. After that fix:

map-filter-count translated A/A base
Node 16.4 17.1 17.1
Bun 16.8 12.4 12.9
Chromium 15.1 14.2 14.1
Firefox 32.0 32.5 28.1

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 5cf8bd95 takes main's collectors unchanged and was not benched again.

Alternatives

Follow-ups

Review

Codex gpt-6.1-sol, medium effort, via quest review, head 614ff2cded0a5b665c76fb0b5c6d807ec1940f29.

  • Round 1 (head e3dfba7), 1 finding: "default advanceBackBy should read by next_back" was refuted. On the pinned nightly, core's sized advance_back_by is try_rfold (library/core/src/iter/traits/double_ended.rs:216-231), and the reported case natively prints None 1..=1 (exhausted), as the runtime does. Added nth/nth_back through map to the fixture.
  • Round 2 (head eddd6df): no findings.
  • Round 3 (head 614ff2c, after the perf fix and the merge of main): no findings.

Local checks: just ci passed on 614ff2c.

Merge of main at 98d262f3 into 614ff2cd, as 0f1b99a1: took main's ts/fixtures/lang/expected/collections.ts and regenerated it, which adds this PR's fold argument to main's new collects. Recorded skip, too trivial to review; just ci passed on 0f1b99a1.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 4 commits October 30, 2026 11:55
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>
@kixelated kixelated changed the title An inclusive range folded through an adaptor fix(ts): fold an inclusive range through std's adaptors as core does Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 19:55
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 555d44d3-e095-4991-a6d3-9e321f0b4314
Commits

Reviewing files that changed from the base of the PR and between 98d262f and 0f1b99a.

Files selected for processing: 26
  • quest/m3/README.md
  • quest/m3/ts-range-fold.md
  • quest/m3/ts-zip-fold.md
  • ts/fixtures/codec/expected/bench.ts
  • ts/fixtures/lang/expected/bounds.ts
  • ts/fixtures/lang/expected/chars.ts
  • ts/fixtures/lang/expected/collections.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/core/result.ts
  • ts/fixtures/lang/expected/errors.ts
  • ts/fixtures/lang/expected/folds.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/hints.ts
  • ts/fixtures/lang/expected/iters.ts
  • ts/fixtures/lang/expected/keys.ts
  • ts/fixtures/lang/expected/nested.ts
  • ts/fixtures/lang/expected/objects.ts
  • ts/fixtures/lang/expected/orders.ts
  • ts/fixtures/lang/expected/refs.ts
  • ts/fixtures/lang/expected/strs.ts
  • ts/fixtures/lang/src/folds.rs
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/iters.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/runtime/rt/iter.ts
  • ts/src/lower/iter.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

kixelated and others added 4 commits October 30, 2026 13:38
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 kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@kixelated
kixelated merged commit a019564 into main Oct 30, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-range-fold branch October 30, 2026 21:42
kixelated added a commit that referenced this pull request Oct 30, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated added a commit that referenced this pull request Oct 30, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant