Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Remove redundant allocations and SemanticModel fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Mar 13, 2021
1 parent df1259e commit 4b108fc
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public class ExplicitConversionCodeFixProvider : CodeFixProvider
public const string CS0029 = nameof(CS0029);
public const string CS0266 = nameof(CS0266);

public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create( CS0029, CS0266); }
}
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(CS0029, CS0266);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down Expand Up @@ -96,7 +93,8 @@ private async Task<Document> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InvocationScaffoldingCodeFixProvider : CodeFixProvider
{
public const string CS7036 = nameof(CS7036);

public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS7036);
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(CS7036);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SplattingCodeFixProvider : CodeFixProvider
public const string CS7036 = nameof(CS7036);
public const string CS1501 = nameof(CS1501);

public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS7036, CS1501);
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(CS7036, CS1501);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class UseLocalVariablesAsParameterCodeFixProvider : CodeFixProvider
/// </summary>
public const string CS1729 = nameof(CS1729);

public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS1501, CS7036, CS1729);
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(CS1501, CS7036, CS1729);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ private static async Task<Document> ReplaceEmptyInitializationBlock(Document doc
{
var oldObjCreation = objectInitializer.FindContainer<ObjectCreationExpressionSyntax>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private static async Task<Document> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class MappingEngine
{
protected readonly SemanticModel semanticModel;
protected readonly SyntaxGenerator syntaxGenerator;



public MappingEngine(SemanticModel semanticModel, SyntaxGenerator syntaxGenerator)
{
this.semanticModel = semanticModel;
Expand All @@ -30,13 +29,6 @@ public TypeInfo GetExpressionTypeInfo(SyntaxNode expression)
return semanticModel.GetTypeInfo(expression);
}

public static async Task<MappingEngine> 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<ExpressionSyntax> MapExpression(ExpressionSyntax sourceExpression, AnnotatedType sourceType, AnnotatedType destinationType, MappingContext mappingContext)
{
var mappingSource = new MappingElement
Expand Down

0 comments on commit 4b108fc

Please sign in to comment.