Skip to content

Commit 8e38aa0

Browse files
committed
Make par_slice consistent with single-threaded execution
1 parent 76be1cc commit 8e38aa0

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

compiler/rustc_data_structures/src/sync/parallel.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,28 @@ fn par_slice<I: DynSend>(
128128
guard: &ParallelGuard,
129129
for_each: impl Fn(&mut I) + DynSync + DynSend,
130130
) {
131+
match items {
132+
[] => return,
133+
[item] => {
134+
guard.run(|| for_each(item));
135+
return;
136+
}
137+
_ => (),
138+
}
139+
131140
let for_each = FromDyn::from(for_each);
132141
let mut items = for_each.derive(items);
133142
rustc_thread_pool::scope(|s| {
134143
let proof = items.derive(());
135-
let group_size = std::cmp::max(items.len() / 128, 1);
136-
for group in items.chunks_mut(group_size) {
144+
145+
const MAX_GROUP_COUNT: usize = 128;
146+
let group_size = items.len().div_ceil(MAX_GROUP_COUNT);
147+
let groups = items.chunks_mut(group_size);
148+
149+
// Reverse the order of the later functions since Rayon executes them in reverse
150+
// order when using a single thread. This ensures the execution order matches
151+
// that of a single threaded rustc.
152+
for group in groups.rev() {
137153
let group = proof.derive(group);
138154
s.spawn(|_| {
139155
let mut group = group;

0 commit comments

Comments
 (0)