Repository navigation
perf: remove LINQ and HashSet lookups from the YAML string check - #3184
Open
Gal Be (g4lb) wants to merge 1 commit into
Open
Gal Be (g4lb) wants to merge 1 commit into
Gal Be (g4lb) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the YAML string-processing optimization.
No open issues 🎉
What changed in this PR
Reduces per-string processing overhead in OpenApiYamlWriter while preserving YAML quoting behavior.
Changes:
- Replaces LINQ-based checks and HashSet fields with character scans and direct string searches.
- Skips numeric, hexadecimal, and timestamp parsing when no ASCII digit is present.
- Adds explicit null validation, changing the exception parameter name from
sourcetoinput.
| File | Description |
|---|---|
src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs |
Optimizes YAML string checks and adds explicit null validation. |
Review effort: Balanced
This branch has not been deployed
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.
Description
GetYamlCompatibleStringruns for each property name and each string value thatOpenApiYamlWriterwrites. It used LINQ calls on threeHashSet<string>fields, and it tried to parse each string as a number, a hexadecimal value, and a date. This change makes the check faster and keeps the output the same.Type of Change
Changes Made
HashSet<string>fields and their LINQ calls with character scans and ordinalIndexOfcalls.decimal, hexadecimal, andDateTimeparses when the string has no ASCII digit. Each of these parses needs at least one ASCII digit to succeed.EndsWith(":")check stays culture-sensitive, as before.Testing
I compared the old and the new method on 1.2 million random and edge-case strings under the invariant, en-US, tr-TR, and de-DE cultures. The output was the same for each string. I also parsed 6.47 million strings that have no ASCII digit.
decimal.TryParseandDateTime.TryParsewith the invariant culture accepted none of them, on .NET 8 and on .NET 10.Benchmarks: BenchmarkDotNet, short job, 12 interleaved runs, .NET 10, macOS on Apple silicon (ARM64). I did not measure x64.
The JSON writer and the readers did not change: the difference was less than the noise, and the allocations were the same.
Checklist
Versions applicability
The
support/v2branch has the same code. I can open a backport PR if you want one.Additional Notes
One small difference: when
WriteValuegets anullstring, theArgumentNullExceptionnow names the parameterinput, notsource.