Skip to content

Generate one member reader per member type instead of one per member - #174

Merged
xoofx merged 2 commits into
xoofx:mainfrom
Mpdreamz:perf/share-member-value-readers
Nov 4, 2026
Merged

xoofx merged 2 commits into
xoofx:mainfrom
Mpdreamz:perf/share-member-value-readers

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

The source generator now writes one ReadMemberValueN(reader, runtimeConverters) method per distinct member type, and every member of that type calls it. The emitted logic is the same, so deserialization behaves identically.

Why

Every member of a generated object inlined its own value reader: the runtime converter lookup, the token and null checks, and the scalar or collection read. A plain string member is 26 lines, and a downstream context with 124 string members repeated that block 124 times. Once type and member names are normalised, 97% of the generated lines are repeats of about 1,800 distinct lines.

That code is what the trimming analyzer (ILLink.RoslynAnalyzer, enabled by IsAotCompatible) spends its time on, and the cost is far larger than the line count suggests. In a downstream project (docs-builder, 14 [YamlSerializable] roots, 271 members) it made one project take minutes to compile.

What

EmitReadMemberValueWithCustomConverter is the one emitter every assignable member goes through, in the plain, constructor and populate paths. It now registers a helper keyed by the member type and the attribute converter, and emits member = ReadMemberValueN(reader, runtimeConverters). The original body is unchanged. It moved into EmitReadMemberValueWithCustomConverterCore, which generates the helper's body with __memberValue as the assignment target. The helpers are written once at the end of the context.

The read only depends on the member type, the reader, the converters and the options, so sharing is safe: it never reads the member name, instance, or returns.

A design note. The registry is a [ThreadStatic] field set in GenerateContextSource and cleared in a finally, because the member emitters are about eight nested methods deep and threading a parameter through all of them would touch every signature. The emitter runs synchronously on one thread per context. I am happy to pass the registry explicitly instead if you prefer that.

Results

The same downstream project, cold compile, alternating runs, this change on its own against main:

Generated code ILLink.RoslynAnalyzer CPU Project wall time
main 3.31 MB, 64.6k lines ~99–118 s 17–19 s
this PR 2.42 MB, 48.8k lines ~27–30 s 9 s

On top of #172 it is about 13–16 s and 8 s, which is close to the floor I measured for that project (about 6 s and 8 s with an almost empty context). 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 a generated-source test: two types with three string and two int members produce exactly one shared reader per type, and every member calls it. Replacing the helper's return value breaks 99 existing tests, and keying every type to one reader fails the build of the test contexts, so the existing generated-context tests cover the behaviour.

cd src
dotnet test SharpYaml.Tests -c Release

This PR applies on its own to main, and is independent of #172 and #173.

🤖 Generated with Claude Code

Every member of a generated object inlined its own value reader: the converter lookup, the token and null checks, and the scalar or collection read. A plain `string` member took 26 lines, and a context with 124 of them repeated the same block 124 times.

The reader only depends on the member type, so the generator now writes one `ReadMemberValueN(reader, runtimeConverters)` per distinct type and each member assigns its result. The emitted logic is the same, so deserialization behaves identically.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
@xoofx

xoofx commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Reviewed and merging. I pushed one small commit: the shared ReadMemberValueN no longer initializes __memberValue with default!, so the compiler's definite-assignment check proves that every path of a shared body assigns the value. A path that did not assign would otherwise silently overwrite a member's initializer with default, which the inlined reader never did. All generated test contexts compile with it.

This comment was written by CodeAlta, an AI coding agent.

@xoofx
xoofx merged commit b360767 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