Skip to content

Reduce the time spent in MA0182 - #1636

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0182-performance-05eee1
Oct 13, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0182-performance-05eee1

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Why

MA0182 (AvoidUnusedInternalTypesAnalyzer) shows up as one of the slowest rules. For every operation that references a type of the analyzed assembly (invocation, member reference, object creation, ...), the rule called SemanticModel.GetEnclosingSymbol to ignore the references a type makes to itself. That call costs 10-100 µs each and accounted for half or more of the time spent in the callbacks of the rule.

It is worst for the types that are only referenced by themselves, as they are never added to the processed types: a public class that calls its own helpers (test classes, controllers) pays it on every reference to one of its own members.

What

The operation callbacks now use OperationAnalysisContext.ContainingSymbol, which Roslyn already provides, instead of querying the semantic model. The lazy ReferenceLocation struct is not needed anymore, so it is removed.

Measurements

Measured by running the analyzer on the test project of this repository (Roslyn 5.9) with CompilationWithAnalyzers and logAnalyzerExecutionTime. The machine was under heavy load, so the medians are noisy, but the direction was the same in every run.

Measurement Before After
MA0182 alone, reported analyzer time (median) 190-330 ms 87-245 ms
MA0182 alone, reported analyzer time (best iteration) 78-181 ms 46-65 ms
MA0182 with all the analyzers (median) 349 ms 179 ms
Time inside the callbacks, sequential, best case ~16 ms ~7.5 ms

Behavior change

A type that references itself only from an attribute applied to a parameter of its own primary constructor is now reported. It was considered as used before, because GetEnclosingSymbol returns the containing namespace or type for that position:

internal class NotUsed([Sample(typeof(NotUsed))] int value) { }

This is consistent with the same attribute applied to the type itself, which was already reported (InternalClassAttributeTypeOfOwnClass_Diagnostic).

I compared the old and the new reference location on every operation of the analyzer project and of the test project (about 79,000 operations). The only difference is the base call of a primary constructor, which has no effect on the diagnostics. The diagnostics reported on both projects are the same before and after.

Tests

  • 4 new tests: a type referencing itself in a lambda, a local function and a primary constructor, and the matching cases where the type is used from another type. InternalClassReferencingItselfInPrimaryConstructor_Diagnostic fails without the change.
  • AvoidUnusedInternalTypesAnalyzerTests passes with every supported Roslyn version (119 tests with 4.8 and 4.14, 125 with 5.0, 5.6 and 5.9). The full test suite was not run locally.
  • dotnet run --project src/DocumentationGenerator produces no change.

The rule called SemanticModel.GetEnclosingSymbol for every operation that
references a type of the analyzed assembly, to ignore the references a type
makes to itself. This call is much more expensive than the rest of the
analysis, and it was made again and again for the types that are only
referenced by themselves, as they are never added to the processed types.

The operation callbacks now use the containing symbol provided by the
analysis context, which is free.

A type that references itself only from an attribute applied to a parameter
of its own primary constructor is now reported, as the reference is now
attributed to the type instead of its containing namespace or type.
@meziantou
meziantou enabled auto-merge (squash) October 13, 2026 14:42
@meziantou
meziantou merged commit 010135f into main Oct 13, 2026
14 checks passed
@meziantou
meziantou deleted the feature/ma0182-performance-05eee1 branch October 13, 2026 15:57
kivilev pushed a commit to kivilev/plsql-developer-text-tools that referenced this pull request Oct 13, 2026
Updated
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
from 3.0.290 to 3.0.303.

<details>
<summary>Release notes</summary>

_Sourced from [Meziantou.Analyzer's
releases](https://github.com/meziantou/Meziantou.Analyzer/releases)._

## 3.0.303

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.303>

## What's Changed
* MA0100: do not look up GetAwaiter for the types that cannot be awaited
by @​meziantou in
meziantou/Meziantou.Analyzer#1635
* Reduce the time spent in MA0154, MA0218 and MA0219 by @​meziantou in
meziantou/Meziantou.Analyzer#1638
* Reduce the time spent in MA0182 by @​meziantou in
meziantou/Meziantou.Analyzer#1636
* Reduce the time spent in MA0134 by @​meziantou in
meziantou/Meziantou.Analyzer#1637


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.302...3.0.303

## 3.0.302

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.302>

## What's Changed
* Reduce the time spent in MA0001, MA0002, MA0011, MA0026, MA0074 and
MA0150 by @​meziantou in
meziantou/Meziantou.Analyzer#1634


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.301...3.0.302

## 3.0.301

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.301>

## What's Changed
* Reduce the CPU and the memory used by MA0202 by @​meziantou in
meziantou/Meziantou.Analyzer#1633


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.300...3.0.301

## 3.0.300

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.300>

## What's Changed
* MA0015, MA0043: do not enumerate all the symbols in scope by
@​meziantou in meziantou/Meziantou.Analyzer#1632


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.299...3.0.300

## 3.0.299

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.299>

## What's Changed
* MA0031: do not suggest Skip(n).Any() when the value is not a constant
by @​meziantou in
meziantou/Meziantou.Analyzer#1631
* chore(deps): update all dependencies by @​renovate[bot] in
meziantou/Meziantou.Analyzer#1627


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.298...3.0.299

## 3.0.298

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.298>

## What's Changed
* chore(deps): update dependency meziantou.framework.roslyn to 1.2.1 by
@​renovate[bot] in
meziantou/Meziantou.Analyzer#1629


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.297...3.0.298

## 3.0.297

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.297>

## What's Changed
* Update Meziantou.NET.Sdk to 1.0.183 and use its SharedHttpClient by
@​meziantou in meziantou/Meziantou.Analyzer#1628


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.296...3.0.297

## 3.0.296

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.296>

## What's Changed
* Add tests for non-culture-sensitive string methods by @​meziantou in
meziantou/Meziantou.Analyzer#1626


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.295...3.0.296

## 3.0.295

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.295>

## What's Changed
* MA0002: treat the xunit assertions that take an IEqualityComparer<T>
as ordinal with report_only_non_ordinal by @​vencyk in
meziantou/Meziantou.Analyzer#1625

## New Contributors
* @​vencyk made their first contribution in
meziantou/Meziantou.Analyzer#1625

**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.294...3.0.295

## 3.0.294

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.294>

## What's Changed
* Do not suggest instance members in static contexts in MA0166/MA0167 by
@​meziantou in meziantou/Meziantou.Analyzer#1623


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.293...3.0.294

## 3.0.293

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.293>

## What's Changed
* Check for a set instance only before an MA0002 report by
@​alexander-efremov in
meziantou/Meziantou.Analyzer#1622


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.292...3.0.293

## 3.0.292

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.292>

## What's Changed
* Cache LookupSymbols results in OverloadFinder by @​alexander-efremov
in meziantou/Meziantou.Analyzer#1621
* chore(deps): update all dependencies to 1.0.179 by @​renovate[bot] in
meziantou/Meziantou.Analyzer#1618

## New Contributors
* @​alexander-efremov made their first contribution in
meziantou/Meziantou.Analyzer#1621

**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.291...3.0.292

## 3.0.291

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.291>

## What's Changed
* Do not merge is patterns declaring variables in the MA0194 code fix by
@​meziantou in meziantou/Meziantou.Analyzer#1620


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.290...3.0.291

Commits viewable in [compare
view](meziantou/Meziantou.Analyzer@3.0.290...3.0.303).
</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This was referenced Oct 13, 2026
This was referenced Oct 16, 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.

1 participant