Skip to content

perf: O(1) else-token lookup in Java analyzer via child(3) instead of find()#477

Merged
askpt merged 2 commits into
mainfrom
repo-assist/perf-java-else-token-O1-20260726-d2e3d15df700612d
Jul 26, 2026
Merged

perf: O(1) else-token lookup in Java analyzer via child(3) instead of find()#477
askpt merged 2 commits into
mainfrom
repo-assist/perf-java-else-token-O1-20260726-d2e3d15df700612d

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Summary

Replaces a linear Array.prototype.find() scan with an O(1) indexed child(3) lookup when locating the else keyword in Java if_statement nodes.

What Changed

src/metricsAnalyzer/languages/javaAnalyzer.ts

In Java's tree-sitter AST, an if_statement with an else clause always has this fixed child layout:

Index Node Named
0 if keyword
1 condition expression
2 then-body block
3 else keyword
4 else-body

Before: node.children.find((c) => !c.isNamed && c.type === "else")

  • Allocates a new closure on every call
  • Performs a linear scan of the children array (O(n))
  • Only skipped at all when elseBranchNode is null (no else clause)

After: node.child(3) + type guard

  • Direct indexed access — O(1), no allocation, no scan
  • Defensive type check (elseToken.type === "else") retained for safety

This path runs for every if_statement with an else clause during analysis. In files with many if/else chains, this eliminates repeated closure allocations and scans.

Test Status

npm run compile  ✅  (0 errors)
npm run lint     ✅  (0 warnings)
npm run test:unit  ✅  198 passing, 0 failing

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

… find()

In Java's tree-sitter AST an if_statement with an else clause has a fixed
structure: [0]=if, [1]=condition, [2]=then-body, [3]=else-keyword, [4]=else-body.
Replace the O(n) Array.prototype.find() scan with a direct O(1) child(3) lookup,
eliminating a closure allocation and linear scan over every if_statement's
children list when an else clause is present.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [repo-assist] perf: O(1) else-token lookup in Java analyzer via child(3) instead of find() perf: O(1) else-token lookup in Java analyzer via child(3) instead of find() Jul 26, 2026
@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.43%. Comparing base (a94c59d) to head (961d058).

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

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 Java cognitive complexity analysis by replacing a linear scan over if_statement children to locate the else keyword with a direct indexed lookup, reducing per-node overhead during traversal.

Changes:

  • Replaced node.children.find(...) with node.child(3) when retrieving the else token for Java if_statement nodes that have an else branch.
  • Kept a defensive type check (elseToken.type === "else") before recording the “else clause / else if clause” complexity detail.

@askpt
askpt merged commit 8966cdd into main Jul 26, 2026
18 checks passed
@askpt
askpt deleted the repo-assist/perf-java-else-token-O1-20260726-d2e3d15df700612d branch July 26, 2026 13:23
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