Repository navigation
Parse spans directly in TryParse(ReadOnlySpan<char>, IFormatProvider, out T) - #713
Merged
Merged
Conversation
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
TryParse(ReadOnlySpan<char>, IFormatProvider?, out T)polyfills (below net7) dids.ToString()to reach the string overload, allocating a string per call.Where the BCL already has
T.TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider, out T)they now call that with the same default styles as before, and keep the string path elsewhere:byte,sbyte,short,ushort,int,uint,long,ulong,float,double,decimal:NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATERnint,nuint:NET6_0_OR_GREATERChecked against the reference assemblies: the span overloads of
IntPtr/UIntPtrare in the net6 pack but not in the net5 one (which only has the string overloads), and the primitives have them in the netcoreapp3.0/3.1 packs. The solution build compiles the netcoreapp2.1 and netstandard2.1 splits.The styles passed are untouched (
NumberStyles.Integer,NumberStyles.Float | NumberStyles.AllowThousands,NumberStyles.Number), so results are the same.Tests
For each of the 13 types, the span overload is compared with
T.TryParse(string, <default styles>, provider, out)(both the returned bool and the value) for about 60 inputs with a null provider, the invariant culture and a customNumberFormatInfo(,decimal separator,.group separator,~negative sign): signs, leading and trailing whitespace, empty and whitespace-only input, group and decimal separators, exponents, parentheses, currency and hex notation, the boundary values of every integer width and ofdecimal, overflow,NaNand the infinities, full-width digits and a no-break space. The span is a slice out of the middle of a longer string, so reading outside of it would change the result. An empty span is checked separately.The new tests pass against the previous implementation and against the BCL on net7+. Locally run on net48, the netcoreapp3.1 and net6.0 builds (rolled forward onto the .NET 8 runtime) and net10.0.
Measurements
Stopwatch /
GC.GetAllocatedBytesForCurrentThread, best of 9 rounds after warm-up, Release. At this scale the times are dominated by noise on a busy machine (the same code measures anywhere between 10 and 30 ns); the allocation figures are exact.net6.0 build
int.TryParse(span, provider)long.TryParse(span, provider)ulong.TryParse(span, provider)byte.TryParse(span, provider)float.TryParse(span, provider)double.TryParse(span, provider)decimal.TryParse(span, provider)nint.TryParse(span, provider)netcoreapp3.1 build
int.TryParse(span, provider)double.TryParse(span, provider)decimal.TryParse(span, provider)nint.TryParse(span, provider)