Improve performance of managed formatting code
authorStephen Toub <stoub@microsoft.com>
Wed, 15 Nov 2017 17:54:06 +0000 (12:54 -0500)
committerStephen Toub <stoub@microsoft.com>
Sat, 18 Nov 2017 22:58:51 +0000 (17:58 -0500)
commit7f0eb6109c8572e15c5a263c864f53fb2442baf3
tree03b3eeaecd5f332eb82da41100f1285a8188341b
parent64a1fd6e3d5e6d44b643d02671521aa3e71e7ec8
Improve performance of managed formatting code

- Optimize NumberBuffer passing by reference instead of value. It's a large struct of ~50 bytes; copying it around has non-trivial cost.
- Replace formatting StringBuilder with ref struct and stack allocation. Avoids lots of allocation and associated throughput costs.
- Improve perf of 'D' formatting of 32-bit and 64-bit integers.
- Remove array allocations accessing NumberFormatInfo props.
- Accessing array properties like PercentGroupSizes clones the corresponding field.  That's unnecessary here, as we don't mutate the array.
- Remove int[] allocation from NumberToStringFormat. Span makes it easy to start with stack space and grow to an allocated array as needed.
- Improve perf of hex formatting of integers. Including removing some sizable allocations.
- Manually inline several hot functions called in only one place.
- Tweak some range comparisons in ParseFormatSpecifier.
- Avoid large stackallocs in NumberToString{Fixed}. It's incurring non-trivial overheads.
- Tweak perf of ValueStringBuilder. In particular, make Append(string) faster for the single-char case, which is extremely common in integer formatting due to its prevalence in strings in NumberFormatInfo.
src/mscorlib/shared/System.Private.CoreLib.Shared.projitems
src/mscorlib/shared/System/Number.Formatting.cs
src/mscorlib/shared/System/Number.Parsing.cs
src/mscorlib/shared/System/Text/ValueStringBuilder.cs [new file with mode: 0644]