Repository navigation
DLR patch 0106: MathUtils.ToString(BigInteger, radix) in subquadratic time - #187
Merged
Merged
Conversation
… 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]>
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
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. View limit details
|
32 of 36 tasks
This was referenced Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 replacesMathUtils.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:radix**(k*2**i), whereradix**kis the largest power of the radix that fits in a uint. Pieces of 8 or fewer words are divided in place byradix**k(the existingdivhelper), which gives k digits per pass.BigInteger.DivRemis 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.DivRemis Burnikel-Ziegler and was faster than the Barrett path in measurements, so it is used directly. The switch isEnvironment.Version.Major.The output is the same as before: upper case digits, a
-for negatives,"0", and the sameArgumentOutOfRangeExceptionfor a bad radix.Util/check-dlr-patches.shglobsBuild/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_suses its ownClrBigInteger.ToRadixStringand no longer reaches this method. The only IronRuby caller left is IronRuby.Tests (NumberLiteralTests.TestBigInt). The other users are DLR consumers.Verification
Microsoft.Dynamic.dll, on both net8.0 and net10.0, with 0 failures out of 41,843 checks per TFM:r**eand its neighbours for e up to 700, and2**band its neighbours for b up to 2100.r**e - 1,r**eand-(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 withBigInteger.ToString().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.BigIntegerToString1, which covers radixes 2–36, powers of the radix and their neighbours up tor**4100, random values up to 20k bits, sign handling, upper case, and agreement withBigInteger.ToString()for radix 10.core/integer/to_s_specandinspect_spec: 11 examples, 0 failures on both net8.0 and net10.0./root/workspace/dlr(DLR_REF plus 0001–0105). Applied to that file, it reproduces the tested file byte for byte.Notes
/root/workspace/dlrneeds this patch added as a commit onironruby-patchedafter merge. Until then, builds against it fail the check-dlr-patches guard.net462andnetstandard2.0builds of Microsoft.Dynamic already fail from earlier patches (Type.IsCollectible,RuntimeHelpers.TryEnsureSufficientExecutionStack). This patch uses nothing beyond netstandard2.0.Integer#to_sis still quadratic. For3**600000(~950k bits, Debug build)to_stakes 6–9 s andto_s(7)takes 16 s.ClrBigInteger.ToRadixStringcould call this method plusToLowerInvariant()for large values. The crossover is about 1k bits against the chunked radix code, and about 30k bits againstBigInteger.ToString()for radix 10.🤖 Generated with Claude Code


Confidence 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.Acknowledged scope from
Largo: speeding up IronRuby’s separateInteger#to_sis deferred. Existing DLR build failures onnet462andnetstandard2.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 --> GReviews (1) · Last reviewed commit: "DLR patch 0106: MathUtils.ToString(BigIn..." · Reviewed by Greptile