bkoelman / CSharpGuidelinesAnalyzer

Reports diagnostics for C# coding guidelines that are not already covered by Resharper.
Apache License 2.0
145 stars 33 forks source link

Optimize running time of AV1568 #92

Closed bkoelman closed 6 years ago

bkoelman commented 8 years ago

Analyzer

AV1568: DoNotAssignToParametersAnalyzer

Repro steps

Results of running the analyzers on its own codebase:

1>  Total analyzer execution time: 0.670 seconds. (TaskId:28)
1>  NOTE: Elapsed time may be less than analyzer execution time because analyzers can run concurrently. (TaskId:28)
1>  Time (s)    %   Analyzer (TaskId:28)
1>  0.498   74   CSharpGuidelinesAnalyzer, Version=0.2.0.0, Culture=neutral, PublicKeyToken=null (TaskId:28)
1>  0.121   18      CSharpGuidelinesAnalyzer.Maintainability.DoNotAssignToParametersAnalyzer (TaskId:28)
1>  0.051    7      CSharpGuidelinesAnalyzer.Documentation.AvoidInlineCommentsAnalyzer (TaskId:28)
1>  0.040    5      CSharpGuidelinesAnalyzer.Naming.UseUnderscoresForUnusedLambdaParametersAnalyzer (TaskId:28)
1>  0.028    4      CSharpGuidelinesAnalyzer.Documentation.AvoidToDoCommentsAnalyzer (TaskId:28)
1>  0.021    3      CSharpGuidelinesAnalyzer.Naming.DoNotUseAbbreviationsInIdentifierNamesAnalyzer (TaskId:28)
1>  0.021    3      CSharpGuidelinesAnalyzer.Maintainability.IfElseIfStatementsShouldFinishWithElseClauseAnalyzer (TaskId:28)
1>  0.014    2      CSharpGuidelinesAnalyzer.ClassDesign.DoNotHideInheritedMembersAnalyzer (TaskId:28)
1>  0.013    1      CSharpGuidelinesAnalyzer.Naming.NamePropertiesWithAnAffirmativePhraseAnalyzer (TaskId:28)
1>  0.013    1      CSharpGuidelinesAnalyzer.Maintainability.AvoidMultipleTypesPerFileAnalyzer (TaskId:28)
1>  0.012    1      CSharpGuidelinesAnalyzer.Maintainability.AvoidMembersWithMoreThanSevenStatementsAnalyzer (TaskId:28)
1>  0.012    1      CSharpGuidelinesAnalyzer.ClassDesign.MembersShouldDoASingleThingAnalyzer (TaskId:28)
1>  0.011    1      CSharpGuidelinesAnalyzer.Maintainability.NamespacesShouldMatchAssemblyNameAnalyzer (TaskId:28)
1>  0.011    1      CSharpGuidelinesAnalyzer.ClassDesign.AvoidStaticClassesAnalyzer (TaskId:28)
1>  0.010    1      CSharpGuidelinesAnalyzer.Maintainability.OverloadsShouldCallOtherOverloadsAnalyzer (TaskId:28)
1>  0.009    1      CSharpGuidelinesAnalyzer.Naming.DoNotUseNumbersInIdentifiersAnalyzer (TaskId:28)
1>  0.009    1      CSharpGuidelinesAnalyzer.Maintainability.DoNotChangeLoopVariablesAnalyzer (TaskId:28)
1>  0.009    1      CSharpGuidelinesAnalyzer.Framework.ProvideAssemblyInformationAnalyzer (TaskId:28)
1>  0.008    1      CSharpGuidelinesAnalyzer.MiscellaneousDesign.EvaluateQueriesBeforeReturningThemAnalyzer (TaskId:28)
1>  0.007    1      CSharpGuidelinesAnalyzer.MiscellaneousDesign.DoNotPassNullsOnEventInvocationAnalyzer (TaskId:28)
1>  0.006   <1      CSharpGuidelinesAnalyzer.Maintainability.AssignVariablesInSeparateStatementsAnalyzer (TaskId:28)
1>  0.006   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidBooleanParametersAnalyzer (TaskId:28)
1>  0.006   <1      CSharpGuidelinesAnalyzer.Documentation.DocumentAllInternalMembersAnalyzer (TaskId:28)
1>  0.006   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidConditionsWithDoubleNegativesAnalyzer (TaskId:28)
1>  0.005   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidExplicitBooleanComparisonsAnalyzer (TaskId:28)
1>  0.005   <1      CSharpGuidelinesAnalyzer.Framework.OnlyUseDynamicForUnknownTypesAnalyzer (TaskId:28)
1>  0.005   <1      CSharpGuidelinesAnalyzer.Naming.AvoidMisleadingNamesAnalyzer (TaskId:28)
1>  0.004   <1      CSharpGuidelinesAnalyzer.MemberDesign.ReturnInterfacesToCollectionsAnalyzer (TaskId:28)
1>  0.004   <1      CSharpGuidelinesAnalyzer.ClassDesign.TypesShouldHaveASinglePurposeAnalyzer (TaskId:28)
1>  0.004   <1      CSharpGuidelinesAnalyzer.Framework.FavorAsyncAwaitOverTaskContinueWithAnalyzer (TaskId:28)
1>  0.003   <1      CSharpGuidelinesAnalyzer.Naming.UseFrameworkTerminologyInMemberNamesAnalyzer (TaskId:28)
1>  0.003   <1      CSharpGuidelinesAnalyzer.Maintainability.SwitchStatementsShouldHaveADefaultCaseAnalyzer (TaskId:28)
1>  0.003   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidMembersWithMoreThanThreeParametersAnalyzer (TaskId:28)
1>  0.003   <1      CSharpGuidelinesAnalyzer.Naming.DoNotIncludeContainingTypeNameInMembersAnalyzer (TaskId:28)
1>  0.003   <1      CSharpGuidelinesAnalyzer.MiscellaneousDesign.RaiseEventsFromProtectedVirtualMethodsAnalyzer (TaskId:28)
1>  0.002   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidNestedLoopsAnalyzer (TaskId:28)
1>  0.002   <1      CSharpGuidelinesAnalyzer.Naming.StaticClassesShouldOnlyContainExtensionMethodsAnalyzer (TaskId:28)
1>  0.002   <1      CSharpGuidelinesAnalyzer.Naming.DoNotUseHelperMethodsAnalyzer (TaskId:28)
1>  0.002   <1      CSharpGuidelinesAnalyzer.Maintainability.AvoidUsingNamedArgumentsAnalyzer (TaskId:28)
1>  0.002   <1      CSharpGuidelinesAnalyzer.Framework.BuildWithTheHighestWarningLevelAnalyzer (TaskId:28)
1>  0.001   <1      CSharpGuidelinesAnalyzer.Maintainability.CaseClausesInSwitchStatementsShouldHaveBracesAnalyzer (TaskId:28)
1>  0.001   <1      CSharpGuidelinesAnalyzer.Naming.NameAsyncMethodsCorrectlyAnalyzer (TaskId:28)
1>  <0.001   <1      CSharpGuidelinesAnalyzer.Naming.PrefixEventHandlersWithOnAnalyzer (TaskId:28)
1>  0.146   21   Microsoft.CodeAnalysis.CSharp.Analyzers, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (TaskId:28)
1>  0.093   13      Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpReportDiagnosticAnalyzer (TaskId:28)
1>  0.041    6      Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpRegisterActionAnalyzer (TaskId:28)
1>  0.008    1      Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpDiagnosticAnalyzerFieldsAnalyzer (TaskId:28)
1>  0.003   <1      Microsoft.CodeAnalysis.CSharp.Analyzers.CSharpImmutableObjectMethodAnalyzer (TaskId:28)
1>  0.001   <1      Microsoft.CodeAnalysis.CSharp.Analyzers.FixAnalyzers.CSharpFixerWithFixAllAnalyzer (TaskId:28)
1>  <0.001   <1      Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpDiagnosticDescriptorCreationAnalyzer (TaskId:28)
1>  0.019    2   CodeContractNullability, Version=1.0.5.0, Culture=neutral, PublicKeyToken=null (TaskId:28)
1>  0.010    1      CodeContractNullability.CodeContractItemNullabilityAnalyzer (TaskId:28)
1>  0.008    1      CodeContractNullability.CodeContractNullabilityAnalyzer (TaskId:28)
1>  0.007    1   Microsoft.CodeAnalysis.Analyzers, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (TaskId:28)
1>  0.007    1      Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers.DiagnosticAnalyzerAttributeAnalyzer (TaskId:28)
1>  <0.001   <1      Microsoft.CodeAnalysis.Analyzers.InternalImplementationOnlyAnalyzer (TaskId:28)

The output above shows that DoNotAssignToParametersAnalyzer takes a lot of time. It runs (expensive) dataflow analysis on the method body for each parameter individually. We should register on method instead, then run the analysis once and inspect results for each parameter.

bkoelman commented 7 years ago

To get these stats, add <ReportAnalyzer>true</ReportAnalyzer> to the global section of a .csproj file.

bkoelman commented 6 years ago

The proposed fix does not help. Dataflow analysis probably already runs for all identifiers in scope at once, returning results from cache on subsequent requests.