Skip to content

Code generator regenerates all output on every edit, making builds slow for assemblies with many serializable types #11362

Open

Description

@koenbeuk

Summary

In an assembly with many [GenerateSerializer] types, OrleansSerializationSourceGenerator dominates build time, and a comment-only edit costs about as much as a full rebuild. The generator is an IIncrementalGenerator, but in practice its incremental pipeline saves nothing on edits: discovering types is incremental, but generating code is not.

On a synthetic 1,280-type grain-abstractions project, the generator takes 5.3s of an 8.2s CoreCompile, both on a rebuild and after a comment-only edit in a file that declares no Orleans types. With dotnet watch, every save pays this cost.

I'd like to open a series of small, separately measured PRs to improve this, and use this issue to track them and discuss the larger changes.

Measurements

Built with dotnet build against main (825aae7), compiler server warm. Each value is the median of 5 runs. Generator and analyzer times come from -p:ReportAnalyzer=true; analyzer times are summed across threads.

Workload Scenario Build CoreCompile Orleans generator Orleans analyzers
Large rebuild 10.44s 8.15s 5.30s 1.91s
Large comment edit 10.40s 8.15s 5.13s 1.9–3.9s
Small rebuild 3.75s 1.46s 0.78s 0.18s
Small comment edit 3.39s 1.19s 0.73s 0.16s

The two most expensive Orleans analyzers on the large rebuild are GrainInterfaceMethodReturnTypeDiagnosticAnalyzer (0.87s) and GenerateSerializationAttributesAnalyzer (0.83s).

The large workload generates 14.4 MB of source in 1,349 files: 10.0 MB of codecs (8 KB per type), 3.9 MB of proxies (about 9 KB per grain method, 488 KB for the 55-method interface) and a 519 KB metadata file.

Benchmark setup

  • Workloads. Two synthetic grain-abstractions projects with one file per type. The types are a mix of enums, [Immutable] records, mutable classes with collection and nested-DTO members, polymorphic hierarchies (abstract bases with derived types, plus many types derived from one shared base), generics and structs. Grain interfaces return Task/ValueTask of those types.
    • Large: 1,280 types, 64 grain interfaces, 433 grain methods, 1,371 files. Modelled on a production assembly with about 1,280 serializable types and 400 grain methods.
    • Small: 160 types, 8 interfaces, 97 methods.
  • Wiring. The projects reference Orleans from a local checkout, with the generator and analyzers added as analyzers the same way the Orleans repo wires its own projects. Comparing a branch with main is just a checkout and a re-run.
  • Scenarios.
    • Rebuild: --no-incremental --no-dependencies.
    • Comment edit: append a comment to a file with no Orleans types, then build incrementally.
  • Configuration. Release builds, AMD Ryzen 9 7900X (24 logical cores), Windows 11, .NET SDK 10.0.401.

What happens on a comment-only edit

Running the generator in-process with trackIncrementalGeneratorSteps and reusing the driver, as the IDE and dotnet watch do, shows what each step does after a comment-only edit on the large workload:

Step Runs Output
Orleans.SerializerOutputs 1 batch, all 1,283 types Unchanged
Orleans.SerializableTypeResults 1,283 all Unchanged
Orleans.PreparedProxyOutputs 1 batch, all interfaces Unchanged
Orleans.ReferenceAssemblyData 1 Unchanged
Orleans.MetadataOutputs – Cached

Unchanged means the step ran again and produced output equal to the last run. Only Cached means it was skipped. In this run, the first three steps together accounted for nearly all of the edit's generator time. Every expensive step does its full work and finds nothing changed:

  • Batch steps. SerializerOutputs and PreparedProxyOutputs are combined with CompilationProvider. Any edit creates a new Compilation, so they regenerate every codec and every proxy, then resolve each model back to symbols with TypeSymbolResolver.
  • Per-type transform. Roslyn re-runs the ForAttributeWithMetadataName transform for every matched type when the compilation changes, as expected. Each run builds a new LibraryTypes (about 100 GetTypeByMetadataName calls and the codec tables), so its cost grows linearly with the number of types.
  • ReferenceAssemblyData. It's driven by CompilationProvider, and ComputeAssembliesToExamine starts from compilation.Assembly. So it walks every declared type in the current assembly on every edit and re-extracts serializable-type models the per-type pipeline already produced. Because of this, an added member or grain method also rebuilds the metadata output.
  • Test gap. IncrementalCachingTests.UnrelatedChange_DoesNotTriggerRegeneration accepts Unchanged as well as Cached, so it passes even though every step re-ran.

Where the time goes

A CPU profile of the comment-edit case, using the EventPipe sampler, broke down as:

  • about 47% in GeneratedSourceOutput.CreateSourceString, i.e. NormalizeWhitespace() plus ToFullString() over the generated syntax trees
  • about 30% in LibraryTypes..ctor, nearly all in GetTypeByMetadataName
  • about 21% in proxy preparation, overlapping with the first item
  • a large share of samples waiting on GC; a single generator run allocates about 1.4 GB

For the analyzers:

  • GrainInterfaceMethodReturnTypeDiagnosticAnalyzer. It spends its time in InvokableBaseTypeResolver.HasUsableConstructor. Each check adds a speculative syntax tree to the full compilation, which re-merges its declaration table. The cache is keyed by the constructed base type (for example TaskRequest<SomeDto>), so the check repeats once per distinct grain-method return type. The generator's proxy step uses the same resolver.
  • GenerateSerializationAttributesAnalyzer. It uses GeneratedCodeAnalysisFlags.Analyze without ReportDiagnostics, so it binds the attributes of every generated class even though it can never report on generated code.

Proposed changes

Small, isolated fixes

Each of these would be its own PR with before/after numbers. All keep generated output byte-identical on the large workload.

  1. Cache LibraryTypes per Compilation (ConditionalWeakTable). Large workload: generator −36% on rebuild, −31% on edit.
  2. Skip generated code in GenerateSerializationAttributesAnalyzer (GeneratedCodeAnalysisFlags.None). No behaviour change, since it couldn't report there.
  3. Key the resolver's constructor-usability cache on OriginalDefinition. Whether base() or a [GeneratedActivatorConstructor] call binds doesn't depend on type arguments, and constraints are checked separately in SatisfiesConstraints. In-process measurement: the return-type analyzer dropped from about 4s to 0.1s on the large workload, and proxy preparation got faster too. This one changes semantics in edge cases, such as return types using private nested types, so it deserves a review on its own.

Larger changes

  1. Make code generation incremental per type. Resolve, inside the per-type transform, the facts each codec depends on: the codec for each member, whether it's shallow-copyable, the base codec, the activator. Store them in an equatable model, then generate from that model with no Compilation input, through per-item RegisterSourceOutput. Unchanged types would be Cached. A change to a type that affects another type's codec would still invalidate that type, because its resolved facts change. The metadata output already works this way. The same approach applies to proxies, with ownership of shared invokables decided in a small collected step.
  2. Emit text instead of building SyntaxFactory trees and calling NormalizeWhitespace. This follows naturally from 4, since the code writers would no longer need symbols. It's the largest remaining cost on cold builds, and it changes the formatting of generated code.
  3. Re-scan referenced assemblies only when references change, and stop re-extracting models for the assembly being compiled.
  4. Tighten the incremental caching tests so they assert Cached where steps should be skipped, rather than accepting Unchanged.
  5. Shrink generated output, longer term. For example, move proxy and invokable boilerplate into generic runtime base classes. That touches serialization hot paths and must not change the wire format, so it would need benchmarks and agreement first.

I'll start with the isolated fixes and link each PR here. For the larger items, especially 4 and 5, I'd like to agree a design here before writing code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions