[repo-assist] refactor: replace O(n) children scans with O(1) firstNamedChild/childForFieldName lookups#427
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
…ForFieldName lookups
Replace linear .some()/.find() scans over node.children with O(1) alternatives
in Go, Rust, and JS/TS analyzers:
- goAnalyzer: hasLabel() uses firstNamedChild?.type instead of children.some()
- goAnalyzer: pointer_type inner type uses firstNamedChild instead of children.find()
- rustAnalyzer: hasLabel() uses firstNamedChild?.type instead of children.some()
- rustAnalyzer: getComplexityReason() else_clause uses firstNamedChild?.type
- jsLikeAnalyzer: labeled break/continue uses firstNamedChild?.type
- jsLikeAnalyzer: else_clause reason uses firstNamedChild?.type
- jsLikeAnalyzer: pair key lookup uses childForFieldName('key') for semantic clarity
All 159 unit tests pass.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Replaces the remaining linear
.some()/.find()scans overnode.childrenwith O(1) tree-sitter field-access alternatives across three language analyzers. These hot paths run on every keystroke in the VS Code CodeLens provider, so reducing per-node allocation and iteration has a direct latency benefit.Changes
goAnalyzer.tshasLabel():node.children.some(c => c.type === "label_name")→node.firstNamedChild?.type === "label_name"In Go's AST,
label_nameis the only named child of a labeledbreak/continue, sofirstNamedChildgives an O(1) membership test.typeNode.children.find(c => c.type === "type_identifier")→typeNode.firstNamedChildwith a type checkpointer_typehas exactly one named child (the pointee type), sofirstNamedChildavoids the linear scan.rustAnalyzer.tshasLabel():node.children.some(c => c.type === "label" || c.type === "loop_label")→firstNamedChild?.typeequality checkRust
break_expression/continue_expressionnodes have the loop label as their first (and only) named child.getComplexityReason()else_clausecase:.some(c => c.type === "if_expression")→node.firstNamedChild?.type === "if_expression"The first named child of an
else_clauseis either anif_expression(else-if) or ablock(plain else).jsLikeAnalyzer.tsgetComplexityIncrement()break_statement/continue_statement:.some(c => c.type === "statement_identifier")→node.firstNamedChild?.type === "statement_identifier"JS/TS labeled break/continue have
statement_identifieras their only named child.getComplexityReason()else_clausecase:.some(c => c.type === "if_statement")→node.firstNamedChild?.type === "if_statement"Same pattern as Rust above.
getFunctionName()pairkey extraction:parent.children.find(c => c.type === "property_identifier" || c.type === "identifier")→parent.childForFieldName("key")with type guardtree-sitter-javascript exposes the
keyfield onpairnodes;childForFieldNameis O(1) and more clearly expresses the intent.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 workflows to your repo
To install this agentic workflow, run