⚡️ Speed up method AsyncLoggingQueue._fetch_batch_from_queue by 21%
#172
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.
📄 21% (0.21x) speedup for
AsyncLoggingQueue._fetch_batch_from_queueinmlflow/utils/async_logging/async_logging_queue.py⏱️ Runtime :
929 microseconds→769 microseconds(best of106runs)📝 Explanation and details
The optimized code achieves a 20% speedup by replacing inefficient queue polling with a more performant batch draining approach and eliminating redundant computations.
Key optimizations:
Queue draining strategy: Instead of using
queue.qsize()estimate and iterativequeue.get()calls, the code now drains the entire queue upfront usingget_nowait()in a try/except loop. This eliminates the unreliable queue size estimation and reduces queue locking overhead from multiple individual operations to a single batch drain.Precomputed length calculations: The original code repeatedly computed
len(merged_batch.metrics + merged_batch.params + merged_batch.tags)which creates temporary concatenated lists. The optimization precomputes individual lengths once per iteration and reuses them, avoiding expensive list concatenation operations.Reduced queue interaction: The original approach made multiple
queue.empty()andqueue.get()calls within the loop, each requiring thread synchronization. The optimized version minimizes this to a single draining phase, reducing locking contention.Performance characteristics from test results:
The changes maintain identical functionality while significantly improving performance for typical async logging scenarios where multiple batches need to be processed and merged efficiently.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-AsyncLoggingQueue._fetch_batch_from_queue-mhx3f6weand push.