Skip to content

Introduce a priority queue#9389

Merged
Edouard-chin merged 1 commit intoruby:masterfrom
Shopify:ec-queue-with-priority
Mar 16, 2026
Merged

Introduce a priority queue#9389
Edouard-chin merged 1 commit intoruby:masterfrom
Shopify:ec-queue-with-priority

Conversation

@Edouard-chin
Copy link
Collaborator

@Edouard-chin Edouard-chin commented Mar 12, 2026

What was the end-user or developer problem that led to this PR?

In #9381, I explained the issue about the "tail latency" problem.

TL;DR When a gem with a native extensions is downloaded, the sooner we starts compiling, the sooner it gets installed. If a gem with a native extensions ends up at the end of the queue, the longer bundle install becomes.

Before (Profile recorded from master)

tail-before

After

tail-after

What is your fix for the problem, implemented in this PR?

I'd like to introduce a simple queue with priority. When a gem is downloaded, we check whether that gem has a native extension and if its dependencies are installed. If both conditions are met, we add the gem in the priority queue to be picked up as quickly as possible.

Make sure the following tasks are checked

@Edouard-chin Edouard-chin force-pushed the ec-queue-with-priority branch 3 times, most recently from 20103fc to cc94811 Compare March 12, 2026 01:14
@Edouard-chin Edouard-chin force-pushed the ec-queue-with-priority branch from cc94811 to 1d92d07 Compare March 12, 2026 01:51
- ### Problem

  In ruby#9381, I explained the
  issue about the "tail latency".

  TL;DR When a gem with a native extensions is downloaded, the sooner
  we starts compiling, the sooner it gets installed.
  If a gem with a native extensions ends up at the end of the queue,
  the longer `bundle install` becomes.

  ### Solution

  I'd like to introduce a simple queue with priority. When a gem is
  downloaded, we check whether that gem has a native extension and
  if its dependencies are installed. If both conditions are met,
  we add the gem in the priority queue to be picked up as quickly
  as possible.
@Edouard-chin Edouard-chin force-pushed the ec-queue-with-priority branch from 1d92d07 to c271257 Compare March 12, 2026 11:48
Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small suggestion, otherwise this look good.

end

def enqueue_with_priority?
state == :installable && spec.extensions.any?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since installed is defined above, I think we should use that here.

Suggested change
state == :installable && spec.extensions.any?
installed? && spec.extensions.any?

Copy link
Collaborator Author

@Edouard-chin Edouard-chin Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

installed is a different state that installable.

installed means that the spec is fully installed and there is nothing else to do.
installable means that the spec is downloaded and can be installed immediately (either because its a pure ruby gem, or because it's a native extension gem and its dependencies are installed)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh whoops I read it wrong. 🤦🏼‍♀️

@Edouard-chin
Copy link
Collaborator Author

This change speeds up a freshly generated Rails application by around ~13%

I originally wrote that in the PR description as that's what I was consistently seeing. We now have a set of benchmark where we can run our changes on and it shows a much smaller gain. This change is hard to benchmark correctly since the optimization is purely on improving bundle install speed when compilation take place. Isolating compilation is tricky and will vary a lot from machine to machine.

The slower compilation is on a machine, the bigger the impact this change has on bundle install. I'm going to merge this one as I think it's worth it .

@Edouard-chin Edouard-chin merged commit 115228e into ruby:master Mar 16, 2026
95 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants