Repository navigation
Generate one member reader per type and move merge-key handling into the runtime - #172
Merged
Merged
Conversation
The generator wrote every member's read logic twice per type: once in the main loop and once in `ApplyMergeMapping` for YAML merge keys. Both now call a single local `ReadMember(string key)`, in the plain and the constructor-parameter paths. The emitted code is the same logic with half the member readers, so deserialization behaves identically. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every generated object reader carried its own copy of the YAML merge-key machinery, two local functions of about 100 lines. They now call `YamlReader.ReadMergeValue`, which holds that logic once, and pass `ReadMember` as the member reader. Adds tests for merge sequences, nested merges, `<<: null` and the three merge errors on generated contexts. They pass unchanged on the previous generator. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This was referenced Nov 4, 2026
`YamlReader.ReadMergeValue` took the generated `ReadMember` local function as an `Action<string>`. Converting a local function to a delegate makes the C# compiler allocate a closure class at the start of the method, for every object that is read, even when it has no merge key. A benchmark of the generated reader showed +48 bytes per object, 9.7 KB for a document of 200 services, with no change in time. `ReadMergeKeys` is an iterator that yields each merged key, and the generated reader calls `ReadMember` for it directly, so the closure stays a struct. The iterator is only allocated when a `<<` key is read. The merge behavior and the three error messages are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This was referenced Nov 4, 2026
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.
The source generator now emits each member's reader once per object type instead of twice, and the YAML merge-key machinery lives in
YamlReader.ReadMergeKeysinstead of two local functions in every generated reader. Deserialization behaves identically.Why
Generated code size drives compile time. In a downstream solution (docs-builder, 14
[YamlSerializable]roots, 271 members), the context generated a 6.9 MB file, and the trimming analyzer enabled byIsAotCompatible(ILLink.RoslynAnalyzer) needed about 4 minutes of CPU on it. The generator itself takes 17 ms; the cost is the analyzer running over its output, and it grows roughly linearly with the output size.For every object type the generator wrote the member-matching chain twice: once in the main loop and once inside
ApplyMergeMappingfor<<. It also wroteReadAndApplyMergeandApplyMergeMappingper type, about 100 lines each time.What
One
ReadMember(string key)per typeBoth the main loop and merged mappings call it. This covers the plain path and the constructor-parameter path. A read-ignored member no longer emits a loop-level
continue, because thematchedflag already makes it a no-op.YamlReader.ReadMergeKeys(bool mergeEnabled, HashSet<string>? explicitKeys)The merge logic moved here unchanged, including the three
YamlExceptionmessages. It is an iterator that yields each merged key, with the reader positioned on that key's value, and the generated reader callsReadMemberfor it in aforeach. It is documented as intended for generated code, likeIsDiscriminatorMemberandBeginUnmappedMember.An iterator and not an
Action<string>is deliberate. My first version tookReadMemberas a delegate, and a benchmark of the generated reader showed it allocated 48 bytes per object (9.7 KB for a 200-service document, with no change in time). Converting a local function to a delegate makes the compiler allocate a closure class for every object that is read, even without a merge key. The last commit fixes that, and the iterator is only allocated when a<<key is read.Results
The same downstream project,
PreferredObjectCreationHandlingpinned, cold compile, alternating runs:ILLink.RoslynAnalyzerCPUThe downstream solution's 8,630 unit tests pass with this build, apart from one failure that also fails on its unmodified
main.Tests
Adds 7 tests on generated contexts: merge sequences applied in order (plain and constructor types), nested merges,
<<: null, and the three merge errors. All of them also pass against the generator onmain, so they describe existing behaviour. I mutated the merge call in each path, the explicit-key check andReadMergeKeys, and each mutation fails tests.I kept the
GenerationModeoption in a separate PR, since it adds API and is independent of this one.🤖 Generated with Claude Code