Skip to content

System.Text.Json: replace enum integer regex with a lexical scan - #137586

Open
RutulPatel8 wants to merge 1 commit into
dotnet:mainfrom
RutulPatel8:perf/json-integer-lexical-scan-136184
Open

RutulPatel8 wants to merge 1 commit into
dotnet:mainfrom
RutulPatel8:perf/json-integer-lexical-scan-136184

Conversation

@RutulPatel8

Copy link
Copy Markdown
Contributor

Fixes #136184

EnumConverter currently uses a regex to decide whether a quoted enum value is an integer. The check only needs optional sign and ASCII digit recognition, so this replaces it with a small span-based scan and removes the regex setup.

The scanner retains the existing whitespace handling and leaves named enum values and AllowNumbers behavior intact. Tests cover signed values, leading zeros, Unicode whitespace, disabled numeric values, and invalid integer-like strings.

Validation: git diff --check passed. Full unit tests could not run locally because this checkout requires SDK 12.0.100-alpha.1.26557.102, which is not installed and could not be downloaded in this environment. CI will provide build and test results.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 15, 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-text-json
See info in area-owners.md if you want to be subscribed.


foreach (char c in source)
{
if ((uint)(c - '0') > 9)

@MihaZupan MihaZupan Nov 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((uint)(c - '0') > 9)
if (!char.IsAsciiDigit(c))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. System.Text.Json also targets netstandard2.0 and .NET Framework, where char.IsAsciiDigit is unavailable. The unsigned subtraction keeps this check valid across those targets without a conditional compilation branch. I agree with the concern about using a span search for these typically short enum tokens, so I am keeping the simple loop for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @MihaZupan that we should use char.IsAsciiDigit, even if it means conditional compilation. I don't know whether it changes the generated code here, but it makes the intent clearer and lets us benefit from any future optimizations to that API, including in the JIT.

When we drop the older targets, we can search for #if NET and remove the fallback branches. Keeping the fallback as the only implementation makes it much harder to notice that this code can be simplified later.

We could also use a polyfill, following CharPolyfills.cs, which already has IsAsciiHexDigit. Adding IsAsciiDigit there and including it for the older targets would keep the call site simple.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polyfills are the preferred way to solve this problem.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json 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.

[System.Text.Json] Replace JsonHelpers.IntegerRegex with a lexical scan

5 participants