-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize BigInteger.ToString
for large decimal string
#112178
base: main
Are you sure you want to change the base?
Conversation
Tagging subscribers to this area: @dotnet/area-system-numerics |
|
||
int cuDst = 0; | ||
// The Ratio is calculated as: log_{10^9}(2^32) | ||
const double digitRatio = 1.0703288734719332; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it worth to do a FP calculation, instead of conservatively allocate for upper limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not much difference, but the required storage size will be smaller. For example, when the bit length is 58 or 59, stackalloc is used.
_bit.Length | old | new |
---|---|---|
57 | 64 | 62 |
58 | 65 | 63 |
59 | 66 | 64 |
This change also aims to achieve mathematical correctness in the implementation.
Debug.Assert(base1E9Buffer[iuDst] < TenPowMaxPartial); | ||
int base1E9BufferLength = (int)(value._bits.Length * digitRatio) + 1; | ||
uint[]? base1E9BufferFromPool = null; | ||
Span<uint> base1E9Buffer = base1E9BufferLength < BigIntegerCalculator.StackAllocThreshold ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an (uint)
cast to check lower bound of base1E9BufferLength
(and make it clear that both bounds are checked)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added (uint). Also, to match other parts, I used <= instead of < and removed the excess portion using Slice
.
efcf4e1
to
bf75784
Compare
This PR is a counterpart to #55121. divide-and-conquer algorithm
Number.FormatBigInteger()
can run inThe computational complexity of division have been improved by #96895.
Benchmark
Code