Skip to content

Commit ffa40c8

Browse files
⚡️ Speed up method BenchmarkFunctionRemover._is_benchmark_call by 16% in PR #313 (skip-benchmark-instrumentation)
Here’s an optimized version of your program, designed to improve runtime **without** altering the function signatures or output values. The function is quite simple, but we streamline the checks for speed and minimize unnecessary computation by. - Saving attribute lookups to variables to avoid recalculating them multiple times. - Reordering `if` checks so that the least expensive checks are performed first. - Returning early to avoid the cost of forming an unnecessary `bool()` call or evaluating unnecessary conditions. **Preserved all comments.** This removes a function call to `bool`, skips unnecessary conditions, and avoids both repeated lookups and unnecessary object creation. The logic is unchanged; this is strictly faster.
1 parent e353f38 commit ffa40c8

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

codeflash/code_utils/code_replacer.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ def _is_benchmark_marker(self, decorator: ast.expr) -> bool:
7676
@staticmethod
7777
def _is_benchmark_call(call: ast.Call) -> bool:
7878
"""Check if a call is using the benchmark fixture."""
79-
if isinstance(call.func, ast.Name) and call.func.id == "benchmark":
80-
return True
81-
return bool(
82-
isinstance(call.func, ast.Attribute)
83-
and call.func.attr in ["benchmark", "__call__"]
84-
and isinstance(call.func.value, ast.Name)
85-
and call.func.value.id == "benchmark"
86-
)
79+
func = call.func
80+
if isinstance(func, ast.Name):
81+
return func.id == "benchmark"
82+
if isinstance(func, ast.Attribute):
83+
if func.attr not in {"benchmark", "__call__"}:
84+
return False
85+
value = func.value
86+
return isinstance(value, ast.Name) and value.id == "benchmark"
87+
return False
8788

8889
def visit_FunctionDef(self, node: ast.FunctionDef) -> Optional[AST]:
8990
"""Visit function definitions and remove if they use benchmark fixture."""

0 commit comments

Comments
 (0)