Skip to content

perf: avoid substring allocation in getBinaryOperator for C# && and || - #480

Merged
askpt merged 2 commits into
mainfrom
repo-assist/perf-csharp-binop-O1-20260727-9f87dadefc0d0118
Jul 27, 2026
Merged

perf: avoid substring allocation in getBinaryOperator for C# && and ||#480
askpt merged 2 commits into
mainfrom
repo-assist/perf-csharp-binop-O1-20260727-9f87dadefc0d0118

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

In tree-sitter-c-sharp, anonymous operator tokens (&&, ||, +, etc.) use their literal text as the node type — i.e. node.type === "&&". This means we can return node.type directly instead of allocating a substring via sourceText.substring(startIndex, endIndex).

Changes

getBinaryOperator() in csharpAnalyzer.ts

Before:

if (type === "&&" || type === "||" || type === "binary_operator") {
  return this.sourceText.substring(operatorNode.startIndex, operatorNode.endIndex);
}

After:

if (type === "&&" || type === "||") {
  return type;  // node.type IS the operator text — no allocation needed
}

This eliminates a string allocation on every &&/|| binary expression in C# files. The "binary_operator" branch was dead code — tree-sitter-c-sharp never emits a node of that type; all operators have their literal text as their type.

Why this matters

The getBinaryOperator() method is called for every binary_expression node during analysis. On large C# files with many boolean conditions, this adds up. Avoiding the substring allocation reduces GC pressure during real-time CodeLens analysis.

Test Status

  • npm run compile — clean (0 errors)
  • npm run lint — clean (0 warnings)
  • npm run test:unit — 199/199 passing

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@1c6668b751c51af8571f01204ceffb19362e0f66

In tree-sitter-c-sharp, anonymous operator tokens such as && and ||
use their literal text as the node type. Reading node.type directly
avoids a sourceText.substring() heap allocation on every binary
logical expression. The previously matched 'binary_operator' case was
dead code (no such token type is emitted by the grammar).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [repo-assist] perf: avoid substring allocation in getBinaryOperator for C# && and || perf: avoid substring allocation in getBinaryOperator for C# && and || Jul 27, 2026
@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.43%. Comparing base (4af65b8) to head (3d2e846).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #480      +/-   ##
==========================================
- Coverage   81.43%   81.43%   -0.01%     
==========================================
  Files          13       13              
  Lines        4347     4346       -1     
  Branches      439      439              
==========================================
- Hits         3540     3539       -1     
  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 July 27, 2026 11:44
@askpt
askpt self-requested a review as a code owner July 27, 2026 11:44
Copilot AI review requested due to automatic review settings July 27, 2026 11:44

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 the C# cognitive complexity analyzer by avoiding per-node string allocations when detecting logical && / || operators in binary_expression nodes, improving performance during repeated real-time analysis runs.

Changes:

  • Update getBinaryOperator() in the C# analyzer to return operatorNode.type directly for && and || rather than allocating a substring.
  • Remove the unused "binary_operator" handling branch based on tree-sitter-c-sharp’s operator token typing behavior.
  • Expand inline documentation clarifying why .type is sufficient for these operators.

@askpt
askpt merged commit b8a54b8 into main Jul 27, 2026
11 checks passed
@askpt
askpt deleted the repo-assist/perf-csharp-binop-O1-20260727-9f87dadefc0d0118 branch July 27, 2026 11:48
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