Skip to content

Commit 50c564e

Browse files
committed
Make par_slice consistent with single-threaded execution
1 parent 9e0a42c commit 50c564e

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
@@ -129,12 +129,28 @@ fn par_slice<I: DynSend>(
129129
for_each: impl Fn(&mut I) + DynSync + DynSend,
130130
proof: FromDyn<()>,
131131
) {
132+
match items {
133+
[] => return,
134+
[item] => {
135+
guard.run(|| for_each(item));
136+
return;
137+
}
138+
_ => (),
139+
}
140+
132141
let for_each = proof.derive(for_each);
133142
let mut items = for_each.derive(items);
134143
rustc_thread_pool::scope(|s| {
135144
let proof = items.derive(());
136-
let group_size = std::cmp::max(items.len() / 128, 1);
137-
for group in items.chunks_mut(group_size) {
145+
146+
const MAX_GROUP_COUNT: usize = 128;
147+
let group_size = items.len().div_ceil(MAX_GROUP_COUNT);
148+
let groups = items.chunks_mut(group_size);
149+
150+
// Reverse the order of the later functions since Rayon executes them in reverse
151+
// order when using a single thread. This ensures the execution order matches
152+
// that of a single threaded rustc.
153+
for group in groups.rev() {
138154
let group = proof.derive(group);
139155
s.spawn(|_| {
140156
let mut group = group;

0 commit comments

Comments
 (0)