Skip to content

feat(ts): multiply wide integers on halves, and join u64 halves in one call - #68

Merged
kixelated merged 7 commits into
mainfrom
quest/m2/ts-wide-fast
Oct 26, 2026
Merged

kixelated merged 7 commits into
mainfrom
quest/m2/ts-wide-fast

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 through bigint (i64, u128, i128, and every checked_mul), so moq-net's Timestamp::convert, a u128 product past 2^53 divided back down, took 14-19x bigint's time. And from_halves, which every 8-byte varint decode calls, went through rt.u64Or(rt.u64Shl(hi, 32), lo).

Approach

  • Products on halves (runtime): mul and checked_mul share one product. At 64 bits it multiplies the factors' 32-bit halves, read without boxing a number, signed by magnitude, for u64 and i64. 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 through bigint.
  • One join (runtime): join(hi, lo) makes a 64-bit value's one form from its halves without allocating when it is a safe integer, replacing one(halves(..)) everywhere. u64Div by a uint32 reads its dividend's halves without boxing the divisor.
  • Fused from_halves (MIR lowering only, as ts-mir is deleting THIR): ((hi as u64) << 32) | lo as u64 of halves of at most 32 bits lowers to rt.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.
  • Benchmark: the codec fixture's bench::run op 9 converts timestamps between timescales as moq-net's Timestamp::convert does, in u128 and again in u64, with moq's bigint way (js/hang's wallClockTime) as the hand-written route (bench/rescale.ts).
  • Tests: the wide sweep adds 128-bit products and checked products of each pair of 64-bit edge values, extended in each type's sign, two edge values whose halves carry, -2^32 ± 1, and from_halves of each edge value (the lang fixture's convert, in a u32 and 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% of translated here.

workload engine bigint (hand-written) main this PR A/A
time/u128/micros-to-90k Node 104 1440 856 878
time/u128/micros-to-90k Bun 75 1430 880 901
time/u128/micros-to-90k Chrome 61 1130 693 674
time/u64/micros-to-90k Node 105 406 143 149
time/u64/micros-to-90k Bun 75 345 160 154
time/u64/micros-to-90k Chrome 62 333 111 114

The 90k-to-micros rows 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 with u64Or(u64Shl(..)) hand-restored, over four runs at load 22-34 (each value is the unfused route's time over the fused one's):

row engine unfused ÷ fused A/A ÷ fused
varint/quic/8B decode Chrome 1.20, 1.10, 1.06, 1.05 1.02, 0.99, 1.00, 1.00
varint/quic/8B decode Node 1.06, 1.07, 0.96, 1.08 1.02, 1.05, 0.99, 1.00
varint/quic/8B decode Bun 1.08, 0.90, 1.05, 0.82 1.03, 1.01, 0.99, 0.78

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:

  1. The fused from_halves:
    • ✅ (a) Keep it: Chrome's 8-byte decode measured 1.05-1.2x faster.
    • (b) Drop it, and the MIR pattern with it.
  2. The follow-up's scope (quest):
    • ✅ (a) Also measure u128 and i128 as native bigint instead of numbers and limbs, on the codec and timestamp workloads, bundle size included. u64 and i64 stay numbers below 2^53.
    • (b) Only take the remaining runtime paths off bigint.

Impact

  • rt.ts: new u64FromHalves(hi, lo). u64CheckedMul and the other three wide checked_muls no longer take a detour through bigint; their results are unchanged.
  • Translator output from --lower mir: ((hi as u64) << 32) | lo as u64 of halves of at most 32 bits is rt.u64FromHalves(hi, lo). THIR output is unchanged.
  • bench::run has op 9, and the report has time/ rows.

Alternatives

  • Dropping the fusion: removes the MIR pattern (about 60 lines) at the cost of Chrome's 8-byte decode, the only engine where it measured.
  • Fusing in num::binary: would change THIR output too, which ts-mir deletes; MIR temporaries are separate statements there anyway.
  • Unboxing every 64-bit operation's operands (add, sub, bitwise): no workload passes 2^53 there, so nothing would measure it.

Follow-ups

  • Wide-integer runtime off bigint (new quest): time/u128 is still 8-12x bigint's time, from 128-bit division and comparison with numbers through bigint, and every checked_add/checked_sub/saturating op goes through bigint, numbers included.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 5 commits October 25, 2026 16:21
Quest-Claim: 8d40da37bd40de3a
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
@kixelated kixelated changed the title Wide-integer fast paths feat(ts): multiply wide integers on halves, and join u64 halves in one call Oct 26, 2026
@kixelated
kixelated marked this pull request as ready for review October 26, 2026 03:25
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T03:34:59.676930Z ffaab04 New commits
🔒 Security Review ✅ Completed 2026-10-26T03:36:36.375721Z ffaab04 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: 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.

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

@kixelated
kixelated merged commit d7bff79 into main Oct 26, 2026
1 check passed
@kixelated kixelated mentioned this pull request Oct 26, 2026
@kixelated
kixelated deleted the quest/m2/ts-wide-fast branch October 27, 2026 20:32
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