Improve performance of integer formatting (#76726)
authorStephen Toub <stoub@microsoft.com>
Tue, 11 Oct 2022 04:05:48 +0000 (00:05 -0400)
committerGitHub <noreply@github.com>
Tue, 11 Oct 2022 04:05:48 +0000 (00:05 -0400)
commit5892ef2a557d83f06eeab9580fee9e2c708244a6
tree0a365bd13bbc08db8cb652c2fed815ec87ca8dbf
parentdaa88f67964c92df75759b3a40d7cf0970be0eeb
Improve performance of integer formatting (#76726)

* Improve performance of integer formatting

1. The DivRem(..., 10) for each digit in the number ends up being the most expensive aspect of formatting.  This employs a trick other formatting libraries use, which is to have a table for all the formatted values between 00 and 99, and to then DivRem(..., 100) to cut the number of operations in half, which for longer values is meaningful.
2. Avoids going through the digit counting path when we know at the call site it won't be needed.
3. Employs a branch-free, table-based lookup for CountDigits(ulong) to go with a similar approach added for uint.

* Address PR feedback (move license notice)
THIRD-PARTY-NOTICES.TXT
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/FormattingHelpers.CountDigits.cs
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
src/libraries/System.Private.CoreLib/src/System/UInt64.cs