Skip to content

perf: remove LINQ and HashSet lookups from the YAML string check - #3184

Open
Gal Be (g4lb) wants to merge 1 commit into
microsoft:mainfrom
g4lb:perf/yaml-string-check
Open

Gal Be (g4lb) wants to merge 1 commit into
microsoft:mainfrom
g4lb:perf/yaml-string-check

Conversation

@g4lb

Copy link
Copy Markdown

Description

GetYamlCompatibleString runs for each property name and each string value that OpenApiYamlWriter writes. It used LINQ calls on three HashSet<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

  • Other (please describe): performance improvement. The output does not change.

Changes Made

  • Replace the three HashSet<string> fields and their LINQ calls with character scans and ordinal IndexOf calls.
  • Skip the decimal, hexadecimal, and DateTime parses when the string has no ASCII digit. Each of these parses needs at least one ASCII digit to succeed.
  • The EndsWith(":") check stays culture-sensitive, as before.

Testing

  • All existing tests pass
  • Manual testing performed

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.TryParse and DateTime.TryParse with 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.

Benchmark Before After Allocated before Allocated after
Write YAML, generated 40-path document (v3) 1.57 ms 400 µs 1,936 KB 558 KB
Write YAML, document built in code 580 µs 222 µs 800 KB 367 KB
Write 10 short plain strings 7.04 µs 1.30 µs 9.2 KB 1.6 KB
Write 10 number strings 6.67 µs 1.62 µs 9.2 KB 2.0 KB
Write 10 date-like strings 8.10 µs 2.65 µs 9.5 KB 2.3 KB
Write 9 non-ASCII strings 9.15 µs 3.63 µs 8.4 KB 1.5 KB

The JSON writer and the readers did not change: the difference was less than the noise, and the allocations were the same.

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

Versions applicability

  • My change applies to the version 2.X of the library, if so PR link:
  • I have evaluated the applicability of my change against the other versions above.

The support/v2 branch has the same code. I can open a backport PR if you want one.

Additional Notes

One small difference: when WriteValue gets a null string, the ArgumentNullException now names the parameter input, not source.

@g4lb
Gal Be (g4lb) requested a review from a team as a code owner November 20, 2026 16:15
Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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 source to input.
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

No deployments
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.

2 participants