Skip to content

Commit 5ff60e2

Browse files
authored
Merge pull request #878 from codeflash-ai/codeflash/optimize-pr867-2025-11-05T08.40.37
⚡️ Speed up method `ImportAnalyzer.visit_Call` by 31% in PR #867 (`inspect-signature-issue`)
2 parents 0f2c747 + 1ec1005 commit 5ff60e2

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,37 @@ def generic_visit(self, node: ast.AST) -> None:
449449
if self.found_any_target_function:
450450
return
451451
# Direct base call improves run speed (avoids extra method resolution)
452-
ast.NodeVisitor.generic_visit(self, node)
452+
self._fast_generic_visit(node)
453+
454+
def _fast_generic_visit(self, node: ast.AST) -> None:
455+
"""Faster generic_visit: Inline traversal, avoiding method resolution overhead.
456+
Short-circuits (returns) if found_any_target_function is True.
457+
"""
458+
# This logic is derived from ast.NodeVisitor.generic_visit, but with optimizations.
459+
found_flag = self.found_any_target_function
460+
# Micro-optimization: store fATF in local variable for quick repeated early exit
461+
if found_flag:
462+
return
463+
for field in node._fields:
464+
value = getattr(node, field, None)
465+
if isinstance(value, list):
466+
for item in value:
467+
if self.found_any_target_function:
468+
return
469+
if isinstance(item, ast.AST):
470+
meth = getattr(self, "visit_" + item.__class__.__name__, None)
471+
if meth is not None:
472+
meth(item)
473+
else:
474+
self._fast_generic_visit(item)
475+
elif isinstance(value, ast.AST):
476+
if self.found_any_target_function:
477+
return
478+
meth = getattr(self, "visit_" + value.__class__.__name__, None)
479+
if meth is not None:
480+
meth(value)
481+
else:
482+
self._fast_generic_visit(value)
453483

454484

455485
def analyze_imports_in_test_file(test_file_path: Path | str, target_functions: set[str]) -> bool:

0 commit comments

Comments
 (0)