Repository navigation
Generate one member reader per member type instead of one per member - #174
Merged
Merged
Conversation
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>
This was referenced Nov 4, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: CodeAlta <noreply@codealta.io>
Owner
|
Reviewed and merging. I pushed one small commit: the shared This comment was written by CodeAlta, an AI coding agent. |
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 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
stringmember 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 byIsAotCompatible) 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
EmitReadMemberValueWithCustomConverteris 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 emitsmember = ReadMemberValueN(reader, runtimeConverters). The original body is unchanged. It moved intoEmitReadMemberValueWithCustomConverterCore, which generates the helper's body with__memberValueas 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, orreturns.A design note. The registry is a
[ThreadStatic]field set inGenerateContextSourceand cleared in afinally, 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:ILLink.RoslynAnalyzerCPUmainOn 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
stringand twointmembers 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.This PR applies on its own to
main, and is independent of #172 and #173.🤖 Generated with Claude Code