From 31701d782472194c739e5c202516e91e25e573d5 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Sat, 22 Apr 2023 16:04:39 +0200 Subject: [PATCH] perf: Use `StringBuilderBasedSourceText` in `DependencyObjectAvailabilityGenerator` --- .../DependencyObjectAvailabilityGenerator.cs | 31 ++++++------------- .../StringBuilderBasedSourceText.cs | 2 +- .../XamlGenerator/XamlCodeGeneration.cs | 2 +- .../XamlGenerator/XamlFileGenerator.cs | 28 ++++++++--------- 4 files changed, 25 insertions(+), 38 deletions(-) rename src/SourceGenerators/Uno.UI.SourceGenerators/{XamlGenerator/Utils => }/StringBuilderBasedSourceText.cs (95%) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/BindableTypeProviders/DependencyObjectAvailabilityGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/BindableTypeProviders/DependencyObjectAvailabilityGenerator.cs index 869ed0aa76c9..e0e7ca801032 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/BindableTypeProviders/DependencyObjectAvailabilityGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/BindableTypeProviders/DependencyObjectAvailabilityGenerator.cs @@ -1,15 +1,16 @@ #nullable enable -using Uno.Extensions; using System; using System.Collections.Generic; -using System.Linq; using System.IO; +using System.Linq; +using System.Xml; using Microsoft.CodeAnalysis; +using Uno.Extensions; using Uno.Roslyn; -using Uno.UI.SourceGenerators.XamlGenerator; using Uno.UI.SourceGenerators.Helpers; -using System.Xml; +using Uno.UI.SourceGenerators.Utils; +using Uno.UI.SourceGenerators.XamlGenerator; #if NETFRAMEWORK using Uno.SourceGeneration; @@ -32,7 +33,7 @@ public void Execute(GeneratorExecutionContext context) new Generator().Generate(context); } - class Generator + private sealed class Generator { private string? _defaultNamespace; private string? _projectFullPath; @@ -98,19 +99,6 @@ from type in module.GlobalNamespace.GetNamespaceTypes() context.AddSource("DependencyObjectAvailability", GenerateTypeProviders(propertyNames)); -#if DEBUG - context.AddSource("DependencyObjectAvailability_Debug", - $""" - /* - _assemblyName:{_assemblyName} - _dependencyObjectSymbol:{_dependencyObjectSymbol} - modules: {string.Join(", ", modules)} - _additionalLinkerHintAttributeSymbol: {_additionalLinkerHintAttributeSymbol} - additionalLinkerHintSymbols: {string.Join(",", additionalLinkerHintSymbols)} - */ - """); -#endif - GenerateLinkerSubstitutionDefinition(propertyNames); } } @@ -234,18 +222,17 @@ private void GenerateLinkerSubstitutionDefinition(string[] propertyNames) doc.Save(fileName); } - private string GenerateTypeProviders(string[] propertyNames) + private StringBuilderBasedSourceText GenerateTypeProviders(string[] propertyNames) { var writer = new IndentedStringBuilder(); writer.AppendLineIndented("// "); writer.AppendLineIndented("// *****************************************************************************"); - writer.AppendLineIndented("// This file has been generated by Uno.UI (BindableTypeProvidersSourceGenerator)"); + writer.AppendLineIndented("// This file has been generated by Uno.UI (DependencyObjectAvailabilityGenerator)"); writer.AppendLineIndented("// *****************************************************************************"); writer.AppendLineIndented("// "); writer.AppendLine(); writer.AppendLineIndented("#pragma warning disable 618 // Ignore obsolete members warnings"); - writer.AppendLineIndented("#pragma warning disable 1591 // Ignore missing XML comment warnings"); writer.AppendLineIndented($"// _intermediatePath: {_intermediatePath}"); writer.AppendLineIndented($"// _intermediateOutputPath: {_intermediateOutputPath}"); @@ -261,7 +248,7 @@ private string GenerateTypeProviders(string[] propertyNames) } } - return writer.ToString(); + return new StringBuilderBasedSourceText(writer.Builder); } } } diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/StringBuilderBasedSourceText.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/StringBuilderBasedSourceText.cs similarity index 95% rename from src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/StringBuilderBasedSourceText.cs rename to src/SourceGenerators/Uno.UI.SourceGenerators/StringBuilderBasedSourceText.cs index a1d9b0d2ef6a..848cfa2a611a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/StringBuilderBasedSourceText.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/StringBuilderBasedSourceText.cs @@ -1,7 +1,7 @@ using System.Text; using Microsoft.CodeAnalysis.Text; -namespace Uno.UI.SourceGenerators.XamlGenerator.Utils; +namespace Uno.UI.SourceGenerators.Utils; /// /// A SourceText implementation that is more performant for cases when we already have a StringBuilder. diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs index 161ce51ba4b9..bde771d2faa9 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs @@ -22,7 +22,7 @@ using System.Collections.Concurrent; using System.Collections.Immutable; using Uno.UI.SourceGenerators.Helpers; -using Uno.UI.SourceGenerators.XamlGenerator.Utils; +using Uno.UI.SourceGenerators.Utils; #if NETFRAMEWORK using Microsoft.Build.Execution; diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 0a8ee4e1019b..2ae93d17dfb5 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1,29 +1,29 @@ #nullable enable -using Uno.Extensions; -using Uno.MsBuildTasks.Utils; -using Uno.UI.SourceGenerators.XamlGenerator.Utils; using System; using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; using System.Text.RegularExpressions; using Microsoft.CodeAnalysis; -using Uno.Roslyn; -using Uno.UI.SourceGenerators.XamlGenerator.XamlRedirection; -using System.Runtime.CompilerServices; -using Uno.UI.Xaml; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Text; using Uno.Disposables; +using Uno.Extensions; +using Uno.MsBuildTasks.Utils; +using Uno.Roslyn; using Uno.UI.SourceGenerators.BindableTypeProviders; -using Microsoft.CodeAnalysis.CSharp; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using Uno.UI.SourceGenerators.Helpers; -using System.Collections.Immutable; -using System.Text; -using Microsoft.CodeAnalysis.Text; - +using Uno.UI.SourceGenerators.Utils; +using Uno.UI.SourceGenerators.XamlGenerator.Utils; +using Uno.UI.SourceGenerators.XamlGenerator.XamlRedirection; +using Uno.UI.Xaml; #if NETFRAMEWORK using Uno.SourceGeneration;