Skip to content

Commit f1fe7a4

Browse files
⚡️ Speed up method CfoVisitor._get_called_func_name by 18% in PR #501 (runtime-fixes-2)
**Key optimizations:** - Avoid multiple `isinstance` calls by caching `type(node)` in a variable and using identity checks against the expected types (`_ast_Name`, `_ast_Attribute`). - This speeds up the type checks, since identity comparison is faster than repeated isinstance checks, and in this hot path, there are only two accepted types.
1 parent 66207b5 commit f1fe7a4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

codeflash/code_utils/edit_generated_tests.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ def visit_Call(self, node): # type: ignore[no-untyped-def] # noqa: ANN201, ANN0
6262
self.results.append(node.lineno - 1)
6363
self.generic_visit(node)
6464

65-
def _get_called_func_name(self, node): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
65+
def _get_called_func_name(self, node):
6666
"""Return name of called fn."""
67-
if isinstance(node, ast.Name):
67+
t = type(node)
68+
if t is _ast_Name:
6869
return node.id
69-
if isinstance(node, ast.Attribute):
70+
if t is _ast_Attribute:
7071
return node.attr
7172
return None
7273

@@ -272,3 +273,8 @@ def add_runtime_comments_to_generated_tests(
272273
modified_tests.append(test)
273274

274275
return GeneratedTestsList(generated_tests=modified_tests)
276+
277+
278+
_ast_Name = ast.Name
279+
280+
_ast_Attribute = ast.Attribute

0 commit comments

Comments
 (0)