Skip to content

Commit 4c37f6d

Browse files
committed
Auto merge of #152375 - Zoxc:rayon-scope-loops, r=jieyouxu,lqd
Use `scope` for `par_slice` instead of `join` This uses `scope` instead of nested `join`s in `par_slice` so that each group of items are independent and do not end up blocking on another.
2 parents ce0bf0b + 91a6126 commit 4c37f6d

1 file changed

Lines changed: 14 additions & 27 deletions

File tree

compiler/rustc_data_structures/src/sync/parallel.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,34 +128,21 @@ fn par_slice<I: DynSend>(
128128
guard: &ParallelGuard,
129129
for_each: impl Fn(&mut I) + DynSync + DynSend,
130130
) {
131-
struct State<'a, F> {
132-
for_each: FromDyn<F>,
133-
guard: &'a ParallelGuard,
134-
group: usize,
135-
}
136-
137-
fn par_rec<I: DynSend, F: Fn(&mut I) + DynSync + DynSend>(
138-
items: &mut [I],
139-
state: &State<'_, F>,
140-
) {
141-
if items.len() <= state.group {
142-
for item in items {
143-
state.guard.run(|| (state.for_each)(item));
144-
}
145-
} else {
146-
let (left, right) = items.split_at_mut(items.len() / 2);
147-
let mut left = state.for_each.derive(left);
148-
let mut right = state.for_each.derive(right);
149-
rustc_thread_pool::join(move || par_rec(*left, state), move || par_rec(*right, state));
131+
let for_each = FromDyn::from(for_each);
132+
let mut items = for_each.derive(items);
133+
rustc_thread_pool::scope(|s| {
134+
let proof = items.derive(());
135+
let group_size = std::cmp::max(items.len() / 128, 1);
136+
for group in items.chunks_exact_mut(group_size) {
137+
let group = proof.derive(group);
138+
s.spawn(|_| {
139+
let mut group = group;
140+
for i in group.iter_mut() {
141+
guard.run(|| for_each(i));
142+
}
143+
});
150144
}
151-
}
152-
153-
let state = State {
154-
for_each: FromDyn::from(for_each),
155-
guard,
156-
group: std::cmp::max(items.len() / 128, 1),
157-
};
158-
par_rec(items, &state)
145+
});
159146
}
160147

161148
pub fn par_for_each_in<I: DynSend, T: IntoIterator<Item = I>>(

0 commit comments

Comments
 (0)