Skip to content

Perf Phase 2: cached-symbol comparisons instead of formatted-string comparisons #458

Description

@credfeto

Part of #455.

Many sites format a symbol to a string per node purely to compare against a constant. Resolve the constant to an INamedTypeSymbol once at RegisterCompilationStartAction and compare with SymbolEqualityComparer instead (zero allocation, and gives a free bail-out gate when the type isn't referenced by the compilation). This is the pattern InternalsVisibleToDiagnosticsAnalyzer and ProhibitedSubstituteForUsageInTestBaseDiagnosticsAnalyzer already use correctly.

  • Helpers/TestDetection.cs:44IsDerivedFromTestBase string-compares ToFullyQualifiedName() per base class per node. Convert to a cached-TestBase-symbol API (accept the compilation / cached symbol) and walk with SymbolEqualityComparer. Callers: TestClassAnalysis, TestClassFieldsAnalysis, TestClassPropertyAnalysis, ProhibitedSubstituteForUsageInTestBase.
  • ArgumentExceptionAnalysisDiagnosticsAnalyzer.cs:129 — cache the 3 exception type symbols
  • ClassVisibilityDiagnosticsAnalyzer.cs:138,179 — cache the MockBase<T> symbol
  • DebuggerDisplayAnalysisDiagnosticsAnalyzer.cs:146 — cache the DebuggerDisplayAttribute symbol (also gives a compilation-start bail-out)
  • SuppressMessageDiagnosticsAnalyzer.cs:90 — cache System.ReadOnlySpan<T> symbol
  • ProhibitedClassesDiagnosticsAnalyzer.cs:91,217 — compare symbol identity against the already-cached type dictionary instead of re-formatting ToFullyQualifiedName() (currently done twice per node)
  • ProhibitedMethodInvocationsDiagnosticsAnalyzer.cs:165,307,348,358 / ForceMethodParametersInvocationsDiagnosticsAnalyzer.cs:149 / ProhibitedMethodWithStrictParametersInvocationDiagnosticsAnalyzer.cs:120 — these rebuild a qualified-name string per node as the KEY into an already-cached dictionary. Re-key the caches by INamedTypeSymbol (SymbolEqualityComparer) so no string is ever formatted on the lookup path.
  • ConstructorGenericParameterTypeDiagnosticsAnalyser.cs:147,311 — same treatment

Verification: full test suite green, benchmarks show reduced allocations for TestDetection-dependent and dictionary-lookup analyzers, no change in the set of diagnostics produced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions