From 4b108fc954d70d62c551471d0d72743a4d3330ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Pi=C4=85tek?= Date: Sat, 13 Mar 2021 22:00:09 +0100 Subject: [PATCH] Remove redundant allocations and SemanticModel fetching --- .../CodeFixes/ExplicitConversionCodeFixProvider.cs | 8 +++----- .../CodeFixes/InvocationScaffoldingCodeFixProvider.cs | 2 +- .../Features/CodeFixes/SplattingCodeFixProvider.cs | 2 +- .../UseLocalVariablesAsParameterCodeFixProvider.cs | 2 +- .../EmptyInitializationBlockRefactoring.cs | 3 ++- .../Refactorings/UpdateLambdaParameterRefactoring.cs | 3 ++- .../MappingGenerator/Mappings/MappingEngine.cs | 10 +--------- 7 files changed, 11 insertions(+), 19 deletions(-) diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/ExplicitConversionCodeFixProvider.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/ExplicitConversionCodeFixProvider.cs index ac5c97a..fc1f8c2 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/ExplicitConversionCodeFixProvider.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/ExplicitConversionCodeFixProvider.cs @@ -24,10 +24,7 @@ public class ExplicitConversionCodeFixProvider : CodeFixProvider public const string CS0029 = nameof(CS0029); public const string CS0266 = nameof(CS0266); - public sealed override ImmutableArray FixableDiagnosticIds - { - get { return ImmutableArray.Create( CS0029, CS0266); } - } + public sealed override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(CS0029, CS0266); public sealed override FixAllProvider GetFixAllProvider() { @@ -96,7 +93,8 @@ private async Task GenerateExplicitConversion(Document document, Assig private static async Task<(MappingEngine, SemanticModel)> CreateMappingEngine(Document document, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); - var mappingEngine = await MappingEngine.Create(document, cancellationToken).ConfigureAwait(false); + var syntaxGenerator = SyntaxGenerator.GetGenerator(document); + var mappingEngine = new MappingEngine(semanticModel, syntaxGenerator); return (mappingEngine, semanticModel); } diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/InvocationScaffoldingCodeFixProvider.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/InvocationScaffoldingCodeFixProvider.cs index 97895fe..0256740 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/InvocationScaffoldingCodeFixProvider.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/InvocationScaffoldingCodeFixProvider.cs @@ -19,7 +19,7 @@ public class InvocationScaffoldingCodeFixProvider : CodeFixProvider { public const string CS7036 = nameof(CS7036); - public sealed override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(CS7036); + public sealed override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(CS7036); public sealed override FixAllProvider GetFixAllProvider() { diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/SplattingCodeFixProvider.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/SplattingCodeFixProvider.cs index 0a61c2e..f0b7c5b 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/SplattingCodeFixProvider.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/SplattingCodeFixProvider.cs @@ -24,7 +24,7 @@ public class SplattingCodeFixProvider : CodeFixProvider public const string CS7036 = nameof(CS7036); public const string CS1501 = nameof(CS1501); - public sealed override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(CS7036, CS1501); + public sealed override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(CS7036, CS1501); public sealed override FixAllProvider GetFixAllProvider() { diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/UseLocalVariablesAsParameterCodeFixProvider.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/UseLocalVariablesAsParameterCodeFixProvider.cs index ba198a9..2cca633 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/UseLocalVariablesAsParameterCodeFixProvider.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/CodeFixes/UseLocalVariablesAsParameterCodeFixProvider.cs @@ -68,7 +68,7 @@ public class UseLocalVariablesAsParameterCodeFixProvider : CodeFixProvider /// public const string CS1729 = nameof(CS1729); - public sealed override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(CS1501, CS7036, CS1729); + public sealed override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(CS1501, CS7036, CS1729); public sealed override FixAllProvider GetFixAllProvider() { diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/EmptyInitializationBlockRefactoring.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/EmptyInitializationBlockRefactoring.cs index 0c3bfe1..80b38d7 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/EmptyInitializationBlockRefactoring.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/EmptyInitializationBlockRefactoring.cs @@ -151,7 +151,8 @@ private static async Task ReplaceEmptyInitializationBlock(Document doc { var oldObjCreation = objectInitializer.FindContainer(); var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, oldObjCreation).Type; - var mappingEngine = await MappingEngine.Create(document, cancellationToken).ConfigureAwait(false); + var syntaxGenerator = SyntaxGenerator.GetGenerator(document); + var mappingEngine = new MappingEngine(semanticModel, syntaxGenerator); var mappingContext = new MappingContext(objectInitializer, semanticModel ); var newObjectCreation = await mappingEngine.AddInitializerWithMappingAsync(oldObjCreation, mappingMatcher, createdObjectType, mappingContext).ConfigureAwait(false); diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/UpdateLambdaParameterRefactoring.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/UpdateLambdaParameterRefactoring.cs index ed6e5a7..d4a15f4 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/UpdateLambdaParameterRefactoring.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Features/Refactorings/UpdateLambdaParameterRefactoring.cs @@ -67,7 +67,8 @@ private static async Task ReplaceWithMappingBody(Document document, La { var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(lambda, cancellationToken).Symbol; var createdObjectType = methodSymbol.Parameters.First().Type; - var mappingEngine = await MappingEngine.Create(document, cancellationToken).ConfigureAwait(false); + var syntaxGenerator = SyntaxGenerator.GetGenerator(document); + var mappingEngine = new MappingEngine(semanticModel, syntaxGenerator); var mappingContext = new MappingContext(lambda, semanticModel); var mappingTargetHelper = new MappingTargetHelper(); var propertiesToSet = mappingTargetHelper.GetFieldsThaCanBeSetPublicly(createdObjectType, mappingContext); diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs index 34a637b..d86c0aa 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs @@ -17,8 +17,7 @@ public class MappingEngine { protected readonly SemanticModel semanticModel; protected readonly SyntaxGenerator syntaxGenerator; - - + public MappingEngine(SemanticModel semanticModel, SyntaxGenerator syntaxGenerator) { this.semanticModel = semanticModel; @@ -30,13 +29,6 @@ public TypeInfo GetExpressionTypeInfo(SyntaxNode expression) return semanticModel.GetTypeInfo(expression); } - public static async Task Create(Document document, CancellationToken cancellationToken) - { - var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); - var syntaxGenerator = SyntaxGenerator.GetGenerator(document); - return new MappingEngine(semanticModel, syntaxGenerator); - } - public async Task MapExpression(ExpressionSyntax sourceExpression, AnnotatedType sourceType, AnnotatedType destinationType, MappingContext mappingContext) { var mappingSource = new MappingElement