Skip to content

Commit 56475a9

Browse files
⚡️ Speed up function code_context_extraction_failed_error by 14% in PR #695 (enhancement/codeflash-errors)
The optimization replaces `**locals()` with explicit parameter passing (`error=error`) in the `CodeflashError` constructor call. This eliminates the overhead of: 1. **Dictionary creation**: `locals()` creates a new dictionary containing all local variables at runtime 2. **Dictionary unpacking**: The `**` operator unpacks this dictionary into keyword arguments By passing `error=error` explicitly, we avoid these dictionary operations entirely. The line profiler shows this reduces the function's total time from 2.32ms to 2.21ms across test runs. The optimization is particularly effective for: - **High-frequency calls**: The large-scale test with 1000 iterations shows 12.5% speedup - **Long error messages**: Tests with very long strings (1000+ characters) see up to 38% improvement - **All test patterns**: Every test case shows 15-38% speedup, indicating the optimization benefits all usage patterns consistently This is a classic Python performance pattern - avoiding unnecessary dictionary operations when the required parameters are known at compile time.
1 parent 13b5284 commit 56475a9

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
@@ -61,7 +61,7 @@ def no_best_optimization_found_error(function_name: str) -> CodeflashError:
6161

6262
def code_context_extraction_failed_error(error: str) -> CodeflashError:
6363
return CodeflashError(
64-
"CODE_CONTEXT_EXTRACTION_FAILED_ERROR", "Failed to extract code context. Error: {error}.", **locals()
64+
"CODE_CONTEXT_EXTRACTION_FAILED_ERROR", "Failed to extract code context. Error: {error}.", error=error
6565
)
6666

6767

0 commit comments

Comments
 (0)