Skip to content

Parse Int32 via TryParseInt64D in Utf8Parser so it can be inlined - #136762

Closed
bgrainger wants to merge 2 commits into
dotnet:mainfrom
bgrainger:utf8parser-int32-via-int64
Closed

bgrainger wants to merge 2 commits into
dotnet:mainfrom
bgrainger:utf8parser-int32-via-int64

Conversation

@bgrainger

Copy link
Copy Markdown
Contributor

Fixes #136761

TryParseInt32D was the last large, fully unrolled Utf8Parser integer parser (659 bytes of IL). It sits just outside the JIT's profitability threshold for inlining, so Utf8Parser.TryParse(..., out int, ...) pays a call that the other seven integer overloads don't. This change implements it as TryParseInt64D + a range check, which accepts and rejects the same inputs and is small enough to inline (+6/−133 lines).

The sbyte/short/byte/ushort/uint parsers are unchanged: they already inline, and routing them through the 64-bit parser is slower for them.

Behavior

Unchanged, including:

  • a leading + or -, and -0
  • any number of leading zeros
  • out-of-range 10-digit values, and 11 or more digits
  • trailing non-digits
  • bytesConsumed = 0 and value = 0 on failure

Inputs that overflow int but fit in long are now rejected after parsing instead of at the 11th digit.

Tests

Added D-format Int32 cases with 22+ leading zeros (MaxValue, MinValue, MaxValue + 1). Inputs of 19+ bytes now go through TryParseInt64D's overflow-checked loop, and nothing covered that. System.Memory.Tests with -namespace System.Buffers.Text.Tests: 44,437 passed, 0 failed.

Performance

BenchmarkDotNet 0.16.0-preview.2, i9-14900K with every process pinned to P-cores. Local Release builds of main (591622c) and this change in one run; only System.Private.CoreLib.dll differs. The benchmark is Utf8Parser.TryParse(bytes, out int value, out int consumed) ? value + consumed : -1.

Input main PR Ratio
0 2.13 ns 1.28 ns 0.60
42 2.39 ns 1.53 ns 0.64
-7 2.16 ns 1.64 ns 0.76
12345 3.34 ns 2.86 ns 0.86
123456789 4.21 ns 4.03 ns 0.96
-2147483648 4.60 ns 4.71 ns 1.02

Code this change doesn't touch moved by up to ±25% between the two builds in the same run (layout noise). So the 1–2 digit wins are clear, and the longer inputs are a wash. The issue has the analysis and the JIT-knob experiments.

🤖 Generated with Claude Code

TryParseInt32D was the last large, fully unrolled Utf8Parser integer
parser (659 bytes of IL). It falls just outside the JIT's profitability
threshold for inlining, so Utf8Parser.TryParse(..., out int, ...) pays a
call that the other integer overloads don't. Parsing as Int64 and
range-checking accepts and rejects the same inputs and is small enough
to inline.

Fixes dotnet#136761

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 31, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

@tannergooding

Copy link
Copy Markdown
Member

This seems worse than just doing the same parsing implementation pattern for TryParseInt32D

It's also largely unnecessary as Utf8Parser is effectively replaced by types like int just naturally supporting UTF8 parsing now and having a much broader range of support in that scenario; which is where future optimizations and handling are preferred to be targeted if they're actually relevant (and I'm not convinced this case is "that" relevant, you're not going to be bottlenecked on the 1ns difference here in practice, you'll rather get bottlenecked on the higher level operations feeding this).

Instead of calling TryParseInt64D and range-checking the result, parse
Int32 with the same loop as TryParseInt64D, using a 32-bit accumulator.
It is still small enough to inline (231 bytes of IL, down from 659) and
uses only 32-bit arithmetic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bgrainger bgrainger closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Numerics community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Utf8Parser.TryParse(..., out int, ...) is slower than the other integer overloads because TryParseInt32D is too large to inline

2 participants