⚡️ Speed up function fetch_all_users by 786%
#173
+1
−5
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.
📄 786% (7.86x) speedup for
fetch_all_usersinsrc/asynchrony/various.py⏱️ Runtime :
302 milliseconds→34.1 milliseconds(best of250runs)📝 Explanation and details
The optimization replaces sequential async execution with concurrent execution using
asyncio.gather().Key Change:
await fetch_user(user_id), blocking until each completesasyncio.gather(*(fetch_user(user_id) for user_id in user_ids))to launch all fetch operations concurrentlyWhy This Creates a Speedup:
The original code suffers from "false serialization" - it waits for each 0.0001-second database call to complete before starting the next one. With N users, total time is roughly N × 0.0001 seconds. The optimized version launches all fetch operations simultaneously, so total time becomes approximately max(fetch_times) ≈ 0.0001 seconds regardless of list size.
Performance Impact:
The line profiler shows the optimization eliminates the expensive sequential loop overhead - the original
fetch_all_usersspent 96.3% of time waiting on individual fetch calls, while the optimized version completes in a single concurrent operation.Test Case Performance:
The optimization excels with larger datasets - test cases with 100-500 users show the most dramatic improvements since they maximize the concurrency benefit. Small lists (1-10 users) still benefit but see smaller gains due to the fixed asyncio.sleep overhead.
This pattern is particularly valuable for I/O-bound operations like database queries, API calls, or file operations where the underlying operations can run independently.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-fetch_all_users-mhq8hacpand push.