Skip to content

Use pre-compiled static Regex instances in ScalarNodeDeserializer#1088

Merged
EdwardCooke merged 1 commit intoaaubry:masterfrom
fdcastel:perf/regex-compilation-caching
Apr 9, 2026
Merged

Use pre-compiled static Regex instances in ScalarNodeDeserializer#1088
EdwardCooke merged 1 commit intoaaubry:masterfrom
fdcastel:perf/regex-compilation-caching

Conversation

@fdcastel
Copy link
Copy Markdown
Contributor

Problem

ScalarNodeDeserializer.cs calls Regex.IsMatch() with string patterns on every scalar deserialization. These patterns are constant strings but the regexes are not compiled or cached. On hot paths (large YAML documents), this creates repeated regex compilation overhead for every scalar value processed.

Affected call sites:

  • DeserializeBooleanHelper() — boolean true/false patterns (2 calls)
  • AttemptUnknownTypeDeserialization() — hex, octal, float, infinity, NaN patterns (5 calls)

Fix

Converted all 7 regex patterns from inline Regex.IsMatch(value, pattern) calls to pre-compiled static readonly Regex instances with RegexOptions.Compiled. The regexes are compiled once at class initialization and reused for all subsequent calls.

Uses the project's existing StandardRegexOptions.Compiled pattern for consistency.

Files Changed

  • YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs

Validation

All 326 serialization tests pass with no regressions.

Replace all Regex.IsMatch() calls with pre-compiled static readonly Regex
instances using RegexOptions.Compiled. This eliminates repeated regex
compilation overhead on every scalar deserialization call.

Affected patterns:
- Boolean true/false patterns (DeserializeBooleanHelper)
- Hex, octal, float, infinity, NaN patterns (AttemptUnknownTypeDeserialization)

All 7 regex patterns are now compiled once at class initialization.
@EdwardCooke EdwardCooke merged commit 53ef015 into aaubry:master Apr 9, 2026
1 check passed
This was referenced Apr 9, 2026
This was referenced Apr 13, 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.

2 participants