Roslyn Source Generators: Reduction of Resource Consumption in IDEs – Part 9

In this article we will see how to reduce the resource consumption of a Source Generator when running inside an IDE by redirecting the code generation to RegisterImplementationSourceOutput.

In this article:

pg
Pawel Gerr is architect consultant at Thinktecture. He focuses on backends with .NET Core and knows Entity Framework inside out.

More information about the Smart Enums and the source code can be found on GitHub:

After a detour to the new high-level in previos article (TODO: link), we will see how to reduce the resource consumption of a Source Generator when running inside an IDE by redirecting the code generation to RegisterImplementationSourceOutput.

Roslyn API: RegisterImplementationSourceOutput

With newer compiler versions, Roslyn team introduced the method RegisterImplementationSourceOutput to give developers a means to split the code generation in 2 categories: the code that can be used during development directly and the code which is important during runtime of the application only. If the code must be able to be referenced by the developers, like property Items generated in the first part of this series, then continue using RegisterSourceOutput, otherwise use RegisterImplementationSourceOutput. An IDE has a choice whether to run the pipeline registered with RegisterImplementationSourceOutput during development in the background or to skip the code generation until build (of the assembly) as a performance optimization.

At the time of writing, as I can see, the IDEs doesn’t treat RegisterImplementationSourceOutput any different than RegisterSourceOutput. But this should change in the future to be able to handle growing number of Roslyn Source Generators.

Pipeline for Generation of Translations

The TranslationAttribute, generated in the part 7, is nothing that can be used by a developer, so, it is a perfect candidate for RegisterImplementationSourceOutput.

Everything that is related to translations is moved to a new generation pipeline in InitializeTranslationsGenerator. The pipeline became more fine grained to leverage the efficiency of memoization described in part 4.

				
					   public void Initialize(IncrementalGeneratorInitializationContext context) 
   { 
      var enumTypes = context.SyntaxProvider 
                             .ForAttributeWithMetadataName("DemoLibrary.EnumGenerationAttribute", 
                                                           CouldBeEnumerationAsync, 
                                                           GetEnumInfoOrNull) 
                             .Collect() 
                             .SelectMany((enumInfos, _) => enumInfos.Distinct());
      ... 
 
      InitializeTranslationsGenerator(context, enumTypes); 
   } 
   
   private static void InitializeTranslationsGenerator( 
      IncrementalGeneratorInitializationContext context, 
      IncrementalValuesProvider<DemoEnumInfo> enumTypes) 
   {
      // fetch namespace and name only, so the generation is not triggered anew,
      // if something else (unimportant) changes.
      var enumNames = enumTypes.Select((t, _) => (t.Namespace, t.Name)); 
 
      var mergedTranslations = context.AdditionalTextsProvider 
            .Where(text => text.Path.EndsWith("translations.json",
                                              StringComparison.OrdinalIgnoreCase)) 
            .Select((text, token) => text.GetText(token)?.ToString())
            // Parse JSON before generation
            .Select((json, _) => ParseTranslations(json)) 
            .Where(translations => !translations.IsEmpty)
            .Collect() 
            // Merge translations from different JSON files
            .Select(MergeTranslations); 
 
      var translationInfos = enumNames.Combine(mergedTranslations) 
            .SelectMany((tuple, _) => 
            { 
                var (ns, name) = tuple.Left; 
                var translationsByClassName = tuple.Right; 
 
                // search for the translations of current smart enum,
                // it there are none, then don't generate anything
                if (!translationsByClassName.TryGetValue(name, out var translations)) 
                    return ImmutableArray<EnumTranslationInfo>.Empty; 
 
                var translationInfo = new EnumTranslationInfo(ns, name, translations); 
                return ImmutableArray.Create(translationInfo); 
           }); 
 
      context.RegisterImplementationSourceOutput(translationInfos, GenerateCode); 
   }
				
			
The type EnumTranslationInfo implements Equals and GetHashCode for proper caching.
				
					public readonly struct EnumTranslationInfo : IEquatable<EnumTranslationInfo> 
{ 
   public string? Namespace { get; } 
   public string Name { get; } 
   public ImmutableDictionary<string, string> Translations { get; } 
 
   public EnumTranslationInfo( 
      string? ns, 
      string name, 
      ImmutableDictionary<string, string> translations) 
   { 
      Namespace = ns; 
      Name = name; 
      Translations = translations; 
   } 
 
   // Equals and GetHashCode
} 

				
			
Parsing of the JSON file is virtually the same but is using ImmutableDictionary to be able to return empty dictionaries without creation of new instances.
				
					   private static ImmutableDictionary<string, ImmutableDictionary<string, string>> ParseTranslations(string? json) 
   { 
      if (String.IsNullOrWhiteSpace(json)) 
         return ImmutableDictionary<string, ImmutableDictionary<string, string>>.Empty; 
 
      try 
      { 
         return JsonConvert.DeserializeObject<ImmutableDictionary<
                                            string, ImmutableDictionary<string, string>>>(json!) 
                ?? ImmutableDictionary<string, ImmutableDictionary<string, string>>.Empty; 
      } 
      catch (Exception) 
      { 
         return ImmutableDictionary<string, ImmutableDictionary<string, string>>.Empty; 
      } 
   } 
				
			

The source generator must merge the translations if there are multiple JSON files with translations.

				
					   private static ImmutableDictionary<string, ImmutableDictionary<string, string>> MergeTranslations( 
      ImmutableArray<ImmutableDictionary<string, ImmutableDictionary<string, string>>>
                        collectedTranslations, 
      CancellationToken cancellationToken) 
   { 
      if (collectedTranslations.IsDefaultOrEmpty) 
         return ImmutableDictionary<string, ImmutableDictionary<string, string>>.Empty; 
 
      if (collectedTranslations.Length == 1) 
         return collectedTranslations[0]; 
 
      var mergedTranslations = ImmutableDictionary<string,
                                                   ImmutableDictionary<string, string>>.Empty; 
 
      foreach (var translationsByClassName in collectedTranslations) 
      { 
         cancellationToken.ThrowIfCancellationRequested(); 
 
         foreach (var kvp in translationsByClassName) 
         { 
            try 
            { 
               var className = kvp.Key; 
               var translationByLanguage = kvp.Value; 
 
               if (mergedTranslations.TryGetValue(className, out var otherTranslations)) 
                  translationByLanguage = translationByLanguage.AddRange(otherTranslations); 
 
               mergedTranslations = mergedTranslations.SetItem(className, translationByLanguage); 
            } 
            catch (Exception) 
            { 
               // Report the error 
            } 
         } 
      } 
 
      return mergedTranslations; 
   } 
				
			
The actual code generation has been moved out of DemoCodeGenerator to EnumTranslationsGenerator.
				
					   private static void GenerateCode(
      SourceProductionContext context,
      EnumTranslationInfo translationInfo)
   {
      var ns = translationInfo.Namespace is null ? null : $"{translationInfo.Namespace}.";
      var code = EnumTranslationsGenerator.Instance.Generate(translationInfo);

      if (!String.IsNullOrWhiteSpace(code))
         context.AddSource($"{ns}{translationInfo.Name}.Translations.g.cs", code);
   }
   
// -------------------------------
 
public class EnumTranslationsGenerator 
{ 
   public static readonly EnumTranslationsGenerator Instance = new(); 
 
   public string Generate(EnumTranslationInfo translationsInfo) 
   { 
      var ns = translationsInfo.Namespace; 
      var name = translationsInfo.Name; 
 
      var sb = new StringBuilder(@$"// <auto-generated /> 
#nullable enable 
 
{(ns is null ? null : $@"namespace {ns}; 
")}"); 
 
      GenerateTranslationAttributes(sb, translationsInfo.Translations); 
 
      sb.Append(@$" 
partial class {name} 
{{ 
}} 
"); 
 
      return sb.ToString(); 
   } 
 
   private static void GenerateTranslationAttributes( 
      StringBuilder sb, 
      IReadOnlyDictionary<string, string> translations) 
   { 
      foreach (var kvp in translations.OrderBy(kvp => kvp.Key)) 
      { 
         sb.Append(@" 
[global::DemoLibrary.TranslationAttribute(""").Append(kvp.Key).Append("\", \"").Append(kvp.Value).Append("\")]"); 
      } 
   } 
} 

				
			

Summary

Even if this feature is not supported (by all) IDEs yet. I recommend start using it now, so our IDEs stay responsive with growing number of Roslyn Source Generators as soon as RegisterImplementationSourceOutput is treated properly.
Free
Newsletter

Current articles, screencasts and interviews by our experts

Don’t miss any content on Angular, .NET Core, Blazor, Azure, and Kubernetes and sign up for our free monthly dev newsletter.

EN Newsletter Anmeldung (#7)
Related Articles
AI
sg
One of the more pragmatic ways to get going on the current AI hype, and to get some value out of it, is by leveraging semantic search. This is, in itself, a relatively simple concept: You have a bunch of documents and want to find the correct one based on a given query. The semantic part now allows you to find the correct document based on the meaning of its contents, in contrast to simply finding words or parts of words in it like we usually do with lexical search. In our last projects, we gathered some experience with search bots, and with this article, I'd love to share our insights with you.
17.05.2024
Angular
SL-rund
If you previously wanted to integrate view transitions into your Angular application, this was only possible in a very cumbersome way that needed a lot of detailed knowledge about Angular internals. Now, Angular 17 introduced a feature to integrate the View Transition API with the router. In this two-part series, we will look at how to leverage the feature for route transitions and how we could use it for single-page animations.
15.04.2024
.NET
KP-round
.NET 8 brings Native AOT to ASP.NET Core, but many frameworks and libraries rely on unbound reflection internally and thus cannot support this scenario yet. This is true for ORMs, too: EF Core and Dapper will only bring full support for Native AOT in later releases. In this post, we will implement a database access layer with Sessions using the Humble Object pattern to get a similar developer experience. We will use Npgsql as a plain ADO.NET provider targeting PostgreSQL.
15.11.2023