⚡️ Speed up function retry_with_backoff by 48%
#116
Closed
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.
📄 48% (0.48x) speedup for
retry_with_backoffinsrc/async_examples/concurrency.py⏱️ Runtime :
208 milliseconds→141 milliseconds(best of151runs)📝 Explanation and details
The optimization replaces
time.sleep()withawait asyncio.sleep()in the retry backoff mechanism. This change is critical for async code correctness and concurrency.Key Performance Impact:
Why This Optimization Works:
time.sleep()is a blocking synchronous call that freezes the entire event loop, preventing any other async tasks from running during backoff periodsawait asyncio.sleep()is non-blocking and yields control back to the event loop, allowing concurrent execution of other async operationsThroughput Trade-off Explained:
await asyncio.sleep()has slightly more overhead than the blockingtime.sleep()Test Case Performance:
The optimization particularly benefits concurrent test cases like
test_retry_with_backoff_many_concurrent_*andtest_retry_with_backoff_throughput_*where multiple retry operations run simultaneously. Without this fix, all concurrent operations would be serialized by the blocking sleep calls.This is a mandatory fix for async code - using blocking calls in async functions breaks the async execution model.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-retry_with_backoff-mfvq8saeand push.