Skip to content

perf: replace O(depth) isInPreprocessorBlock walk with O(1) depth counter - #505

Merged
askpt merged 1 commit into
mainfrom
repo-assist/perf-csharp-preprocessor-depth-20260804-5891f4286dcad1b5
Aug 4, 2026
Merged

perf: replace O(depth) isInPreprocessorBlock walk with O(1) depth counter#505
askpt merged 1 commit into
mainfrom
repo-assist/perf-csharp-preprocessor-depth-20260804-5891f4286dcad1b5

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

🤖 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 from node up to the root to find any preproc_* ancestor. It was called for every field_declaration and variable_declaration node visited during analysis via getComplexityFromMalformedDeclaration(). In C# files with many field or variable declarations, this adds repeated traversal proportional to AST depth.

Fix

Added a preprocessorDepth counter to the analyzer class. It is:

  • reset to 0 at the start of each function analysis (analyzeFunction)
  • incremented when visit() enters a preproc_* node
  • decremented when visit() exits a preproc_* node

isInPreprocessorBlock now simply checks this.preprocessorDepth > 0, making it O(1) per call.

Trade-offs

  • Tiny additional state per analyzer instance (one integer)
  • isInPreprocessorBlock parameter becomes unused; kept for call-site compatibility (renamed to _node per TypeScript convention)

Test Status

  • Compile: ✅ npm run compile — no errors
  • Unit tests: ✅ 201/201 passing, coverage thresholds maintained
  • All existing C# preprocessor-related tests continue to pass

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • releaseassets.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@42c2ab5b4e4c9273534c39259b2e0df7f20f07e9

…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>
@askpt askpt changed the title [repo-assist] perf: replace O(depth) isInPreprocessorBlock walk with O(1) depth counter perf: replace O(depth) isInPreprocessorBlock walk with O(1) depth counter Aug 4, 2026
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.47%. Comparing base (b7bab91) to head (6ffd48b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@askpt
askpt marked this pull request as ready for review August 4, 2026 07:25
@askpt
askpt self-requested a review as a code owner August 4, 2026 07:25
Copilot AI lite review requested due to automatic review settings August 4, 2026 07:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 preprocessorDepth state to track whether traversal is currently inside preproc_* nodes.
  • Reset preprocessorDepth at the start of each analyzeFunction() call.
  • Updated visit() to increment/decrement preprocessorDepth on entry/exit of preproc_* nodes, and switched isInPreprocessorBlock() to a constant-time counter check.

@askpt
askpt merged commit e52ebcb into main Aug 4, 2026
24 checks passed
@askpt
askpt deleted the repo-assist/perf-csharp-preprocessor-depth-20260804-5891f4286dcad1b5 branch August 4, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants