Skip to content

Commit ec4cce9

Browse files
Refactor rustc thread granting loop
1 parent 50a6083 commit ec4cce9

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -416,31 +416,34 @@ impl<'a, 'cfg> DrainState<'a, 'cfg> {
416416
// Now that we've learned of all possible work that we can execute
417417
// try to spawn it so long as we've got a jobserver token which says
418418
// we're able to perform some parallel work.
419-
while self.active.len() < self.tokens.len() + 1 && !self.pending_queue.is_empty() {
419+
while self.has_extra_tokens() && !self.pending_queue.is_empty() {
420420
let (unit, job) = self.pending_queue.remove(0);
421421
self.run(&unit, job, cx, scope)?;
422422
}
423423

424424
Ok(())
425425
}
426426

427+
fn has_extra_tokens(&self) -> bool {
428+
self.active.len() < self.tokens.len() + 1
429+
}
430+
431+
// If we managed to acquire some extra tokens, send them off to a waiting rustc.
427432
fn grant_rustc_token_requests(&mut self) -> CargoResult<()> {
428-
// If we managed to acquire some extra tokens, send them off to a waiting rustc.
429-
let extra_tokens = self.tokens.len() - (self.active.len() - 1);
430-
for _ in 0..extra_tokens {
431-
if !self.to_send_clients.is_empty() {
432-
// remove from the front so we grant the token to the oldest
433-
// waiter
434-
let (id, client) = self.to_send_clients.remove(0);
435-
let token = self.tokens.pop().expect("an extra token");
436-
self.rustc_tokens
437-
.entry(id)
438-
.or_insert_with(Vec::new)
439-
.push(token);
440-
client
441-
.release_raw()
442-
.chain_err(|| "failed to release jobserver token")?;
443-
}
433+
while !self.to_send_clients.is_empty() && self.has_extra_tokens() {
434+
// Remove from the front so we grant the token to the oldest waiter
435+
let (id, client) = self.to_send_clients.remove(0);
436+
// This unwrap is guaranteed to succeed. `active` must be at least
437+
// length 1, as otherwise there can't be a client waiting to be sent
438+
// on, so tokens.len() must also be at least one.
439+
let token = self.tokens.pop().unwrap();
440+
self.rustc_tokens
441+
.entry(id)
442+
.or_insert_with(Vec::new)
443+
.push(token);
444+
client
445+
.release_raw()
446+
.chain_err(|| "failed to release jobserver token")?;
444447
}
445448

446449
Ok(())

0 commit comments

Comments
 (0)