Skip to content

Commit

Permalink
Merge pull request #15 from hnez/hashset-entry
Browse files Browse the repository at this point in the history
jobs/machines: manager: simplify logic using HashMap::entry()
  • Loading branch information
hnez authored Aug 27, 2024
2 parents 1b5fa6f + 175f056 commit 89d3863
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
7 changes: 1 addition & 6 deletions src/jobs/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ impl Manager {
let oar = job.triplet().clone().into_owner_and_repo();
let run_id = job.run_id();

if let Some(run_ids) = res.get_mut(&oar) {
run_ids.insert(run_id);
} else {
let run_ids = HashSet::from_iter([run_id]);
res.insert(oar, run_ids);
}
res.entry(oar).or_default().insert(run_id);
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/machines/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ impl Manager {
let mut demand: HashMap<Triplet, u64> = HashMap::new();

for triplet in requested {
if let Some(count) = demand.get_mut(triplet) {
*count += 1
} else {
demand.insert(triplet.clone(), 1);
}
let count = demand.entry(triplet.clone()).or_insert(0);

*count += 1;
}

debug!("Updating the machine demand with:");
Expand Down Expand Up @@ -148,17 +146,13 @@ impl Manager {
let cfg = self.config.get();

for (triplet, count) in demand {
if !machines.contains_key(&triplet) {
machines.insert(triplet.clone(), Vec::new());
}

for _ in 0..count {
let cfg = cfg.clone();
let auth = self.auth.clone();
let rescheduler = self.rescheduler();

if let Some(m) = Machine::new(cfg, auth, rescheduler, triplet.clone()) {
machines.get_mut(&triplet).unwrap().push(m);
machines.entry(triplet.clone()).or_default().push(m);
}
}
}
Expand Down

0 comments on commit 89d3863

Please sign in to comment.