-
Notifications
You must be signed in to change notification settings - Fork 21
⚡️ Speed up method JavaScriptSupport._find_and_extract_body by 12% in PR #1561 (add/support_react)
#1610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeflash-ai
wants to merge
1
commit into
add/support_react
Choose a base branch
from
codeflash/optimize-pr1561-2026-02-20T14.01.06
base: add/support_react
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
⚡️ Speed up method JavaScriptSupport._find_and_extract_body by 12% in PR #1561 (add/support_react)
#1610
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1301,38 +1301,39 @@ def _find_and_extract_body(self, source: str, function_name: str, analyzer: Tree | |||||||
|
|
||||||||
| def find_function_node(node: Any, target_name: str) -> Any: | ||||||||
| """Recursively find a function/method with the given name.""" | ||||||||
| # Check method definitions | ||||||||
| if node.type == "method_definition": | ||||||||
| name_node = node.child_by_field_name("name") | ||||||||
| if name_node: | ||||||||
| name = source_bytes[name_node.start_byte : name_node.end_byte].decode("utf8") | ||||||||
| if name == target_name: | ||||||||
| return node | ||||||||
|
|
||||||||
| # Check function declarations | ||||||||
| if node.type in ("function_declaration", "function"): | ||||||||
| name_node = node.child_by_field_name("name") | ||||||||
| if name_node: | ||||||||
| name = source_bytes[name_node.start_byte : name_node.end_byte].decode("utf8") | ||||||||
| if name == target_name: | ||||||||
| return node | ||||||||
|
|
||||||||
| # Check arrow functions assigned to variables | ||||||||
| if node.type == "lexical_declaration": | ||||||||
| for child in node.children: | ||||||||
| if child.type == "variable_declarator": | ||||||||
| name_node = child.child_by_field_name("name") | ||||||||
| value_node = child.child_by_field_name("value") | ||||||||
| if name_node and value_node and value_node.type == "arrow_function": | ||||||||
| name = source_bytes[name_node.start_byte : name_node.end_byte].decode("utf8") | ||||||||
| if name == target_name: | ||||||||
| return value_node | ||||||||
| target_bytes = target_name.encode("utf8") | ||||||||
| stack = [node] | ||||||||
| while stack: | ||||||||
| n = stack.pop() | ||||||||
|
|
||||||||
| # Check method definitions | ||||||||
| if n.type == "method_definition": | ||||||||
| name_node = n.child_by_field_name("name") | ||||||||
| if name_node: | ||||||||
| if source_bytes[name_node.start_byte : name_node.end_byte] == target_bytes: | ||||||||
| return n | ||||||||
|
|
||||||||
| # Check function declarations | ||||||||
| if n.type in ("function_declaration", "function"): | ||||||||
| name_node = n.child_by_field_name("name") | ||||||||
| if name_node: | ||||||||
| if source_bytes[name_node.start_byte : name_node.end_byte] == target_bytes: | ||||||||
| return n | ||||||||
|
|
||||||||
| # Check arrow functions assigned to variables | ||||||||
| if n.type == "lexical_declaration": | ||||||||
| for child in n.children: | ||||||||
| if child.type == "variable_declarator": | ||||||||
| name_node = child.child_by_field_name("name") | ||||||||
| value_node = child.child_by_field_name("value") | ||||||||
| if name_node and value_node and value_node.type == "arrow_function": | ||||||||
| if source_bytes[name_node.start_byte : name_node.end_byte] == target_bytes: | ||||||||
| return value_node | ||||||||
|
|
||||||||
| # Recurse into children (stack push) | ||||||||
| for child in n.children: | ||||||||
| stack.append(child) | ||||||||
|
Comment on lines
+1334
to
+1335
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ruff PERF402: This
Suggested change
|
||||||||
|
|
||||||||
| # Recurse into children | ||||||||
| for child in node.children: | ||||||||
| result = find_function_node(child, target_name) | ||||||||
| if result: | ||||||||
| return result | ||||||||
|
|
||||||||
| return None | ||||||||
|
|
||||||||
|
|
||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: The conversion from recursion to iterative
stack.pop()+stack.extend(n.children)changes traversal order from left-to-right DFS to right-to-left DFS. The original recursive version would find the first (leftmost) matching function in source order, while this iterative version will find a different one if there are duplicate function names.In practice this is unlikely to cause issues since function names are typically unique within a file, but worth being aware of. If strict left-to-right order is needed, use
stack.pop(0)(BFS) or reverse children before pushing:stack.extend(reversed(n.children)).