Skip to content

Commit 8c2d6f8

Browse files
⚡️ Speed up function baseline_establishment_failed_error by 15% in PR #695 (enhancement/codeflash-errors)
The key optimization is replacing `**locals()` with an explicit keyword argument `failure_msg=failure_msg`. The `**locals()` call forces Python to: 1. Create a dictionary containing all local variables in the function scope 2. Unpack that dictionary as keyword arguments to the constructor Since this function only has one local variable (`failure_msg`), the explicit approach avoids the overhead of dictionary creation and unpacking. The line profiler shows the `**locals()` line took 12.6% of total execution time (926544ns per hit) in the original vs 10.2% (686248ns per hit) for the direct assignment - a 26% reduction in that line's cost. This optimization is particularly effective for: - **Frequent function calls** - as shown in the large-scale tests where 1000 calls saw 13.5-16.1% speedups - **Simple error creation patterns** - all test cases showed 21-37% improvements - **Memory-constrained scenarios** - eliminates unnecessary dictionary allocation The 15% overall speedup comes from eliminating Python's introspection overhead while maintaining identical functionality and error object structure.
1 parent 13b5284 commit 8c2d6f8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

codeflash/errors/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def baseline_establishment_failed_error(failure_msg: str) -> CodeflashError:
3939
return CodeflashError(
4040
"BASELINE_ESTABLISHMENT_FAILED_ERROR",
4141
"Failed to establish a baseline for the original code. {failure_msg}",
42-
**locals(),
42+
failure_msg=failure_msg,
4343
)
4444

4545

0 commit comments

Comments
 (0)