Skip to content

perf: use O(1) positional child access for binary operator lookup#394

Merged
askpt merged 2 commits into
mainfrom
repo-assist/perf-operator-child-access-20260621-842fbf1d80b798a4
Jun 21, 2026
Merged

perf: use O(1) positional child access for binary operator lookup#394
askpt merged 2 commits into
mainfrom
repo-assist/perf-operator-child-access-20260621-842fbf1d80b798a4

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Summary

Replaces O(n) node.children iteration with O(1) node.child(1) positional access in all language analyzers for the getBinaryOperator / getBooleanOperator / getOperator methods.

Motivation

Every binary_expression (and Python's boolean_operator) node in tree-sitter has exactly three children:

binary_expression
├── [0] left operand
├── [1] operator  (&&, ||, ??, and, or, ...)
└── [2] right operand

The previous code iterated node.children — a JS Array that tree-sitter allocates on every access — and used .find() or a for...of loop to locate the operator. Since the operator is always at index 1, node.child(1) is both correct and constant-time.

Changes

All six language analyzers are updated:

File Method renamed/changed
csharpAnalyzer.ts getBinaryOperator
goAnalyzer.ts getBinaryOperator
javaAnalyzer.ts getBinaryOperator
jsLikeAnalyzer.ts getOperator
pythonAnalyzer.ts getBooleanOperator
rustAnalyzer.ts getBinaryOperator

The operator type/text check is preserved — if child(1) is unexpectedly missing or is not a logical operator, the method returns null safely, matching existing behaviour.

Test Status

All 103 tests pass (npm run test:unit). Lint clean (npm run lint). No regressions.

103 passing (83ms)

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.

Replace O(n) node.children iteration with O(1) node.child(1) access in
all language analyzers for getBinaryOperator / getBooleanOperator /
getOperator methods.

In every tree-sitter grammar (C#, Go, Java, JS/TS, Python, Rust), a
binary_expression (or boolean_operator) has exactly three children:
[left, operator, right]. The operator is always at index 1.

Using node.child(1) avoids allocating the children array and iterating
up to three elements for every logical operator encountered during
analysis, which improves performance on deeply nested or complex
expressions.

Files changed:
  csharpAnalyzer.ts, goAnalyzer.ts, javaAnalyzer.ts,
  jsLikeAnalyzer.ts, pythonAnalyzer.ts, rustAnalyzer.ts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [repo-assist] perf: use O(1) positional child access for binary operator lookup perf: use O(1) positional child access for binary operator lookup Jun 21, 2026
@askpt askpt marked this pull request as ready for review June 21, 2026 08:55
@askpt askpt self-requested a review as a code owner June 21, 2026 08:55
Copilot AI review requested due to automatic review settings June 21, 2026 08:55
@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.17%. Comparing base (428fe18) to head (09cc5f5).

Files with missing lines Patch % Lines
src/metricsAnalyzer/languages/javaAnalyzer.ts 60.00% 2 Missing ⚠️
src/metricsAnalyzer/languages/csharpAnalyzer.ts 80.00% 1 Missing ⚠️
src/metricsAnalyzer/languages/goAnalyzer.ts 87.50% 1 Missing ⚠️
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts 87.50% 1 Missing ⚠️
src/metricsAnalyzer/languages/pythonAnalyzer.ts 87.50% 1 Missing ⚠️
src/metricsAnalyzer/languages/rustAnalyzer.ts 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #394      +/-   ##
==========================================
- Coverage   76.26%   76.17%   -0.09%     
==========================================
  Files          13       13              
  Lines        4086     4088       +2     
  Branches      447      445       -2     
==========================================
- Hits         3116     3114       -2     
- Misses        968      972       +4     
  Partials        2        2              

☔ 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.

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 improves performance in the Tree-sitter language analyzers by replacing O(n) scans of node.children (which allocates an array) with O(1) positional access via node.child(1) when extracting logical/binary operators.

Changes:

  • Updated operator-extraction helpers to use node.child(1) instead of iterating node.children.
  • Preserved existing safety behavior by returning null when the operator node is missing or not one of the expected operators.
  • Applied consistently across the Rust, Python, JS-like, Java, Go, and C# analyzers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/metricsAnalyzer/languages/rustAnalyzer.ts Use positional child access to fetch &&/`
src/metricsAnalyzer/languages/pythonAnalyzer.ts Use positional child access to fetch and/or in boolean_operator.
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts Use positional child access to fetch &&/`
src/metricsAnalyzer/languages/javaAnalyzer.ts Use positional child access to fetch &&/`
src/metricsAnalyzer/languages/goAnalyzer.ts Use positional child access to fetch &&/`
src/metricsAnalyzer/languages/csharpAnalyzer.ts Use positional child access to fetch operator node for binary_expression (including binary_operator).

@askpt askpt merged commit c66228b into main Jun 21, 2026
10 checks passed
@askpt askpt deleted the repo-assist/perf-operator-child-access-20260621-842fbf1d80b798a4 branch June 21, 2026 09:01
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