Commit bd76049
authored
Optimize retry_with_backoff
The optimization replaces blocking `time.sleep()` with non-blocking `await asyncio.sleep()`, which improves **concurrent throughput** despite appearing to increase individual call runtime.
**Key Change:**
- **Replaced `time.sleep(0.0001 * attempt)` with `await asyncio.sleep(0.0001 * attempt)`**
- **Import changed from `time` to `asyncio`**
**Why This Improves Performance:**
The line profiler shows the sleep operation went from 75% of total time (11.635ms) to 28.1% of total time (1.42ms) - an **87% reduction in sleep overhead**. While individual function calls appear slower (42.3ms vs 11.7ms), this is misleading because:
1. **Non-blocking behavior**: `asyncio.sleep()` yields control to the event loop, allowing other async tasks to execute concurrently during backoff periods
2. **Better async integration**: Prevents blocking the entire event loop thread during retries
3. **Improved parallelism**: Multiple retry operations can now overlap their waiting periods
**Throughput Impact:**
The **10.1% throughput improvement** (202,257 → 222,750 ops/sec) demonstrates the real-world benefit. When multiple retry operations run concurrently, the non-blocking sleep allows the event loop to efficiently multiplex between tasks, processing more total operations per second.
**Test Case Performance:**
The optimization particularly benefits test cases with concurrent execution (`test_retry_with_backoff_concurrent_*`, `test_retry_with_backoff_many_concurrent_*`, and throughput tests) where multiple retry operations can now overlap their backoff periods instead of blocking sequentially.
This is a critical fix for any async application where `retry_with_backoff` might be called concurrently, as it prevents the function from becoming a bottleneck that blocks the entire event loop.1 parent 1406409 commit bd76049
1 file changed
+2
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | | - | |
| 15 | + | |
15 | 16 | | |
0 commit comments