Skip to content

SA1141: Use IOperation for expression analysis - #4292

Merged
AArnott merged 2 commits into
masterfrom
aarnott-sa1141-operation-analyzer
Oct 13, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-sa1141-operation-analyzer

Conversation

@AArnott

@AArnott AArnott commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

SA1141's repeated GetSymbolInfo calls were reported to cause heavy CPU usage. This uses Roslyn operations for expression semantics while preserving narrow syntax registrations and filtering before requesting an operation.

Fixes #3609

Approach

  • Keep the existing invocation, object creation, default-expression, and cast-expression syntax registrations. There are no broad operation callbacks or callbacks for implicit conversions.
  • Retrieve an invocation operation only after the existing argument-count, member-access, and Create name checks pass. For object creation, defaults, and casts, retrieve an operation only when the relevant type syntax contains a generic ValueTuple name, including nested tuple/generic types.
  • Read target methods and types from operations through the existing light-up wrappers instead of querying each candidate type's symbol separately.
  • Preserve diagnostic spans and expression-tree exclusions, including query-generated lambdas.
  • Keep declaration checks syntax-based and retain the original semantic checks on compilers without supported IOperation APIs. Older Roslyn tuple symbols expose generic arguments through the underlying ValueTuple type.

No intentional diagnostic or code-fix behavior changes. The added tests characterize existing behavior rather than reproduce a diagnostic failure on master; this issue concerns analysis cost.

Validation

The revised Debug build completed with zero warnings. SA1141 tests passed in all ten C# 6 through C# 15 test projects on net10.0. The initial implementation also passed the C# 7 project's net472 tests. Added coverage exercises nested types, unrelated operations, expression-tree casts, nested lambdas, and queryable versus enumerable queries.

Performance

Review identified that the initial broad operation registrations added standalone operation-driver overhead on unrelated code. Those registrations have been removed. Unrelated expressions now exit through syntax filtering without requesting an operation.

A local standalone benchmark generated 2,000 methods per scenario, pre-bound each fresh compilation with GetDiagnostics, then timed GetAnalyzerDiagnosticsAsync with only SA1141 enabled and concurrency disabled. Each process ran three warmups and five measured iterations, using the C# 15 project's Roslyn assemblies. Three paired process runs compared the original implementation at 47643d314 with the revised implementation:

Scenario Baseline times Revised times Approximate managed allocations, baseline / revised
Unrelated expressions 327.4, 340.8, 309.9 ms 317.1, 339.0, 330.3 ms 63.1 MB / 63.1 MB
Tuple-heavy expressions 485.1, 479.7, 438.2 ms 475.9, 489.8, 495.5 ms 125.9 MB / 100.9 MB

Unrelated methods contain Math.Max(1, 2), new List<int>(), default(List<int>), and (List<int>)input. Tuple-heavy methods contain ValueTuple.Create(1, 2), new ValueTuple<int, int>(1, 2), default(List<ValueTuple<int, int>>), and (ValueTuple<int, int>)input; the harness asserts 8,000 diagnostics in that scenario and zero in the unrelated scenario.

The previous roughly 10 MB unrelated-code allocation penalty is eliminated; tuple-heavy allocations decrease about 20%. Timing varies between runs, so these measurements do not establish a CPU speedup or a large-solution performance claim.

Use operation-provided types and target methods instead of repeated semantic-model queries. Preserve diagnostic locations, expression-tree exclusions, declaration analysis, and the legacy compiler fallback.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 13, 2026 22:32
Copilot AI balanced review requested due to automatic review settings October 13, 2026 22:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The broader callbacks introduce a measured standalone performance regression without demonstrating a CPU improvement.

1 open finding
What changed in this PR

Refactors SA1141 expression analysis to use Roslyn operations while retaining syntax fallback for older compilers.

Changes:

  • Adds operation-based tuple analysis and expression-tree exclusions.
  • Adds C# 7 regression coverage for nested types and queries.
File Description
SA1141UseTupleSyntax.cs Implements operation-based analysis.
SA1141CSharp7UnitTests.cs Covers tuple expressions and exclusions.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.61%. Comparing base (47643d3) to head (610af82).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4292      +/-   ##
==========================================
- Coverage   97.63%   97.61%   -0.03%     
==========================================
  Files        1292     1292              
  Lines      139777   139961     +184     
  Branches     4603     4625      +22     
==========================================
+ Hits       136467   136618     +151     
- Misses       2119     2149      +30     
- Partials     1191     1194       +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Avoid broad operation dispatch and operation-tree setup on unrelated code. Retrieve operations only for Create invocations and type syntax containing ValueTuple, preserving operation-based semantic checks.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 22:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation addresses the prior performance concern and provides comprehensive inherited test coverage.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@AArnott
AArnott merged commit 1fca908 into master Oct 13, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1141-operation-analyzer branch October 13, 2026 22:55
This was referenced Nov 11, 2026
This was referenced Nov 29, 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.

Convert SA1141 (Use Tuple Syntax) to an IOperation analyzer

2 participants