Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take priority into account within the pending queue #11032

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ struct DrainState<'cfg> {
/// The list of jobs that we have not yet started executing, but have
/// retrieved from the `queue`. We eagerly pull jobs off the main queue to
/// allow us to request jobserver tokens pretty early.
pending_queue: Vec<(Unit, Job)>,
pending_queue: Vec<(Unit, Job, usize)>,
print: DiagnosticPrinter<'cfg>,

/// How many jobs we've finished
Expand Down Expand Up @@ -576,8 +576,16 @@ impl<'cfg> DrainState<'cfg> {
// possible that can run. Note that this is also the point where we
// start requesting job tokens. Each job after the first needs to
// request a token.
while let Some((unit, job)) = self.queue.dequeue() {
self.pending_queue.push((unit, job));
while let Some((unit, job, priority)) = self.queue.dequeue() {
// We want to keep the pieces of work in the `pending_queue` sorted
// by their priorities, and insert the current job at its correctly
// sorted position: following the lower priority jobs, and the ones
// with the same priority (since they were dequeued before the
// current one, we also keep that relation).
let idx = self
.pending_queue
.partition_point(|&(_, _, p)| p <= priority);
self.pending_queue.insert(idx, (unit, job, priority));
if self.active.len() + self.pending_queue.len() > 1 {
jobserver_helper.request_token();
}
Expand All @@ -586,8 +594,11 @@ impl<'cfg> DrainState<'cfg> {
// Now that we've learned of all possible work that we can execute
// try to spawn it so long as we've got a jobserver token which says
// we're able to perform some parallel work.
// The `pending_queue` is sorted in ascending priority order, and we
// remove items from its end to schedule the highest priority items
// sooner.
while self.has_extra_tokens() && !self.pending_queue.is_empty() {
let (unit, job) = self.pending_queue.remove(0);
let (unit, job, _) = self.pending_queue.pop().unwrap();
*self.counts.get_mut(&unit.pkg.package_id()).unwrap() -= 1;
if !cx.bcx.build_config.build_plan {
// Print out some nice progress information.
Expand Down
28 changes: 14 additions & 14 deletions src/cargo/util/dependency_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ impl<N: Hash + Eq + Clone, E: Eq + Hash + Clone, V> DependencyQueue<N, E, V> {
///
/// A package is ready to be built when it has 0 un-built dependencies. If
/// `None` is returned then no packages are ready to be built.
pub fn dequeue(&mut self) -> Option<(N, V)> {
let key = self
pub fn dequeue(&mut self) -> Option<(N, V, usize)> {
let (key, priority) = self
.dep_map
.iter()
.filter(|(_, (deps, _))| deps.is_empty())
.map(|(key, _)| key.clone())
.max_by_key(|k| self.priority[k])?;
.map(|(key, _)| (key.clone(), self.priority[key]))
.max_by_key(|(_, priority)| *priority)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This max_by_key is unnecessary as we are removing all and then sorting. If we change it to .next(), only the test will need to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can remove it if you want ? It doesn't seem to hurt today, and makes the order at least deterministic.

Copy link
Contributor

Choose a reason for hiding this comment

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

I got nerd sniped, but it doesn't matter. Leave it bee.

let (_, data) = self.dep_map.remove(&key).unwrap();
Some((key, data))
Some((key, data, priority))
}

/// Returns `true` if there are remaining packages to be built.
Expand Down Expand Up @@ -213,19 +213,19 @@ mod test {
q.queue(5, (), vec![(4, ()), (3, ())], 1);
q.queue_finished();

assert_eq!(q.dequeue(), Some((1, ())));
assert_eq!(q.dequeue(), Some((3, ())));
assert_eq!(q.dequeue(), Some((1, (), 5)));
assert_eq!(q.dequeue(), Some((3, (), 4)));
assert_eq!(q.dequeue(), None);
q.finish(&3, &());
assert_eq!(q.dequeue(), None);
q.finish(&1, &());
assert_eq!(q.dequeue(), Some((2, ())));
assert_eq!(q.dequeue(), Some((2, (), 4)));
assert_eq!(q.dequeue(), None);
q.finish(&2, &());
assert_eq!(q.dequeue(), Some((4, ())));
assert_eq!(q.dequeue(), Some((4, (), 3)));
assert_eq!(q.dequeue(), None);
q.finish(&4, &());
assert_eq!(q.dequeue(), Some((5, ())));
assert_eq!(q.dequeue(), Some((5, (), 2)));
}

#[test]
Expand All @@ -238,16 +238,16 @@ mod test {
q.queue(4, (), vec![(2, ()), (3, ())], 1);
q.queue_finished();

assert_eq!(q.dequeue(), Some((3, ())));
assert_eq!(q.dequeue(), Some((1, ())));
assert_eq!(q.dequeue(), Some((3, (), 9)));
assert_eq!(q.dequeue(), Some((1, (), 4)));
assert_eq!(q.dequeue(), None);
q.finish(&3, &());
assert_eq!(q.dequeue(), None);
q.finish(&1, &());
assert_eq!(q.dequeue(), Some((2, ())));
assert_eq!(q.dequeue(), Some((2, (), 3)));
assert_eq!(q.dequeue(), None);
q.finish(&2, &());
assert_eq!(q.dequeue(), Some((4, ())));
assert_eq!(q.dequeue(), Some((4, (), 2)));
assert_eq!(q.dequeue(), None);
q.finish(&4, &());
assert_eq!(q.dequeue(), None);
Expand Down