Skip to content

DLR patch 0106: MathUtils.ToString(BigInteger, radix) in subquadratic time - #187

Merged
Largo merged 1 commit into
masterfrom
claude/issue-89-dlr-bigint-tostring
Nov 10, 2026
Merged

Largo merged 1 commit into
masterfrom
claude/issue-89-dlr-bigint-tostring

Conversation

@Largo

@Largo Largo commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Part of #89 ("The DLR's MathUtils.ToString(BigInteger, radix) is quadratic").

What

New DLR patch Build/dlr-patches/0106-BigInteger-ToString-radix.patch. It replaces MathUtils.ToString(BigInteger, radix). The old version divided the whole number by the radix once per digit, a BigInteger division for every digit. The new one:

  • Power-of-two radix: reads the bits directly, in linear time.
  • Any other radix: divide and conquer. It splits by radix**(k*2**i), where radix**k is the largest power of the radix that fits in a uint. Pieces of 8 or fewer words are divided in place by radix**k (the existing div helper), which gives k digits per pass.
  • On .NET 5–8: BigInteger.DivRem is quadratic there (schoolbook) while multiplication is Karatsuba. So splits of 4096 bits or more use Barrett reduction, with a reciprocal from Newton's method that needs only multiplications. A quotient or reciprocal that is slightly off is corrected exactly, so speed depends on the estimate but correctness does not.
  • On .NET 9 and later: DivRem is Burnikel-Ziegler and was faster than the Barrett path in measurements, so it is used directly. The switch is Environment.Version.Major.

The output is the same as before: upper case digits, a - for negatives, "0", and the same ArgumentOutOfRangeException for a bad radix.

Util/check-dlr-patches.sh globs Build/dlr-patches/*.patch, so it needs no change. It accepts the patched copy and flags a DLR without it ("0106-... is not applied"). The README table has a row for the new patch.

IronRuby callers: since #86, Integer#to_s uses its own ClrBigInteger.ToRadixString and no longer reaches this method. The only IronRuby caller left is IronRuby.Tests (NumberLiteralTests.TestBigInt). The other users are DLR consumers.

Verification

  • Differential test, a C# harness against the built Microsoft.Dynamic.dll, on both net8.0 and net10.0, with 0 failures out of 41,843 checks per TFM:
    • Edge values, all radixes 2–36, against a verbatim copy of the old method: 0, ±1, the Int32/Int64/UInt64 limits, r**e and its neighbours for e up to 700, and 2**b and its neighbours for b up to 2100.
    • Random values against the old method: 6,000 values up to 12k bits and 35 values of 40k–70k bits, both signs, all radixes.
    • Round-trip checks, where the old method is too slow: r**e - 1, r**e and -(r**e + 1) for e from 20,000 to 65,535, plus random values of 100k, 300k and 1M bits in every radix. Each result is parsed back with an independent divide-and-conquer parser, which also rejects leading zeros. Radix 10 is also compared with BigInteger.ToString().
    • Bad radixes (-1, 0, 1, 37, 100) still throw.
  • Timing in ms per call (CPU time, box at load 60–110, so roughly ±30%):
bits radix old (net8 / net10) new net8.0 new net10.0
10k 10 12.5 / 13.2 ~0.65 ~0.5
10k 16 10.0 / 10.2 0.05–0.1 0.05
100k 10 1,050 / 1,052 ~16–28 ~9–14
100k 7 1,270 / 1,258 ~20–50 ~10–17
1M 10 106,000–110,600 (both) ~525–570 ~280–350
1M 7 – ~520–625 ~260–360
1M 2 – 5 6

Without Barrett, net8.0 took 2.7 s (radix 10) and 3.3 s (radix 7) for 1M bits. For comparison, .NET's own BigInteger.ToString() takes 1.8 s on net8.0 and 7.1 s on net10.0 for 1M bits.

  • IronRuby.Tests, Debug, built against a private DLR copy with every patch plus 0106: net8.0 0 FAILED, net10.0 0 FAILED. The run includes a new BigIntegerToString1, which covers radixes 2–36, powers of the radix and their neighbours up to r**4100, random values up to 20k bits, sign handling, upper case, and agreement with BigInteger.ToString() for radix 10.
  • ruby/spec core/integer/to_s_spec and inspect_spec: 11 examples, 0 failures on both net8.0 and net10.0.
  • The patch was generated against /root/workspace/dlr (DLR_REF plus 0001–0105). Applied to that file, it reproduces the tested file byte for byte.

Notes

  • Shared DLR checkout: /root/workspace/dlr needs this patch added as a commit on ironruby-patched after merge. Until then, builds against it fail the check-dlr-patches guard.
  • Pre-existing build failures: the net462 and netstandard2.0 builds of Microsoft.Dynamic already fail from earlier patches (Type.IsCollectible, RuntimeHelpers.TryEnsureSufficientExecutionStack). This patch uses nothing beyond netstandard2.0.
  • Possible follow-up, not done here: IronRuby's own big Integer#to_s is still quadratic. For 3**600000 (~950k bits, Debug build) to_s takes 6–9 s and to_s(7) takes 16 s. ClrBigInteger.ToRadixString could call this method plus ToLowerInvariant() for large values. The crossover is about 1k bits against the chunked radix code, and about 30k bits against BigInteger.ToString() for radix 10.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Speeds up the DLR’s MathUtils.ToString(BigInteger, radix) while keeping its output format.

  • Large integers now turn into radix digits without one division per digit.

Acknowledged scope from Largo: speeding up IronRuby’s separate Integer#to_s is deferred. Existing DLR build failures on net462 and netstandard2.0, and manually updating the shared DLR checkout after merge, are explicitly noted.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Check radix and sign"] --> B{"Zero?"}
  B -->|Yes| Z["Return 0"]
  B -->|No| C{"Power-of-two radix?"}
  C -->|Yes| D["Read digits from bits"]
  C -->|No| E["Split by powers of the radix"]
  E --> F["Convert small pieces in groups"]
  D --> G["Add sign and return"]
  F --> G
Loading

Reviews (1) · Last reviewed commit: "DLR patch 0106: MathUtils.ToString(BigIn..." · Reviewed by Greptile

… time

The DLR's MathUtils.ToString(BigInteger, radix) divided the whole number by
the radix once per digit: a million-bit number took ~108 s. The patch reads the
bits for a power-of-two radix and otherwise splits the number by powers of the
radix (divide and conquer), using Barrett reduction with a Newton reciprocal on
.NET 5-8, whose BigInteger.DivRem is quadratic. Output is unchanged (upper
case, '-' for negatives); a million bits now take ~0.3 s on net10.0 and ~0.55 s
on net8.0.

Integer#to_s has not called it since #86; IronRuby.Tests does, and gets a
test of the split points (BigIntegerToString1).

Part of #89

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

  • Ask an admin to enable usage-based reviews

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Next included review available in 32 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 69 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 80419b0 and 59c3db7.

Files selected for processing: 4

  • Build/dlr-patches/0106-BigInteger-ToString-radix.patch
  • Build/dlr-patches/README.md
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/NumberLiteralTests.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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