Skip to content

perf(bigint): add recursive division and decimal conversion - #4201

Merged
bobzhang merged 6 commits into
mainfrom
perf/bigint-recursive-division-decimal
Sep 7, 2026
Merged

perf(bigint): add recursive division and decimal conversion#4201
bobzhang merged 6 commits into
mainfrom
perf/bigint-recursive-division-decimal

Conversation

@Yu-zh

@Yu-zh Yu-zh commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • use recursive wide-digit division for native/wasm BigInts only when the divisor is at least 64 limbs and the quotient needs more than two Knuth passes, retaining single-limb and Knuth division for smaller or narrow-quotient operands
  • render large decimal values with recursively squared powers of ten, using iterative division by 10^19 only at the leaves
  • keep recursive division's work in one mutable limb buffer so highly unbalanced operands copy only bounded high windows
  • compute decimal sizing and split thresholds without Int overflow, and retain boxed decimal-divisor records for stable native-backend compatibility
  • add threshold, boundary, round-trip, unbalanced-operand, and benchmark coverage

There are no public API changes.

Performance

Native release benchmarks at 250,000 digits:

operation previous path recursive path speedup
balanced division 237.09 ms 27.16 ms 8.7x
decimal conversion 362.88 ms 21.44 ms 16.9x

The added 65,536-by-64-limb unbalanced benchmark improves from 29.83 ms with whole-prefix rebuilding to 5.35 ms with the mutable work buffer (5.6x). Its scaling is now linear in the number of quotient blocks rather than quadratic copying of the dividend prefix.

The quotient-width guard restores pidigits at 8,000 digits from 1,237.4 ± 0.8 ms to 795.2 ± 1.9 ms, matching the 795.4 ± 1.0 ms pre-feature baseline. The added 9-by-64-limb narrow-quotient benchmark improves from 489.8 ns on recursive division to 334.6 ns on Knuth D, while balanced and very-wide-quotient results remain unchanged.

The 250,001-digit edigits result was also compared byte-for-byte with Go; both produced SHA-256 2e66b93581445e19afc0d73ca280ba6c68b3637c81b27283acb0da8715b65582.

Validation

  • moon check --deny-warn --target all --warn-list +73
  • moon test bigint --target all (wasm 165/165, wasm-gc 192/192, JS 166/166, native 165/165)
  • moon test --target native (7,420/7,420)
  • moon info --target wasm,wasm-gc,js,native (no .mbti changes)
  • moon fmt

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 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-09-04T10:44:44.721612Z 8e2275a 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.

@coveralls

coveralls commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 6561

Coverage increased (+0.04%) to 89.244%

Details

  • Coverage increased (+0.04%) from the base build.
  • Patch coverage: 5 uncovered changes across 2 files (113 of 118 lines covered, 95.76%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
bigint/division_wide.mbt 63 59 93.65%
bigint/decimal_wide.mbt 53 52 98.11%
Total (3 files) 118 113 95.76%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 18287
Covered Lines: 16320
Line Coverage: 89.24%
Coverage Strength: 274564.13 hits per line

💛 - Coveralls

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d2cf74b59

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread bigint/division_wide.mbt Outdated
@bobzhang
bobzhang force-pushed the perf/bigint-recursive-division-decimal branch from 4d2cf74 to 2fb26a9 Compare September 4, 2026 02:21

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7792079b1e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread bigint/decimal_wide.mbt Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cef4096652

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread bigint/decimal_wide.mbt Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 898cfc64da

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread bigint/decimal_wide.mbt Outdated
Use half-divisor wide digits above 64 limbs so large division inherits Karatsuba's subquadratic behavior, while retaining single-limb and Knuth division for smaller operands. Render large decimal values recursively with squared powers of ten and keep iterative 10^19 conversion at the leaves.

Add threshold, boundary, and large benchmark coverage for both paths.
Clone the dividend limbs once and move a logical high-water mark down as quotient blocks are processed. This avoids rebuilding and copying the full unprocessed prefix at every recursive step.

Keep DecimalDivisor boxed because stable native backends cannot flatten its value-typed BigInt field, and add unbalanced correctness and benchmark coverage.
Split the fixed-point multiplication into quotient and remainder terms so the StringBuilder capacity estimate remains in range for every nonnegative Int bit length.

Cover the previous overflow boundary and Int::max_value against an Int64 oracle on the wide backends.
Compute the ceiling half as bits / 2 + bits % 2 so Int::max_value does not wrap before decimal divisor-table construction. Extend the decimal sizing test to cover both fixed-point estimates against Int64 oracles.
@bobzhang
bobzhang force-pushed the perf/bigint-recursive-division-decimal branch from 8e2275a to 4a57fd0 Compare September 7, 2026 06:19
@bobzhang
bobzhang enabled auto-merge (rebase) September 7, 2026 06:19
@bobzhang
bobzhang merged commit 2914f44 into main Sep 7, 2026
20 checks passed
@bobzhang
bobzhang deleted the perf/bigint-recursive-division-decimal branch September 7, 2026 06:39
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.

3 participants