Skip to content

Parse spans directly in TryParse(ReadOnlySpan<char>, IFormatProvider, out T) - #713

Merged
SimonCropp merged 1 commit into
mainfrom
perf/numbers-tryparse-span
Oct 12, 2026
Merged

SimonCropp merged 1 commit into
mainfrom
perf/numbers-tryparse-span

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

The TryParse(ReadOnlySpan<char>, IFormatProvider?, out T) polyfills (below net7) did s.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_GREATER
  • nint, nuint: NET6_0_OR_GREATER

Checked against the reference assemblies: the span overloads of IntPtr/UIntPtr are 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 custom NumberFormatInfo (, 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 of decimal, overflow, NaN and 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

before after
int.TryParse(span, provider) 19 ns / 40 B 32 ns / 0 B
long.TryParse(span, provider) 19 ns / 40 B 11 ns / 0 B
ulong.TryParse(span, provider) 20 ns / 40 B 11 ns / 0 B
byte.TryParse(span, provider) 13 ns / 32 B 21 ns / 0 B
float.TryParse(span, provider) 64 ns / 48 B 51 ns / 0 B
double.TryParse(span, provider) 64 ns / 48 B 52 ns / 0 B
decimal.TryParse(span, provider) 75 ns / 48 B 55 ns / 0 B
nint.TryParse(span, provider) 18 ns / 40 B 12 ns / 0 B

netcoreapp3.1 build

before after
int.TryParse(span, provider) 17 ns / 40 B 10 ns / 0 B
double.TryParse(span, provider) 56 ns / 48 B 54 ns / 0 B
decimal.TryParse(span, provider) 63 ns / 48 B 56 ns / 0 B
nint.TryParse(span, provider) 21 ns / 40 B 21 ns / 40 B (unchanged below net6)

@SimonCropp SimonCropp modified the milestones: 11.4.4, 11.4.5 Oct 12, 2026
@SimonCropp
SimonCropp merged commit d4cebf4 into main Oct 12, 2026
5 of 7 checks passed
@SimonCropp
SimonCropp deleted the perf/numbers-tryparse-span branch October 12, 2026 12:12
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant