std.math.big.int: Improvements to toString
and setString
speed
#24220
+1,866
−595
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.
This PR is ready to be reviewed but depends on functions defined in #23848, so I am marking it as a draft until the other one is merged (hopefully).
This PR massively improve the speed of
toString
andsetString
.For the former, a subquadratic algorithm has been added, and the quadratic one has also been improved.
For the latter, the previous implementation was, uhh, aiming for accuracy rather than speed, to put it nicely, and was therefore very easy to massively speedup.
I have also used
stackFallback
at 2 locations (in astgen and zongen) to reduce the number of allocations when parsing small numbers.Graphs for


toString
In the last graph (the
long
one), the current std implementation oftoString
takes 1m10 forn = 90000
on my machine.For setString, this is the time the implementation in master takes divided by the time the new implementation takes (for a BigInt of

n
limbs). In short, this graph shows how many times faster is the new implementation.Ast-check of
compiler_rt/udivmodti4_test.zig
comparison (master vs new):