Commit 9776836
authored
⚡️ Speed up function
The optimization replaces dynamic object creation with a pre-instantiated constant. Instead of creating a new `CodeflashError` object on every function call, the optimized version creates the error object once at module load time (`_TEST_CONFIDENCE_ERROR`) and returns that same instance from the function.
**Key changes:**
- Added module-level constant `_TEST_CONFIDENCE_ERROR` containing the pre-created error object
- Function now simply returns the pre-existing object instead of constructing a new one
**Why this is faster:**
- **Eliminates object instantiation overhead**: The original version calls `CodeflashError.__init__()` and allocates memory for a new object on every call (2591.1ns per hit). The optimized version just returns a reference to an existing object (741ns per hit).
- **Reduces string handling**: The constructor arguments (error code and message strings) are processed only once at module load rather than on every function call.
- **Memory efficiency**: Only one error object exists in memory rather than potentially many identical instances.
**Performance gains by test type:**
The 64% speedup is consistent across all test scenarios, with individual test calls showing 90-160% improvements (781ns→371ns, 722ns→331ns, etc.). This optimization is particularly effective for:
- High-frequency error creation scenarios
- Functions that return constant error objects
- Cases where the error object's immutable nature makes instance reuse safe
The optimization maintains identical behavior since `CodeflashError` objects with the same parameters are functionally equivalent, and the tests confirm the returned object has the correct properties.test_confidence_threshold_not_met_error by 64% in PR #695 (enhancement/codeflash-errors)1 parent 13b5284 commit 9776836
1 file changed
+5
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
| |||
70 | 74 | | |
71 | 75 | | |
72 | 76 | | |
73 | | - | |
| 77 | + | |
74 | 78 | | |
75 | 79 | | |
76 | 80 | | |
| |||
0 commit comments