Repository navigation
fix(ts): drain an inclusive range by a fold as core's try_fold does - #179
Conversation
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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>
|
Fixed the [P2] from review 5684773633 in 4c9bd75. (Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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.
Problem
Core's
RangeInclusive::nextstepsstartpastend(wrapping only at the type's end), but itstry_foldandtry_rfoldstopstartatendand setexhausted. The runtime folded every range throughnext, so a range drained byany,find,for_each,collect, and the other consumers std folds ended withstart == end + 1andend_bound() == Included(end), where native Rust readsstart == endandExcluded(start).Approach
rt.Itergainsfolded(): what std'stry_foldreads it by. It is the iterator itself, but an inclusive range returns aRangeInclusiveFoldstepping it as core'stry_folddoes, and a&mutborrow (by_ref) returns its iterator's.folded():count,last,any,all,find,find_map,position,fold,for_each,sum,product,max/min(after the first item, which std'sreducereads bynext), andcollect/extendintoVec,Vec<u8>,VecDeque,HashMap/HashSet, andBTreeMap/BTreeSet.wrappers::wrappersdrainslo..=hiandu64::MAX - n..=u64::MAXby each consumer, directly and throughby_ref, and writes their bounds,start, andis_empty. Without the fix it fails on every case.Impact
RangeInclusivenow 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 tonext(). Onjust 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 forfolded()are below.ns/item, this PR / main (A/A noise in parentheses):
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&mutborrow drops, so the cases this PR covers stay on thenextfast paths, which now read throughfolded(). #172'sfoldpaths are unchanged. GivingRangeInclusiveits ownfoldbelongs to the rewritten quest. Since the merge changed no hot path beyond the onefolded()call per consumer measured above, I didn't re-run the bench.Alternatives
foldNext()on every iterator: the simplest change, but the bench above showed the per-item cost.finally: it has the same per-adaptor work asfolded(), plus mode state that a panic or a nested consumer has to unwind.Decisions made without asking
by_ref. Adaptors whose stdtry_foldforwards to the range (map,filter,rev,chain,skip,peekable, ...) still step it bynext. The quest is rewritten to that remainder rather than closed.Debugof aRangeInclusive(the quest Goal named it) has no translation yet, so nothing here proves it. Bounds,start, andis_emptystand in.Follow-ups
try_rfold's back step forrev, and readcollect/extendbynextfor non-TrustedLeniterators.Debugof the std ranges.(Written by Claude Opus 5.5)
🤖 Generated with Claude Code