⚡️ Speed up function _should_retry by 27%
#161
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.
📄 27% (0.27x) speedup for
_should_retryinskyvern/client/core/http_client.py⏱️ Runtime :
687 microseconds→539 microseconds(best of96runs)📝 Explanation and details
The optimization achieves a 27% speedup by eliminating repeated list creation and using a more efficient data structure for membership testing.
Key optimizations:
Moved list creation to module level: The original code created
retryable_400s = [429, 408, 409]on every function call (40.2% of execution time). The optimized version defines_RETRYABLE_400Sas a module-level constant, eliminating this overhead entirely.Changed from list to set: Sets provide O(1) average-case membership testing vs O(n) for lists. While the list only has 3 elements, the set lookup is still more efficient and signals intent better.
Performance impact analysis:
_should_retryis called after every HTTP request to determine if a failed request should be retried>= 500checkWorkload benefits:
The optimization is particularly effective for the common case of successful requests (2xx codes) where the function can short-circuit after the
>= 500check without any list operations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_should_retry-mjau7hpcand push.