-
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
BigInteger parsing optimization for large decimal string #51953
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -494,35 +494,242 @@ private static bool HexNumberToBigInteger(ref BigNumberBuffer number, out BigInt | |
} | ||
} | ||
|
||
private static int s_naiveThreshold = 20000; | ||
private static bool NumberToBigInteger(ref BigNumberBuffer number, out BigInteger result) | ||
{ | ||
Span<uint> stackBuffer = stackalloc uint[BigInteger.StackallocUInt32Limit]; | ||
|
||
Span<uint> currentBuffer = stackBuffer; | ||
int currentBufferSize = 0; | ||
|
||
int[]? arrayFromPool = null; | ||
|
||
uint partialValue = 0; | ||
int partialDigitCount = 0; | ||
int totalDigitCount = 0; | ||
int numberScale = number.scale; | ||
|
||
const int MaxPartialDigits = 9; | ||
const uint TenPowMaxPartial = 1000000000; | ||
|
||
try | ||
{ | ||
foreach (ReadOnlyMemory<char> digitsChunk in number.digits.GetChunks()) | ||
if (number.digits.Length <= s_naiveThreshold) | ||
{ | ||
if (!ProcessChunk(digitsChunk.Span, ref currentBuffer)) | ||
uint partialValue = 0; | ||
int partialDigitCount = 0; | ||
|
||
foreach (ReadOnlyMemory<char> digitsChunk in number.digits.GetChunks()) | ||
{ | ||
if (!ProcessChunk(digitsChunk.Span, ref currentBuffer)) | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
} | ||
|
||
if (partialDigitCount > 0) | ||
{ | ||
MultiplyAdd(ref currentBuffer, s_uint32PowersOfTen[partialDigitCount], partialValue); | ||
} | ||
|
||
bool ProcessChunk(ReadOnlySpan<char> chunkDigits, ref Span<uint> currentBuffer) | ||
{ | ||
int remainingIntDigitCount = Math.Max(numberScale - totalDigitCount, 0); | ||
ReadOnlySpan<char> intDigitsSpan = chunkDigits.Slice(0, Math.Min(remainingIntDigitCount, chunkDigits.Length)); | ||
|
||
bool endReached = false; | ||
|
||
// Storing these captured variables in locals for faster access in the loop. | ||
uint _partialValue = partialValue; | ||
int _partialDigitCount = partialDigitCount; | ||
int _totalDigitCount = totalDigitCount; | ||
|
||
for (int i = 0; i < intDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = chunkDigits[i]; | ||
if (digitChar == '\0') | ||
{ | ||
endReached = true; | ||
break; | ||
} | ||
|
||
_partialValue = _partialValue * 10 + (uint)(digitChar - '0'); | ||
_partialDigitCount++; | ||
_totalDigitCount++; | ||
|
||
// Update the buffer when enough partial digits have been accumulated. | ||
if (_partialDigitCount == MaxPartialDigits) | ||
{ | ||
MultiplyAdd(ref currentBuffer, TenPowMaxPartial, _partialValue); | ||
_partialValue = 0; | ||
_partialDigitCount = 0; | ||
} | ||
} | ||
|
||
// Check for nonzero digits after the decimal point. | ||
if (!endReached) | ||
{ | ||
ReadOnlySpan<char> fracDigitsSpan = chunkDigits.Slice(intDigitsSpan.Length); | ||
for (int i = 0; i < fracDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = fracDigitsSpan[i]; | ||
if (digitChar == '\0') | ||
{ | ||
break; | ||
} | ||
if (digitChar != '0') | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
partialValue = _partialValue; | ||
partialDigitCount = _partialDigitCount; | ||
totalDigitCount = _totalDigitCount; | ||
|
||
return true; | ||
} | ||
} | ||
else | ||
{ | ||
if (numberScale < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just adding a note to myself: This is the real change in the PR. The block above is just refactoring inside an |
||
{ | ||
result = default; | ||
return false; | ||
} | ||
key-moon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
totalDigitCount = Math.Min(number.digits.Length - 1, numberScale); | ||
var bufferSize = (totalDigitCount + MaxPartialDigits - 1) / MaxPartialDigits; | ||
key-moon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (partialDigitCount > 0) | ||
{ | ||
MultiplyAdd(ref currentBuffer, s_uint32PowersOfTen[partialDigitCount], partialValue); | ||
Span<uint> buffer = new uint[bufferSize]; | ||
|
||
int bufferInd = buffer.Length - 1; | ||
uint currentBlock = 0; | ||
int shiftUntil = (totalDigitCount - 1) % MaxPartialDigits; | ||
int remainingIntDigitCount = totalDigitCount; | ||
foreach (ReadOnlyMemory<char> digitsChunk in number.digits.GetChunks()) | ||
{ | ||
var digitsChunkSpan = digitsChunk.Span; | ||
ReadOnlySpan<char> intDigitsSpan = digitsChunkSpan.Slice(0, Math.Min(remainingIntDigitCount, digitsChunkSpan.Length)); | ||
|
||
for (int i = 0; i < intDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = intDigitsSpan[i]; | ||
Debug.Assert(char.IsDigit(digitChar)); | ||
currentBlock = currentBlock * 10 + digitChar - '0'; | ||
if (shiftUntil == 0) | ||
{ | ||
buffer[bufferInd] = currentBlock; | ||
currentBlock = 0; | ||
bufferInd--; | ||
shiftUntil = MaxPartialDigits; | ||
} | ||
shiftUntil--; | ||
} | ||
remainingIntDigitCount -= intDigitsSpan.Length; | ||
|
||
ReadOnlySpan<char> fracDigitsSpan = digitsChunkSpan.Slice(intDigitsSpan.Length); | ||
for (int i = 0; i < fracDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = fracDigitsSpan[i]; | ||
if (digitChar == '\0') | ||
{ | ||
break; | ||
} | ||
if (digitChar != '0') | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
} | ||
} | ||
Debug.Assert(bufferInd == -1); | ||
|
||
unsafe | ||
{ | ||
Span<uint> newBuffer = new uint[bufferSize]; | ||
|
||
arrayFromPool = ArrayPool<int>.Shared.Rent(1); | ||
Span<uint> multiplier = MemoryMarshal.Cast<int, uint>(arrayFromPool); | ||
multiplier[0] = TenPowMaxPartial; | ||
|
||
int blockSize = 1; | ||
while (true) | ||
{ | ||
fixed (uint* bufPtr = buffer, newBufPtr = newBuffer, mulPtr = multiplier) | ||
{ | ||
uint* curBufPtr = bufPtr; | ||
uint* curNewBufPtr = newBufPtr; | ||
for (int i = 0; i < bufferSize; i += blockSize * 2) | ||
{ | ||
int len = Math.Min(bufferSize - i, blockSize * 2); | ||
int lowerLen = Math.Min(len, blockSize); | ||
int upperLen = len - lowerLen; | ||
if (upperLen != 0) | ||
{ | ||
BigIntegerCalculator.Multiply(mulPtr, blockSize, curBufPtr + blockSize, upperLen, curNewBufPtr, len); | ||
} | ||
|
||
long carry = 0; | ||
int j = 0; | ||
for (; j < lowerLen; j++) | ||
{ | ||
long digit = (curBufPtr[j] + carry) + curNewBufPtr[j]; | ||
curNewBufPtr[j] = unchecked((uint)digit); | ||
carry = digit >> 32; | ||
} | ||
if (carry != 0) | ||
{ | ||
while (true) | ||
{ | ||
curNewBufPtr[j]++; | ||
if (curNewBufPtr[j] != 0) | ||
{ | ||
break; | ||
} | ||
j++; | ||
} | ||
} | ||
|
||
curBufPtr += blockSize * 2; | ||
curNewBufPtr += blockSize * 2; | ||
} | ||
} | ||
|
||
// swap | ||
var tmp = buffer; | ||
buffer = newBuffer; | ||
newBuffer = tmp; | ||
blockSize *= 2; | ||
|
||
if (bufferSize <= blockSize) | ||
{ | ||
break; | ||
} | ||
newBuffer.Clear(); | ||
int[]? arrayToReturn = arrayFromPool; | ||
|
||
arrayFromPool = ArrayPool<int>.Shared.Rent(blockSize); | ||
Span<uint> newMultiplier = MemoryMarshal.Cast<int, uint>(arrayFromPool); | ||
newMultiplier.Clear(); | ||
fixed (uint* mulPtr = multiplier, newMulPtr = newMultiplier) | ||
{ | ||
BigIntegerCalculator.Square(mulPtr, blockSize / 2, newMulPtr, blockSize); | ||
} | ||
multiplier = newMultiplier; | ||
if (arrayToReturn is not null) | ||
{ | ||
ArrayPool<int>.Shared.Return(arrayToReturn); | ||
} | ||
} | ||
} | ||
|
||
// = log_{2^32}(10^9) | ||
const double digitRatio = 0.934292276687070661; | ||
currentBufferSize = Math.Min((int)(bufferSize * digitRatio) + 1, bufferSize); | ||
while (buffer[currentBufferSize - 1] == 0) | ||
{ | ||
currentBufferSize--; | ||
} | ||
currentBuffer = buffer.Slice(0, currentBufferSize); | ||
} | ||
|
||
int trailingZeroCount = numberScale - totalDigitCount; | ||
|
@@ -568,65 +775,6 @@ private static bool NumberToBigInteger(ref BigNumberBuffer number, out BigIntege | |
} | ||
} | ||
|
||
bool ProcessChunk(ReadOnlySpan<char> chunkDigits, ref Span<uint> currentBuffer) | ||
{ | ||
int remainingIntDigitCount = Math.Max(numberScale - totalDigitCount, 0); | ||
ReadOnlySpan<char> intDigitsSpan = chunkDigits.Slice(0, Math.Min(remainingIntDigitCount, chunkDigits.Length)); | ||
|
||
bool endReached = false; | ||
|
||
// Storing these captured variables in locals for faster access in the loop. | ||
uint _partialValue = partialValue; | ||
int _partialDigitCount = partialDigitCount; | ||
int _totalDigitCount = totalDigitCount; | ||
|
||
for (int i = 0; i < intDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = chunkDigits[i]; | ||
if (digitChar == '\0') | ||
{ | ||
endReached = true; | ||
break; | ||
} | ||
|
||
_partialValue = _partialValue * 10 + (uint)(digitChar - '0'); | ||
_partialDigitCount++; | ||
_totalDigitCount++; | ||
|
||
// Update the buffer when enough partial digits have been accumulated. | ||
if (_partialDigitCount == MaxPartialDigits) | ||
{ | ||
MultiplyAdd(ref currentBuffer, TenPowMaxPartial, _partialValue); | ||
_partialValue = 0; | ||
_partialDigitCount = 0; | ||
} | ||
} | ||
|
||
// Check for nonzero digits after the decimal point. | ||
if (!endReached) | ||
{ | ||
ReadOnlySpan<char> fracDigitsSpan = chunkDigits.Slice(intDigitsSpan.Length); | ||
for (int i = 0; i < fracDigitsSpan.Length; i++) | ||
{ | ||
char digitChar = fracDigitsSpan[i]; | ||
if (digitChar == '\0') | ||
{ | ||
break; | ||
} | ||
if (digitChar != '0') | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
partialValue = _partialValue; | ||
partialDigitCount = _partialDigitCount; | ||
totalDigitCount = _totalDigitCount; | ||
|
||
return true; | ||
} | ||
|
||
void MultiplyAdd(ref Span<uint> currentBuffer, uint multiplier, uint addValue) | ||
{ | ||
Span<uint> curBits = currentBuffer.Slice(0, currentBufferSize); | ||
|
This file contains 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
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.
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.
Nit:
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.
Also, it'd be helpful to add a comment about how this value was selected.