perf: replace O(n) indexOf+children with O(1) nextSibling traversal in C# analyzer - #499
Conversation
…n C# analyzer In CSharpMetricsAnalyzer two methods used parent.children.indexOf(node) which is an O(n) linear scan over the children array: 1. getMethodBody: when searching for a preproc_if sibling after a split method, previously called parent.children.indexOf(node) then iterated by index. Now uses node.nextSibling directly, eliminating both the O(n) indexOf scan and the intermediate children array allocation. 2. hasMatchingColonInSiblings: previously called parent.children.indexOf(errorNode) then iterated over the next 2 children by index. Now walks errorNode.nextSibling directly for up to 2 steps. tree-sitter SyntaxNode.nextSibling is O(1) (it is a pointer walk in the underlying C structure), so both hot paths now run in O(k) where k ≤ 2 or k = number of preprocessor siblings found — independent of class size. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #499 +/- ##
==========================================
+ Coverage 81.37% 81.46% +0.08%
==========================================
Files 13 13
Lines 4366 4354 -12
Branches 442 441 -1
==========================================
- Hits 3553 3547 -6
+ Misses 812 806 -6
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Not ready to approve
The new sibling-walk stop condition still only breaks on class/interface declarations and should also stop on other type declarations (struct/record/enum) to avoid mis-associating a later preproc_if as the method body.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
Optimizes the C# cognitive complexity analyzer’s tree-sitter AST traversal by replacing parent.children.indexOf(...) + indexed loops with nextSibling pointer walks, reducing repeated linear scans when handling preprocessor-split method bodies and fragmented ternary operators.
Changes:
- Updated
getFunctionBodyto search forpreproc_ifusingnode.nextSiblingtraversal instead of scanningparent.children. - Updated
hasMatchingColonInSiblingsto check up to twonextSiblings instead of scanning viaindexOf+ bounds math.
File summaries
| File | Description |
|---|---|
| src/metricsAnalyzer/languages/csharpAnalyzer.ts | Replaces sibling lookup loops that depend on parent.children.indexOf with nextSibling traversal for better performance in C# AST analysis. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
🤖 This pull request was created by Repo Assist, an automated AI assistant.
Summary
Eliminates two O(n) linear scans over
parent.childreninCSharpMetricsAnalyzerby replacing them with directnextSiblingpointer walks (O(1) per step in tree-sitter's underlying C structure).Changes
src/metricsAnalyzer/languages/csharpAnalyzer.ts1.
getMethodBody— preprocessor sibling searchPreviously:
Now:
2.
hasMatchingColonInSiblings— ternary colon detectionPreviously:
Now:
Also simplifies the parent-null guard in
hasMatchingColonInSiblings(no longer needsparentat all sincenextSiblinghandles missing siblings gracefully).Rationale
Array.prototype.indexOfwalks the entire children array to find the node. In large C# classes with many members, this linear scan runs every time a preprocessor-split method or ternary in an ERROR node is encountered. TheSyntaxNode.nextSiblingproperty in tree-sitter is a constant-time pointer dereference, making both paths run in O(k) where k is bounded by either the class size found (early exit onpreproc_ifor another method) or 2 (the fixed step limit for colon detection).Test Status
Warning
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