Repository navigation
feat(ts): multiply wide integers on halves, and join u64 halves in one call - #68
Conversation
Quest-Claim: 8d40da37bd40de3a
…e call Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…off bigint Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
# Conflicts: # ts/README.md # ts/bench/main.ts # ts/bench/moq.ts # ts/bench/workloads.ts # ts/fixtures/codec/expected/bench.ts # ts/fixtures/codec/src/bench.rs
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: 7ed9981f9fd21fc09f63663691836a2a6fd2d422 (base 8edff073d24e3ea379f77195b7c2227bc72ed4f2).
No actionable bugs found in the changed runtime arithmetic, MIR half-join lowering, benchmark wiring, or differential cases. No prior published findings to reconcile.
Direction looks sound: sharing the checked/unchecked product implementation avoids divergent overflow logic; the magnitude/sign handling and carry bounds keep the new partial products exact. The MIR fusion conservatively requires single-use temporaries and stable, unmutated narrow locals. Retaining bigint for other 128-bit products is simpler than broadening the limb implementation without measurements. The modest, engine-specific fusion gain is clearly separated from the larger multiplication gain.
Verification limits: static GitHub review only; I did not run the translator, differential sweep, mutation tests, or benchmarks, and have not independently reproduced the reported performance results.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: ffaab043a5fd20ae1db54b89287b26664f34bbfb (base 87e279a85c463e1b6d64a1d4f332aef14cbd5446). Compared with my previous review at 7ed9981f9fd21fc09f63663691836a2a6fd2d422, excluding the incoming base changes: runtime, lowering, benchmarks and tests are unchanged; the substantive update expands quest/m2/ts-wide-bigint.md to evaluate native bigint for 128-bit values.
No actionable bugs found in this update; no prior findings remain open. Direction is good: measuring the simpler native-bigint representation against the hybrid implementation, with bundle size and differential correctness included, avoids committing to more limb complexity before it earns its cost. Keeping the 64-bit representation unchanged bounds that experiment.
Verification limits: static GitHub diff/design review only; no tests or benchmarks run, and performance claims were not independently reproduced.
Finishes quest/m2/ts-wide-fast.md.
Problem
Past 2^53, rust2-ts's wide-integer runtime boxed numbers into limbs before computing on them. A product boxed both factors (
u64), or went throughbigint(i64,u128,i128, and everychecked_mul), so moq-net'sTimestamp::convert, au128product past 2^53 divided back down, took 14-19xbigint's time. Andfrom_halves, which every 8-byte varint decode calls, went throughrt.u64Or(rt.u64Shl(hi, 32), lo).Approach
mulandchecked_mulshare oneproduct. At 64 bits it multiplies the factors' 32-bit halves, read without boxing a number, signed by magnitude, foru64andi64. At 128 bits, two numbers multiply by their halves into four limbs; every partial product stays below 2^53, so each is exact. Other 128-bit products still go throughbigint.join(hi, lo)makes a 64-bit value's one form from its halves without allocating when it is a safe integer, replacingone(halves(..))everywhere.u64Divby a uint32 reads its dividend's halves without boxing the divisor.from_halves(MIR lowering only, as ts-mir is deleting THIR):((hi as u64) << 32) | lo as u64of halves of at most 32 bits lowers tort.u64FromHalves(hi, lo). The shift and casts go unlowered, so the halves are read at the|, which takes temporaries read once of halves nothing assigns again.bench::runop 9 converts timestamps between timescales as moq-net'sTimestamp::convertdoes, inu128and again inu64, with moq'sbigintway (js/hang'swallClockTime) as the hand-written route (bench/rescale.ts).from_halvesof each edge value (the lang fixture'sconvert, in au32and a narrower form). Of 10 hand mutations of the new product and join code, the sweep catches all 10; before the added cases, 3 of the 128-bit ones survived.Numbers
just ts bench --route main=<main's translation>, ns per timestamp, median of 15 rounds. Load average 12-28 on the shared 32-thread host; A/A within 3% oftranslatedhere.The
90k-to-microsrows match. Over the codec's other workloads, main took 1.00x, 1.02x, and 1.01x this PR's time at the geometric mean (A/A 1.01x, 1.01x, 1.00x).Fused
from_halves, MIR translation against the same withu64Or(u64Shl(..))hand-restored, over four runs at load 22-34 (each value is the unfused route's time over the fused one's):So the fusion makes Chrome's 8-byte decode, the slow case #26 named, 1.05-1.2x faster; Node and Bun are within noise. The bench's 8-byte value is 2^53 - 1, a number; past 2^53, where the unfused form boxes twice, a microbenchmark measured the fused call 4x faster.
Bundle: the codec's translation grows 788 B minified (24478 against 23690), 297 B gzipped (7407 against 7110), all of it runtime.
Decisions
Decided by the maintainer on 2026-10-25:
from_halves:u128andi128as nativebigintinstead of numbers and limbs, on the codec and timestamp workloads, bundle size included.u64andi64stay numbers below 2^53.bigint.Impact
rt.ts: newu64FromHalves(hi, lo).u64CheckedMuland the other three widechecked_muls no longer take a detour throughbigint; their results are unchanged.--lower mir:((hi as u64) << 32) | lo as u64of halves of at most 32 bits isrt.u64FromHalves(hi, lo). THIR output is unchanged.bench::runhas op 9, and the report hastime/rows.Alternatives
num::binary: would change THIR output too, which ts-mir deletes; MIR temporaries are separate statements there anyway.Follow-ups
time/u128is still 8-12xbigint's time, from 128-bit division and comparison with numbers throughbigint, and everychecked_add/checked_sub/saturating op goes throughbigint, numbers included.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn