Skip to content

Commit

Permalink
Merge pull request #1689 from sharwell/reduce-allocations
Browse files Browse the repository at this point in the history
Reduce allocations in HandleCompilationStart
  • Loading branch information
sharwell committed Oct 25, 2015
2 parents 49b706d + b008586 commit cd1d898
Show file tree
Hide file tree
Showing 98 changed files with 571 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal class SA1119StatementMustNotUseUnnecessaryParenthesis : DiagnosticAnaly
new DiagnosticDescriptor(ParenthesesDiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Hidden, AnalyzerConstants.EnabledByDefault, Description, HelpLink, customTags: new[] { WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.NotConfigurable });

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> ParenthesizedExpressionAction = HandleParenthesizedExpression;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -85,7 +86,7 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
// is disabled
if (context.Compilation.Options.SpecificDiagnosticOptions.GetValueOrDefault(Descriptor.Id) != Microsoft.CodeAnalysis.ReportDiagnostic.Suppress)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleParenthesizedExpression, SyntaxKind.ParenthesizedExpression);
context.RegisterSyntaxNodeActionHonorExclusions(ParenthesizedExpressionAction, SyntaxKind.ParenthesizedExpression);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ internal class SA1400AccessModifierMustBeDeclared : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<SyntaxKind> BaseTypeDeclarationKinds =
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.EnumDeclaration);

private static readonly ImmutableArray<SyntaxKind> BaseFieldDeclarationKinds =
ImmutableArray.Create(SyntaxKind.EventFieldDeclaration, SyntaxKind.FieldDeclaration);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> BaseTypeDeclarationAction = HandleBaseTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> EventDeclarationAction = HandleEventDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> PropertyDeclarationAction = HandlePropertyDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> BaseFieldDeclarationAction = HandleBaseFieldDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> ConstructorDeclarationAction = HandleConstructorDeclaration;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -50,33 +64,29 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseTypeDeclarationSyntax, SyntaxKind.ClassDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseTypeDeclarationSyntax, SyntaxKind.InterfaceDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseTypeDeclarationSyntax, SyntaxKind.EnumDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseTypeDeclarationSyntax, SyntaxKind.StructDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleDelegateDeclarationSyntax, SyntaxKind.DelegateDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleEventDeclarationSyntax, SyntaxKind.EventDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMethodDeclarationSyntax, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandlePropertyDeclarationSyntax, SyntaxKind.PropertyDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseFieldDeclarationSyntax, SyntaxKind.EventFieldDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleBaseFieldDeclarationSyntax, SyntaxKind.FieldDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleIndexerDeclarationSyntax, SyntaxKind.IndexerDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(HandleConstructorDeclarationSyntax, SyntaxKind.ConstructorDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
context.RegisterSyntaxNodeActionHonorExclusions(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(EventDeclarationAction, SyntaxKind.EventDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(BaseFieldDeclarationAction, BaseFieldDeclarationKinds);
context.RegisterSyntaxNodeActionHonorExclusions(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
context.RegisterSyntaxNodeActionHonorExclusions(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
}

private static void HandleBaseTypeDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleBaseTypeDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (BaseTypeDeclarationSyntax)context.Node;
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
}

private static void HandleDelegateDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (DelegateDeclarationSyntax)context.Node;
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
}

private static void HandleEventDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleEventDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (EventDeclarationSyntax)context.Node;
if (syntax.ExplicitInterfaceSpecifier != null)
Expand All @@ -92,7 +102,7 @@ private static void HandleEventDeclarationSyntax(SyntaxNodeAnalysisContext conte
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
}

private static void HandleMethodDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (MethodDeclarationSyntax)context.Node;
if (syntax.ExplicitInterfaceSpecifier != null)
Expand All @@ -108,7 +118,7 @@ private static void HandleMethodDeclarationSyntax(SyntaxNodeAnalysisContext cont
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
}

private static void HandlePropertyDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (PropertyDeclarationSyntax)context.Node;
if (syntax.ExplicitInterfaceSpecifier != null)
Expand All @@ -124,7 +134,7 @@ private static void HandlePropertyDeclarationSyntax(SyntaxNodeAnalysisContext co
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
}

private static void HandleBaseFieldDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleBaseFieldDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (BaseFieldDeclarationSyntax)context.Node;
if (syntax.Parent.IsKind(SyntaxKind.InterfaceDeclaration))
Expand All @@ -148,7 +158,7 @@ private static void HandleBaseFieldDeclarationSyntax(SyntaxNodeAnalysisContext c
CheckAccessModifiers(context, declarator.Identifier, syntax.Modifiers, declarator);
}

private static void HandleIndexerDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (IndexerDeclarationSyntax)context.Node;
if (syntax.ExplicitInterfaceSpecifier != null)
Expand All @@ -164,7 +174,7 @@ private static void HandleIndexerDeclarationSyntax(SyntaxNodeAnalysisContext con
CheckAccessModifiers(context, syntax.ThisKeyword, syntax.Modifiers);
}

private static void HandleConstructorDeclarationSyntax(SyntaxNodeAnalysisContext context)
private static void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context)
{
var syntax = (ConstructorDeclarationSyntax)context.Node;
CheckAccessModifiers(context, syntax.Identifier, syntax.Modifiers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class SA1402FileMayOnlyContainASingleClass : DiagnosticAnalyzer
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxTreeAnalysisContext> SyntaxTreeAction = HandleSyntaxTree;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -53,7 +54,7 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxTreeActionHonorExclusions(HandleSyntaxTree);
context.RegisterSyntaxTreeActionHonorExclusions(SyntaxTreeAction);
}

private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class SA1403FileMayOnlyContainASingleNamespace : DiagnosticAnalyzer
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxTreeAnalysisContext> SyntaxTreeAction = HandleSyntaxTree;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -46,7 +47,7 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxTreeActionHonorExclusions(HandleSyntaxTree);
context.RegisterSyntaxTreeActionHonorExclusions(SyntaxTreeAction);
}

private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class SA1405DebugAssertMustProvideMessageText : SystemDiagnosticsDebugD
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -51,10 +52,10 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleMethodCall, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeActionHonorExclusions(InvocationExpressionAction, SyntaxKind.InvocationExpression);
}

private static void HandleMethodCall(SyntaxNodeAnalysisContext context)
private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context)
{
HandleMethodCall(context, nameof(Debug.Assert), 1, Descriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class SA1406DebugFailMustProvideMessageText : SystemDiagnosticsDebugDia
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> InvocationExpressionAction = HandleInvocationExpression;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -51,10 +52,10 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleMethodCall, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeActionHonorExclusions(InvocationExpressionAction, SyntaxKind.InvocationExpression);
}

private static void HandleMethodCall(SyntaxNodeAnalysisContext context)
private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context)
{
// Debug.Fail is not available in a portable library. So no nameof(Debug.Fail) here
HandleMethodCall(context, "Fail", 0, Descriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ internal class SA1407ArithmeticExpressionsMustDeclarePrecedence : DiagnosticAnal
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<SyntaxKind> HandledBinaryExpressionKinds =
ImmutableArray.Create(
SyntaxKind.AddExpression,
SyntaxKind.SubtractExpression,
SyntaxKind.MultiplyExpression,
SyntaxKind.DivideExpression,
SyntaxKind.ModuloExpression,
SyntaxKind.LeftShiftExpression,
SyntaxKind.RightShiftExpression);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> BinaryExpressionAction = HandleBinaryExpression;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -68,16 +79,10 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.AddExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.SubtractExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.MultiplyExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.DivideExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.ModuloExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.LeftShiftExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleMathExpression, SyntaxKind.RightShiftExpression);
context.RegisterSyntaxNodeActionHonorExclusions(BinaryExpressionAction, HandledBinaryExpressionKinds);
}

private static void HandleMathExpression(SyntaxNodeAnalysisContext context)
private static void HandleBinaryExpression(SyntaxNodeAnalysisContext context)
{
BinaryExpressionSyntax binSyntax = (BinaryExpressionSyntax)context.Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ internal class SA1408ConditionalExpressionsMustDeclarePrecedence : DiagnosticAna
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.MaintainabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<SyntaxKind> HandledBinaryExpressionKinds =
ImmutableArray.Create(SyntaxKind.LogicalAndExpression, SyntaxKind.LogicalOrExpression);

private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
private static readonly Action<SyntaxNodeAnalysisContext> BinaryExpressionAction = HandleBinaryExpression;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -80,11 +84,10 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(HandleLogicalExpression, SyntaxKind.LogicalAndExpression);
context.RegisterSyntaxNodeActionHonorExclusions(HandleLogicalExpression, SyntaxKind.LogicalOrExpression);
context.RegisterSyntaxNodeActionHonorExclusions(BinaryExpressionAction, HandledBinaryExpressionKinds);
}

private static void HandleLogicalExpression(SyntaxNodeAnalysisContext context)
private static void HandleBinaryExpression(SyntaxNodeAnalysisContext context)
{
BinaryExpressionSyntax binSyntax = (BinaryExpressionSyntax)context.Node;

Expand Down
Loading

0 comments on commit cd1d898

Please sign in to comment.