Skip to content

[Performance] ReferenceCopCodeFixProvider.FixableDiagnosticIds allocates new ImmutableArray on every access #72

Description

@mfogliatto

Description

ReferenceCopCodeFixProvider.FixableDiagnosticIds is implemented as:

public sealed override ImmutableArray<string> FixableDiagnosticIds
{
    get { return Enumerable.Empty<string>().ToImmutableArray<string>(); }
}

Every property access allocates a new ImmutableArray<string> by calling Enumerable.Empty<string>().ToImmutableArray(). The Roslyn infrastructure accesses FixableDiagnosticIds multiple times during analysis — once during registration, during fix-all computation, and potentially on each diagnostic evaluation.

Affected File

  • src/ReferenceCop.Roslyn.CodeFixes/ReferenceCopCodeFixProvider.csFixableDiagnosticIds property getter (line 13)

Impact

  • Repeated allocation: While each ImmutableArray<string> is small (empty), the allocation pattern is wasteful and goes against Roslyn analyzer best practices where SupportedDiagnostics and FixableDiagnosticIds should return cached static values.
  • Analyzer hosting: Roslyn analyzer hosts (VS, MSBuild, OmniSharp) may query this property frequently. The per-access allocation creates unnecessary GC pressure in an environment where analyzers should be allocation-conscious.
  • Inconsistency: The ReferenceCopAnalyzer.SupportedDiagnostics correctly uses ImmutableArray.Create(...) which is more efficient, but this code fix provider does not follow the same pattern.

Suggested Fix

Cache the value as a static field:

private static readonly ImmutableArray<string> EmptyDiagnosticIds = ImmutableArray<string>.Empty;

public sealed override ImmutableArray<string> FixableDiagnosticIds => EmptyDiagnosticIds;

Or simply:

public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray<string>.Empty;

ImmutableArray<string>.Empty is a pre-allocated singleton — no allocation at all.

Additional Note

The empty FixableDiagnosticIds and the boilerplate RegisterCodeFixesAsync / MakeUppercaseAsync methods suggest this code fix provider is still the Roslyn template scaffold and has not been implemented yet. Consider either implementing it or removing the project from the solution to avoid shipping dead code that registers with the Roslyn host.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance optimization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions