Repository navigation
feat(ts): translate size_hint and reserve by it in collect and extend - #294
Conversation
Quest-Claim: 7e3523738fe306f9
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/caller.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/caller.ts # ts/fixtures/lang/expected/core/option.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 7f22c32
- P2, existing finding still present:
ts/runtime/rt/iter.ts:416–417,439–441treats every non-ZST destination as full. This confirms the local-review finding already described in the PR’s Review/Decision sections, rather than a new independent issue: aftertruncate/popor a reservation leaves spare capacity, extending with a non-TrustedLen iterator whose lower bound exceeds the layout limit can panic on the first yielded item, whereas Rust uses the spare slots and can run further iterator side effects (including the documentedconsumedpanic). Recording this in std-rest does not remove the observable divergence. Preserve actual capacity for this decision, or explicitly retain this correctness limitation for maintainer acceptance and add its differential regression alongside the full-destination cases.
No additional actionable bugs found in the reviewed hint propagation, reserve plumbing, or new fixtures. Overall, one sizeHint() API and shared growth helpers are a sensible direction; the differential matrix is valuable, but the stated capacity approximation remains a qualification on parity.
Verification: static review only. I did not run builds, tests, mutation checks, or benchmarks; the PR’s reported results were not independently verified.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review.
|
Problem
Finishes quest/m3/ts-size-hints.md.
Iterator::size_hintof any std iterator refused at its span ("unsupported call to<... as Iterator>::size_hint"; 194 refusals in the new fixture on the base). And no collection reserved by the hint, so where std'scollectorextendpanics withcapacity overflowbefore reading an item, the translation read items instead (1,692 of the new reserve cases differed from native on the base).Approach
rt.Iter.lower()becomessizeHint(): [lower, upper | undefined], std's for every runtime iterator:chainandflattenadd with a checked upper that overflows toNone,ziptakes the shorter side's,flattencounts[T; N]items and specializes on std'sOneShotiterators (Once,Option,[T; 0 | 1]), ranges are exact past 2^53 andusize::MAX, and the rest (Peekable,Fuse,StepBy,TakeWhile, the?shunt,Chars, B-tree ranges, ...) as std gives them.size_hintmaps to it.TrustedLen:Vec,VecDeque, andBinaryHeapreserve the upper bound up front for aTrustedLeniterator (Nonepanics), else the lower bound plus one at the first item and whenever full, growing asRawVecdoes. B-tree collects go through the sameVec.HashMapandHashSetreserve the lower bound (half of it once non-empty) against hashbrown's largest table for that layout, computed by the translator, and panic withHash table capacity overflow.ResultorOptioncollect is never trusted, as std'sGenericShuntisn't.extendstarts from is taken as full. That matches std afterVec::new,vec!, or aTrustedLencollect. The quest now notes where it doesn't.size_hintstays refused with its span (reject fixture,fixtures/reject/src/lib.rs:203).Proven by
hints.rsin the lang fixture, differential against native wasm32:hints::hints(24,576 cases): every adaptor's lower and upper bound before and after reading from either end, at lengths 0, 1, 5,isize::MAXand past it,usize::MAXand past it, and 2^60.hints::reserves(6,500 cases): 13 collect/extend forms across 5 element layouts, one past each collection's threshold, atu32::MAX, and with an unbounded hint. Each collection reaches its overflow panic, and none allocates natively.Mutation checks against the generated runtime are each caught: Chain's checked upper (1,078 differences),
RawVec's doubling (93), the+ 1on the lower bound (279), hashbrown's half reserve (50), and the minimum non-zero capacity (48).The collect loop measures the same or faster than the base runtime in Node (ns/item, median of 15): filter to
Vec<u8>6.41 vs 6.45, filter toVec<u32>11.63 vs 10.48, map toVec<u32>12.66 vs 8.20.Impact
rt.Iter.lower()is replaced bysizeHint().rt.iterToArray,rt.iterToByteVec,rt.iterExtend,rt.iterExtendBytes,rt.dequeExtend,rt.heapExtend,rt.btreeExtend,rt.btreeExtendKeys,rt.hashExtend,rt.hashExtendKeys,rt.iterFlatMap, andrt.iterFlattentake trailing size or trust arguments. The runtime ships with each translation, so there is noMIGRATE.mdentry.size_hintit reaches translates.Alternatives
lower()beside a newupper(): two calls per hint for every adaptor, and a parallel mechanism to feat(ts): translate collect::<String>, String's Extend, and String::drain #284's.Follow-ups
rt.vecCapacity's "full" with the real capacity.return _0thattscrejects, andfrom_fnof a capture-less closure refuses. Both kept zero-sized elements and[T; 0]flattens out of the fixture.[u32; N]keys failtsc.Zip::next_backof random-access sides trims item by item, where std's is O(1).Decision
An extend after
truncate,clear,pop,drain,with_capacity, orreserve. Decided by the orchestrator on 2026-10-30, pending maintainer review: keep "taken as full" (option a). Exact capacity, which fixes this, is already planned inm2/ts-model/std-rest, and this PR records the gap in that quest's plan.The runtime keeps no capacity, so these extends diverge from std. The divergence only shows for iterators whose lower bound is past
isize::MAX / size_of::<T>(), where std uses its spare room first. This PR removes 1,692 existing divergences and leaves only this one. The rejected options were modeling capacity here (std-rest's whole goal) and reserving only for collects andTrustedLenextends (which drops std's panic for a full collection).Review
Local
quest review(codex gpt-6.1-sol, medium), 2 rounds, last on head7f22c32969a0c1e782c22459d2d8b54dd09ba276:355fa842: one finding (medium,ts/runtime/rt/iter.ts:417). Afterv.truncate(1),extendtreats theVecas full and panics withcapacity overflowon an iterator whose lower bound is pastisize::MAX. std's uses the spare capacity first, so its closure runs once more and panics withconsumed. I refuted it as out of scope: the brief leavesVeccapacity tom2/ts-model/std-rest, and this PR records the gap in that quest's plan.7f22c329: the reviewer kept the finding. The orchestrator settled it as recorded under Decision; no other findings.just cipassed on the reviewed head7f22c32969a0c1e782c22459d2d8b54dd09ba276.Merge of main at
1d3a8e0f(#297, #299) into7f22c329, as46124952: ordered main'siters::peeked_othersbeforehints::*inharness.rsandcaller.ts, took main's generated snapshots and regenerated them (this PR's new instances only). Recorded skip, too trivial to review;just cipassed on46124952.🤖 Generated with Claude Code
(written by Claude Opus 5.5)