active_set.rs 978 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use rustc_hash::FxHashSet;

use crate::protocols::WorkerWithDpRank;

#[inline]
pub(crate) fn reconcile_active_workers(
    active: &mut FxHashSet<WorkerWithDpRank>,
    next: &FxHashSet<WorkerWithDpRank>,
    mut on_drop: impl FnMut(WorkerWithDpRank),
) {
    let active_count = active.len();
    let next_count = next.len();

    if next_count == active_count {
        return;
    }

    if next_count < active_count && next.iter().all(|worker| active.contains(worker)) {
        for &worker in active.iter() {
            if !next.contains(&worker) {
                on_drop(worker);
            }
        }
        active.clone_from(next);
        return;
    }

    active.retain(|worker| {
        if next.contains(worker) {
            true
        } else {
            on_drop(*worker);
            false
        }
    });
}