download_queue: wake on completed downloads#23107
Merged
Merged
Conversation
- The parallel fetch loop paused a fixed 50ms per iteration, even when every download had already finished: `Kernel#sleep` was 27% of wall samples (~100ms) in `brew prof --stackprof fetch` for an already-cached formula, the largest single frame ahead of `Kernel#require`. - Warm-cache downloads resolve in microseconds, so almost all of that sleeping was wasted on every `brew fetch`, `brew install` and `brew upgrade`, interactive or scripted, and even the final iteration slept after the last download had finished. - Warm-cache `brew fetch` of a cached formula drops from 0.63-0.70s to 0.45-0.52s (~200ms, ~25-30%), measured both on a TTY and piped, and `Kernel#sleep` disappears from its profile entirely. - Completed downloads are reported as they finish instead of on the next 50ms poll tick. - Skip the end-of-iteration pause entirely when the partition left no downloads pending; previously even the final iteration slept. - Wait on a `Concurrent::Event` signalled by an `on_resolution!` callback on every download future instead of sleeping, so warm caches never pay a poll interval. - On animated TTYs the wait times out after 50ms, purely to keep redrawing the spinner and progress bars at a steady cadence while downloads are in flight; otherwise after 1s as a safety net. - The event is reset before each wait and the remaining futures are re-checked in between, so a download resolving between the partition and the reset cannot cause a lost wakeup.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Homebrew::DownloadQueue’s parallel fetch loop by replacing a fixed poll-and-sleep cadence with an event-driven wakeup when any download future resolves, eliminating wasted sleep time in warm-cache scenarios and reporting completions sooner.
Changes:
- Replace the fixed
sleep 0.05polling loop with aConcurrent::Eventsignaled byFuture#on_resolution!, waking immediately on completion. - Preserve spinner/progress redraw cadence on TTYs via a short wait timeout, while using a longer timeout for non-TTY output.
- Add an RSpec example asserting the queue no longer calls
sleepin the parallel fetch path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Library/Homebrew/download_queue.rb | Switches the parallel fetch loop from fixed-interval sleeping to an event-driven wait signaled by download future resolution. |
| Library/Homebrew/test/download_queue_spec.rb | Adds a spec ensuring the queue wakes on completion without calling sleep (non-TTY mode). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
p-linnane
approved these changes
Jul 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Kernel#sleepwas 27% of wall samples (~100ms) inbrew prof --stackprof fetchfor an already-cached formula, the largest single frame ahead ofKernel#require.brew fetch,brew installandbrew upgrade, interactive or scripted, and even the final iteration slept after the last download had finished.brew fetchof a cached formula drops from 0.63-0.70s to 0.45-0.52s (~200ms, ~25-30%), measured both on a TTY and piped, andKernel#sleepdisappears from its profile entirely.Concurrent::Eventsignalled by anon_resolution!callback on every download future instead of sleeping, so warm caches never pay a poll interval.brewcommands to reproduce the bug?brew lgtm(style, typechecking and tests) locally?Anthropic Fable 5 max with local review and testing.