Skip to content

Generate one member reader per type and move merge-key handling into the runtime - #172

Merged
xoofx merged 3 commits into
xoofx:mainfrom
Mpdreamz:perf/dedupe-generated-member-readers
Nov 4, 2026
Merged

xoofx merged 3 commits into
xoofx:mainfrom
Mpdreamz:perf/dedupe-generated-member-readers

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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.ReadMergeKeys instead 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 by IsAotCompatible (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 ApplyMergeMapping for <<. It also wrote ReadAndApplyMerge and ApplyMergeMapping per type, about 100 lines each time.

What

One ReadMember(string key) per type

Both 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 the matched flag already makes it a no-op.

YamlReader.ReadMergeKeys(bool mergeEnabled, HashSet<string>? explicitKeys)

The merge logic moved here unchanged, including the three YamlException messages. It is an iterator that yields each merged key, with the reader positioned on that key's value, and the generated reader calls ReadMember for it in a foreach. It is documented as intended for generated code, like IsDiscriminatorMember and BeginUnmappedMember.

An iterator and not an Action<string> is deliberate. My first version took ReadMember as 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, PreferredObjectCreationHandling pinned, cold compile, alternating runs:

Generated code ILLink.RoslynAnalyzer CPU
before 3.31 MB, 64.6k lines ~94 s
this PR 2.40 MB, 48.7k lines ~35 s

The 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 on main, so they describe existing behaviour. I mutated the merge call in each path, the explicit-key check and ReadMergeKeys, and each mutation fails tests.

cd src
dotnet test SharpYaml.Tests -c Release

I kept the GenerationMode option in a separate PR, since it adds API and is independent of this one.

🤖 Generated with Claude Code

Mpdreamz and others added 2 commits November 4, 2026 17:01
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>
`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>
@xoofx
xoofx merged commit 8f11764 into xoofx:main Nov 4, 2026
1 check passed
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