Skip to content

perf: use HashSet<string> for NoWarn code lookups#76

Closed
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-71
Closed

perf: use HashSet<string> for NoWarn code lookups#76
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-71

Conversation

@mfogliatto-dev-agent

Copy link
Copy Markdown

Summary

Replaces IEnumerable<string> with HashSet<string> for NoWarn codes at the provider level, converting O(n) linear LINQ Contains lookups to O(1) HashSet lookups in ReferenceEvaluationContextFactory.Create.

Changes

File Change
INoWarnAssembliesProvider.cs Dictionary value type IEnumerable<string>HashSet<string>
NoWarnAssembliesProvider.cs Produces HashSet<string> instead of string[]
ProjectReferenceInfo.cs NoWarn property type IEnumerable<string>HashSet<string>
MSBuildProjectMetadataProvider.cs Wraps split results in HashSet<string>

How it works

The optimization is applied at the provider level — where NoWarn codes are first materialized from config strings. This means:

  • Roslyn path: NoWarnAssembliesProvider.GetNoWarnByAssembly() now returns Dictionary<string, HashSet<string>>. The HashSet values flow through ReferenceCopAnalyzer.AnalyzeCompilation into ReferenceEvaluationContextFactory.Create, where LINQ's Enumerable.Contains() detects the underlying ICollection<T> and dispatches to HashSet<T>.Contains() — O(1).
  • MSBuild path: MSBuildProjectMetadataProvider produces ProjectReferenceInfo with HashSet<string> NoWarn codes, achieving the same O(1) lookup.

The factory's parameter type (IEnumerable<string>) is intentionally unchanged to minimize API surface impact — the performance gain comes from the concrete HashSet passed in at runtime.

Testing

  • All existing tests pass (Roslyn: 11/11, MSBuild: 14/14)
  • NoWarnAssembliesProviderTests validates the provider behavior with the new HashSet return type

Fixes #71

Replace IEnumerable<string> with HashSet<string> for NoWarn codes at the
provider level, converting O(n) LINQ Contains lookups to O(1) HashSet
lookups in ReferenceEvaluationContextFactory.Create.

Changed types in:
- INoWarnAssembliesProvider: Dictionary value type -> HashSet<string>
- NoWarnAssembliesProvider: Produces HashSet<string> instead of arrays
- ProjectReferenceInfo: NoWarn property -> HashSet<string>
- MSBuildProjectMetadataProvider: Wraps split results in HashSet<string>

Fixes #71
@mfogliatto-dev-agent mfogliatto-dev-agent closed this by deleting the head repository Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] ReferenceEvaluationContextFactory.Create uses linear LINQ Contains on NoWarn codes

1 participant