Skip to content

Walk the tokens of a syntax tree once for the spacing and layout rules - #4106

Merged
AArnott merged 3 commits into
DotNetAnalyzers:masterfrom
alexander-efremov:perf/single-token-walk
Oct 7, 2026
Merged

AArnott merged 3 commits into
DotNetAnalyzers:masterfrom
alexander-efremov:perf/single-token-walk

Conversation

@alexander-efremov

Copy link
Copy Markdown
Contributor

Ported from the StyleCopNext.Analyzers fork (see the discussion in alexander-efremov#564).

  • 23 spacing and layout rules (SA1000-SA1027, SA1507, SA1509) each walked every token of every syntax tree with DescendantTokens. The tokens are now collected once per tree and compilation through a SyntaxTreeValueProvider (like the settings cache), and the rules iterate the shared arrays.
  • Token order and set are unchanged, with and without the tokens of structured trivia; new SyntaxTreeTokensTests compare both arrays with DescendantTokens. Diagnostics are unchanged.

Measured on a large test project (csc on 4 cores, 8 interleaved runs): StyleCop /reportanalyzer time median 4.69 s -> 2.80 s, lower in 8 of 8 pairs.

Tests (net6.0, on top of current master): StyleCop.Analyzers.Test, CSharp7 and CSharp13 pass.

Draft until CI on this repository is running again.

@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.22642% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.48%. Comparing base (513ba0f) to head (c439c31).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4106      +/-   ##
==========================================
- Coverage   97.48%   97.48%   -0.01%     
==========================================
  Files        1026     1034       +8     
  Lines      119387   119496     +109     
  Branches     3476     3482       +6     
==========================================
+ Hits       116388   116493     +105     
- Misses       1986     1988       +2     
- Partials     1013     1015       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alexander-efremov
alexander-efremov marked this pull request as ready for review October 7, 2026 05:14
@AArnott

AArnott commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Thanks, this is a great win! CI is green with master merged in, and I'm merging it now.

As a follow-up, could you share:

  1. The memory side of the trade-off. The shared token arrays (two per tree, with and without structured-trivia tokens) live for the compilation's lifetime, so allocation and working-set numbers from the same project would help, especially for IDE scenarios.
  2. A way for us to reproduce the 4.69 s → 2.80 s measurement (a script or steps), so we can track it going forward.

@AArnott
AArnott merged commit 717164c into DotNetAnalyzers:master Oct 7, 2026
21 checks passed
@alexander-efremov
alexander-efremov deleted the perf/single-token-walk branch October 9, 2026 05:34
This was referenced Nov 11, 2026
This was referenced Nov 23, 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.

2 participants