perf(dotnet/policy): cache YamlDotNet IDeserializer in Policy.FromYaml#2155
Merged
imran-siddique merged 1 commit intoMay 12, 2026
Conversation
DeserializerBuilder.Build() walks every public property on the target type (Policy.PolicyDocument and Policy.RuleDocument), resolves naming conventions, and wires up type inspectors and converters. That cost is identical on every call and the resulting IDeserializer is documented as thread-safe once built, so building one per call is pure waste -- loading a few policies at startup, or hot-reloading them from disk, pays the construction cost N times for no benefit. Promote the builder result to a static-readonly field so every FromYaml / FromYamlFile call reuses the same configured deserializer. This mirrors the existing JsonSerializerOptions caching right above it (PolicyDocument's JSON path already does the equivalent). The new regression test parses three different policy documents sequentially and in parallel across multiple iterations, pinning that the cached deserializer's results stay stable across calls -- guarding against future refactors that would (accidentally) introduce per-call state on the shared instance.
🤖 AI Agent: test-generator — `Policy.cs`
|
🤖 AI Agent: code-reviewer — View detailsTL;DR: 0 blockers, 0 warnings. No issues found. Clean change.
Action items: None. |
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
|
🟡 Contributor Check: MEDIUM
Automated check by AGT Contributor Check. |
PR Review Summary
Verdict: ✅ Ready for human review |
MohammadHaroonAbuomar
pushed a commit
to MohammadHaroonAbuomar/agt-acs
that referenced
this pull request
Jun 1, 2026
microsoft#2155) DeserializerBuilder.Build() walks every public property on the target type (Policy.PolicyDocument and Policy.RuleDocument), resolves naming conventions, and wires up type inspectors and converters. That cost is identical on every call and the resulting IDeserializer is documented as thread-safe once built, so building one per call is pure waste -- loading a few policies at startup, or hot-reloading them from disk, pays the construction cost N times for no benefit. Promote the builder result to a static-readonly field so every FromYaml / FromYamlFile call reuses the same configured deserializer. This mirrors the existing JsonSerializerOptions caching right above it (PolicyDocument's JSON path already does the equivalent). The new regression test parses three different policy documents sequentially and in parallel across multiple iterations, pinning that the cached deserializer's results stay stable across calls -- guarding against future refactors that would (accidentally) introduce per-call state on the shared instance.
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.
Summary
Policy.FromYamlrebuilds a freshIDeserializeron every call:DeserializerBuilder.Build()walks every public property on the target type (Policy.PolicyDocumentandPolicy.RuleDocument), resolves the naming convention, and wires up type inspectors and converters. That cost is identical on every call, and YamlDotNet documents the resultingIDeserializeras thread-safe once built -- so building it per call is pure waste. Loading a handful of policies at startup, or hot-reloading them from disk, pays the construction cost N times for no benefit.Change
Promote the builder result to a
static readonly IDeserializerfield so everyFromYaml/FromYamlFilecall reuses the same configured deserializer. This mirrors the existingJsonSerializerOptionscaching right above it -- the JSON path already does the equivalent.Tests
Added
FromYaml_CachedDeserializer_ProducesStableResultsAcrossCalls: parses three different policy documents sequentially and in parallel across multiple iterations, and asserts that field-for-field results stay stable across calls. Guards against future refactors that would accidentally introduce per-call state on the shared instance.Test plan
dotnet test-- 658 / 658 passing (previous baseline 657 + 1 new)FromYaml_*tests unchangedSurfaced during independent audit conducted by @finnoybu (Ken Tannenbaum, AEGIS Initiative); [LOW, .NET].