C# source generator performance has real impact on developer experience — every slow generator adds cost to every build and every IDE keystroke. This covers how to measure generator overhead using MSBuild binary logs, the ReportAnalyzer property, and dotnet-trace CPU profiling. The core optimization model is the incremental source generator pipeline (IIncrementalGenerator), which caches pipeline stage outputs using value equality. Key techniques include: implementing IEquatable<T> on all data models (with an EquatableArray<T> wrapper for ImmutableArray collections), using ForAttributeWithMetadataName instead of CreateSyntaxProvider for attribute-driven generators, keeping predicates to cheap syntax checks only, using RegisterImplementationSourceOutput to skip IDE-unnecessary code generation at design time, and marking lambdas as static to prevent accidental captures. Common anti-patterns are capturing the Compilation object (causes cache miss on every edit), expensive predicate logic, and missing equality implementations. A practical benchmarking approach using a representative consuming project with ReportAnalyzer and binlog comparison is also described.

18m read timeFrom devleader.ca
Post cover image
Table of contents
Why Source Generator Performance MattersThe Two Performance Axes: Build Time and RuntimeHow the Incremental Source Generator Model Solves PerformanceMeasuring Source Generator Build PerformanceThe Cache Hit Rate: The Most Important MetricCommon Source Generator Performance Anti-PatternsSource Generator Optimization TechniquesSource Generator IDE ResponsivenessBenchmarking Your Source Generator ChangesFAQConclusion

Sort: