Skip to content

Add YamlSourceGenerationOptions.GenerationMode to skip writers or readers - #173

Merged
xoofx merged 1 commit into
xoofx:mainfrom
Mpdreamz:feat/generation-mode
Nov 4, 2026
Merged

xoofx merged 1 commit into
xoofx:mainfrom
Mpdreamz:feat/generation-mode

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

A context that only reads YAML can now skip the writers, and one that only writes can skip the readers, through a new GenerationMode option. 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

YamlSourceGenerationMode and YamlSourceGenerationOptions.GenerationMode

It follows System.Text.Json's JsonSourceGenerationMode. It is a [Flags] enum with Default (both directions, as today), Serialization and Deserialization. Serialization | Deserialization also means both.

Skipped direction

The generator keeps the method signature of the skipped direction and replaces its body with a NotSupportedException that names the mode. Nested calls such as WriteValueN inside other writers still compile, and calling the skipped direction fails with a clear message. The default output is unchanged.

Docs

site/docs/source-generation.md gets a short section on keeping generated code small. It also notes that leaving PreferredObjectCreationHandling unset makes the generator emit both the Populate and the Replace reader for every member, so pinning it to Replace (the default) halves the output for contexts that never populate.

Results

In the same downstream project, with GenerationMode = Deserialization on top of #172: 2.40 MB to 1.14 MB of generated code, and ILLink.RoslynAnalyzer CPU 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 NotSupportedException cases. Three generate source in memory and assert that Deserialization has no WriteMembers, Serialization has no ReadObjectCore, and Default emits 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.

cd src
dotnet test SharpYaml.Tests -c Release

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

🤖 Generated with Claude Code

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>
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