perf: replace O(depth) isInPreprocessorBlock walk with O(1) depth counter - #505
Merged
askpt merged 1 commit intoAug 4, 2026
Conversation
…nter Track preprocessor block nesting depth via a counter incremented/decremented in visit() when entering/leaving preproc_* nodes. Replace the previous ancestor-walk implementation of isInPreprocessorBlock() with a simple counter check, eliminating redundant tree traversal for every field_declaration and variable_declaration node visited. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #505 +/- ##
=======================================
Coverage 81.46% 81.47%
=======================================
Files 13 13
Lines 4354 4356 +2
Branches 441 439 -2
=======================================
+ Hits 3547 3549 +2
Misses 806 806
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes C# cognitive complexity analysis by replacing an O(AST-depth) ancestor walk in CSharpMetricsAnalyzer.isInPreprocessorBlock() with an O(1) depth-counter check maintained during AST traversal.
Changes:
- Added
preprocessorDepthstate to track whether traversal is currently insidepreproc_*nodes. - Reset
preprocessorDepthat the start of eachanalyzeFunction()call. - Updated
visit()to increment/decrementpreprocessorDepthon entry/exit ofpreproc_*nodes, and switchedisInPreprocessorBlock()to a constant-time counter check.
askpt
deleted the
repo-assist/perf-csharp-preprocessor-depth-20260804-5891f4286dcad1b5
branch
August 4, 2026 07:28
This was referenced Aug 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated PR from Repo Assist.
Summary
Replaces the O(AST-depth) ancestor walk in
CSharpMetricsAnalyzer.isInPreprocessorBlock()with an O(1) counter check.Problem
isInPreprocessorBlock(node)walked fromnodeup to the root to find anypreproc_*ancestor. It was called for everyfield_declarationandvariable_declarationnode visited during analysis viagetComplexityFromMalformedDeclaration(). In C# files with many field or variable declarations, this adds repeated traversal proportional to AST depth.Fix
Added a
preprocessorDepthcounter to the analyzer class. It is:0at the start of each function analysis (analyzeFunction)visit()enters apreproc_*nodevisit()exits apreproc_*nodeisInPreprocessorBlocknow simply checksthis.preprocessorDepth > 0, making it O(1) per call.Trade-offs
isInPreprocessorBlockparameter becomes unused; kept for call-site compatibility (renamed to_nodeper TypeScript convention)Test Status
npm run compile— no errorsWarning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
releaseassets.githubusercontent.comSee Network Configuration for more information.
Add this agentic workflow to your repo
To install this agentic workflow, run