⚡️ Speed up function test_confidence_threshold_not_met_error
by 64% in PR #695 (enhancement/codeflash-errors
)
#696
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #695
If you approve this dependent PR, these changes will be merged into the original PR branch
enhancement/codeflash-errors
.📄 64% (0.64x) speedup for
test_confidence_threshold_not_met_error
incodeflash/errors/errors.py
⏱️ Runtime :
27.1 microseconds
→16.5 microseconds
(best of227
runs)📝 Explanation and details
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:
_TEST_CONFIDENCE_ERROR
containing the pre-created error objectWhy this is faster:
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).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:
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.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr695-2025-08-29T14.38.33
and push.