mfogliatto / mfogliatto/ReferenceCop
[Performance] ReferenceCopCodeFixProvider.FixableDiagnosticIds allocates new ImmutableArray on every access
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 1
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
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.cs—FixableDiagnosticIdsproperty 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 whereSupportedDiagnosticsandFixableDiagnosticIdsshould 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.SupportedDiagnosticscorrectly usesImmutableArray.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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open src/ReferenceCop.Roslyn.CodeFixes/ReferenceCopCodeFixProvider.cs and inspect FixableDiagnosticIds, then compare its getter with ReferenceCopAnalyzer.SupportedDiagnostics. Replace the per-access conversion with the empty immutable value and verify the property no longer performs allocation; treat the broader scaffold-removal or implementation note as a separate decision.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Refactor
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 78/100