Skip to content

fix(ts): drain an inclusive range by a fold as core's try_fold does - #179

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

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

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Core's RangeInclusive::next steps start past end (wrapping only at the type's end), but its try_fold and try_rfold stop start at end and set exhausted. The runtime folded every range through next, so a range drained by any, find, for_each, collect, and the other consumers std folds ended with start == end + 1 and end_bound() == Included(end), where native Rust reads start == end and Excluded(start).

Approach

  • rt.Iter gains folded(): what std's try_fold reads it by. It is the iterator itself, but an inclusive range returns a RangeInclusiveFold stepping it as core's try_fold does, and a &mut borrow (by_ref) returns its iterator's.
  • Each consumer std folds reads through folded(): count, last, any, all, find, find_map, position, fold, for_each, sum, product, max/min (after the first item, which std's reduce reads by next), and collect/extend into Vec, Vec<u8>, VecDeque, HashMap/HashSet, and BTreeMap/BTreeSet.
  • Differential test: wrappers::wrappers drains lo..=hi and u64::MAX - n..=u64::MAX by each consumer, directly and through by_ref, and writes their bounds, start, and is_empty. Without the fix it fails on every case.

Impact

  • Translator output: a folded RangeInclusive now matches core's state. No API change.
  • rt.Iter.folded() is new runtime surface that translated code doesn't call directly.

Performance

A first cut called a foldNext() method per item, defaulting to next(). On just ts bench, set against main's runtime as a --route, it cost up to +15% on V8 (iter/filter-collect) and +58% on Bun (iter/map-filter-count). folded() costs one call per consumer call and nothing per item. Results for folded() are below.

ns/item, this PR / main (A/A noise in parentheses):

workload Node Bun Chromium Firefox
iter/map-filter-count 12.9 / 12.9 (13) 6.13 / 6.13 (6.2) 9.75 / 9.68 (9.64) 19.1 / 19.1 (19.5)
iter/any 2.58 / 2.59 (2.65) 0.81 / 0.83 (0.818) 1.34 / 1.37 (1.33) 4.83 / 5.01 (5.01)
iter/filter-collect 9.19 / 9.11 (9.05) 9.38 / 10 (10.2) 7.72 / 7.85 (7.75) 16.4 / 15.9 (16.2)
iter/sum 11.6 / 11.3 (11.7) 9.55 / 9.91 (9.21) 8.67 / 8.79 (9.06) 16.6 / 16.4 (16.8)
iter/skip-take-fold 3.77 / 3.75 (3.75) 2.91 / 2.97 (2.96) 3.53 / 3.38 (3.52) 6.9 / 6.75 (6.9)
iter/enumerate-for 7.59 / 7.62 (8.45) 5.04 / 5.29 (5.43) 3.97 / 3.89 (3.91) 11.8 / 11.8 (12)

Bundle: +94 B raw, +41 B gzip.

Merge with #172

#172 gave each iterator kind a fold, which consumers take only where the iterator drops. Neither a range nor a &mut borrow drops, so the cases this PR covers stay on the next fast paths, which now read through folded(). #172's fold paths are unchanged. Giving RangeInclusive its own fold belongs to the rewritten quest. Since the merge changed no hot path beyond the one folded() call per consumer measured above, I didn't re-run the bench.

Alternatives

  • A per-item foldNext() on every iterator: the simplest change, but the bench above showed the per-item cost.
  • A "folding" flag that consumers set on the range and clear in finally: it has the same per-adaptor work as folded(), plus mode state that a panic or a nested consumer has to unwind.

Decisions made without asking

  • Scope: consumers that read the range directly or through by_ref. Adaptors whose std try_fold forwards to the range (map, filter, rev, chain, skip, peekable, ...) still step it by next. The quest is rewritten to that remainder rather than closed.
  • Debug of a RangeInclusive (the quest Goal named it) has no translation yet, so nothing here proves it. Bounds, start, and is_empty stand in.

Follow-ups

  • /quest/m3/ts-range-fold.md: forward the fold through each adaptor, add try_rfold's back step for rev, and read collect/extend by next for non-TrustedLen iterators.
  • Translate Debug of the std ranges.

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 6 commits October 28, 2026 07:05
Quest-Claim: 50bd7f67f489b632
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… for its own step

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title An inclusive range drained by a fold fix(ts): drain an inclusive range by a fold as core's try_fold does Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 14:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T15:07:21.551829Z 4c9bd75 New commits
🔒 Security Review ✅ Completed 2026-10-28T15:08:31.428739Z 4c9bd75 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: a8b4f1a

No actionable new bugs found in the changed code. The per-consumer folded() view is a reasonable, contained alternative to persistent folding-mode state: it shares the range's bounds/exhaustion, preserves short-circuit advancement, and leaves iterator cleanup attached to the original iterator. The collection consumers and borrowed-iterator forwarding are consistent with that approach.

The explicitly deferred adaptor/reverse-fold and non-TrustedLen collection semantics remain follow-up work, not newly introduced findings. Verification: static inspection of the runtime, generated fixture changes, and relevant core range-fold implementation only; I did not run tests or independently reproduce the performance results.

# Conflicts:
#	ts/runtime/rt/collections.ts
#	ts/runtime/rt/iter.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: e3d1b66

Compared with my review of a8b4f1a, excluding upstream #172 changes except their integration with this fix.

[P2] Preserve folded range stepping when the accumulator drops — ts/runtime/rt/iter.ts:570–577. iterFold now uses folded() only when dropAcc is undefined. For let mut r = 1u8..=2; let v = r.by_ref().fold(Vec::new(), |mut v, x| { v.push(x); v });, the translator supplies the Vec accumulator dropper, so this calls inherited Borrowed.fold → foldBy → ordinary next(). The range consequently ends at start 3 with an included end, rather than start 2/exhausted as native folding does. The previous reviewed implementation used the folded view even with dropAcc, so this is a merge regression, not the deferred adaptor gap. Preserve the accumulator-unwind cleanup while reading the folded view, or add equivalent range/borrowed fold support; add a differential case with a dropping accumulator and inspect the range afterward.

Overall direction remains reasonable, but the two fold paths must agree on this direct/by_ref case. Verification: static inspection of the merge, lowering and runtime paths; no tests or benchmarks run.

A fold whose accumulator drops takes the iterator's own fold, whose default read by next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Fixed the [P2] from review 5684773633 in 4c9bd75. foldBy, the default fold, now reads forward items through folded(), so Borrowed's inherited fold steps the range as core's try_fold does. A by_ref().fold with a dropping accumulator (Tally, in wrappers::wrappers) failed the differential before the fix and passes now. The same change covers #172's adaptor folds that reach the default fold, which the quest Plan now notes.

(Written by Claude Opus 5.5)

@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: 4c9bd75

The P2 from my previous review is fixed: forward foldBy now reads through folded(), including a borrowed inclusive range with a dropping accumulator, while keeping cleanup on the original iterator. The new Tally differential fixture exercises that path and observes the range afterward; its generated call includes the accumulator dropper.

No new actionable bugs found in this update. Reusing the existing folded view is a small, coherent fix without a second range-specific cleanup implementation. Previously documented reverse/adaptor limitations remain follow-up work. Verification: static review of the fix and regression fixture; I did not run tests or benchmarks.

@kixelated
kixelated merged commit 13cea2b into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-range-fold branch October 28, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant