Repository navigation
Add YamlSourceGenerationOptions.GenerationMode to skip writers or readers - #173
Merged
Merged
Conversation
A context that only reads YAML still got a writer for every type, about half of the generated code. `YamlSourceGenerationOptions.GenerationMode` takes a `YamlSourceGenerationMode` (`Default`, `Serialization`, `Deserialization`). The skipped direction keeps its method signature and throws `NotSupportedException`, so nested calls still compile. The default is unchanged. Adds runtime tests, generated-source tests for each mode, and a docs section on keeping generated code small. 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.
A context that only reads YAML can now skip the writers, and one that only writes can skip the readers, through a new
GenerationModeoption. In a read-heavy downstream project the writers were about half of the generated code.Why
The generator always emits both a reader and a writer for every type. Many contexts only deserialize configuration files, so the writers (
WriteMembers,WriteValue) are dead code that still has to be compiled and analyzed, including by the trimming and AOT analyzers. In a downstream context with 60 types, the writers were 26,400 of 64,600 generated lines.What
YamlSourceGenerationModeandYamlSourceGenerationOptions.GenerationModeIt follows
System.Text.Json'sJsonSourceGenerationMode. It is a[Flags]enum withDefault(both directions, as today),SerializationandDeserialization.Serialization | Deserializationalso means both.Skipped direction
The generator keeps the method signature of the skipped direction and replaces its body with a
NotSupportedExceptionthat names the mode. Nested calls such asWriteValueNinside other writers still compile, and calling the skipped direction fails with a clear message. The default output is unchanged.Docs
site/docs/source-generation.mdgets a short section on keeping generated code small. It also notes that leavingPreferredObjectCreationHandlingunset makes the generator emit both thePopulateand theReplacereader for every member, so pinning it toReplace(the default) halves the output for contexts that never populate.Results
In the same downstream project, with
GenerationMode = Deserializationon top of #172: 2.40 MB to 1.14 MB of generated code, andILLink.RoslynAnalyzerCPU from about 35 s to about 25 s (cold compile, wall time 11 s to 9 s). Without #172, expect a smaller effect on analyzer time.Tests
Adds 9 tests. Six run generated contexts for each mode: reading objects, lists, dictionaries and constructor types, writing, both flags together, and the two
NotSupportedExceptioncases. Three generate source in memory and assert thatDeserializationhas noWriteMembers,Serializationhas noReadObjectCore, andDefaultemits both. The in-memory helper also fails the test if the generated code does not compile. Forcing either direction to always emit fails two tests each.This PR is independent of #172 and applies on its own to
main.🤖 Generated with Claude Code