Skip to content

Commit 42fea88

Browse files
committed
Auto merge of #139011 - Zoxc:no-rayon-iters, r=<try>
Remove the use of Rayon iterators This removes the use of Rayon iterators and the use of the `rustc-rayon` crate. `rustc-rayon-core` is still used however. In parallel loops, instead of a Rayon iterator a serial iterator are used to collect items into a `Vec` and we use a parallel loop over its elements using the new `par_slice` function which is built on `rustc-rayon-core`'s `join`. This change makes it easier to bring `rustc-rayon-core` in-tree. Tests using 7 threads: <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Physical Memory</td><td align="right">Physical Memory</td><td align="right">%</th><td align="right">Committed Memory</td><td align="right">Committed Memory</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">0.4827s</td><td align="right">0.4828s</td><td align="right"> 0.02%</td><td align="right">201.23 MiB</td><td align="right">201.31 MiB</td><td align="right"> 0.04%</td><td align="right">279.03 MiB</td><td align="right">279.46 MiB</td><td align="right"> 0.15%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.1443s</td><td align="right">0.1401s</td><td align="right">💚 -2.91%</td><td align="right">126.42 MiB</td><td align="right">126.70 MiB</td><td align="right"> 0.22%</td><td align="right">199.79 MiB</td><td align="right">199.99 MiB</td><td align="right"> 0.10%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">0.3252s</td><td align="right">0.3065s</td><td align="right">💚 -5.78%</td><td align="right">161.87 MiB</td><td align="right">161.78 MiB</td><td align="right"> -0.05%</td><td align="right">229.59 MiB</td><td align="right">230.23 MiB</td><td align="right"> 0.28%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">0.5845s</td><td align="right">0.5876s</td><td align="right"> 0.53%</td><td align="right">197.01 MiB</td><td align="right">196.89 MiB</td><td align="right"> -0.06%</td><td align="right">267.62 MiB</td><td align="right">267.47 MiB</td><td align="right"> -0.06%</td></tr><tr><td>Total</td><td align="right">1.5367s</td><td align="right">1.5169s</td><td align="right">💚 -1.29%</td><td align="right">686.53 MiB</td><td align="right">686.68 MiB</td><td align="right"> 0.02%</td><td align="right">976.04 MiB</td><td align="right">977.14 MiB</td><td align="right"> 0.11%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9796s</td><td align="right">💚 -2.04%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.04%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.12%</td></tr></table> <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Physical Memory</td><td align="right">Physical Memory</td><td align="right">%</th><td align="right">Committed Memory</td><td align="right">Committed Memory</td><td align="right">%</th></tr><tr><td>🟠 <b>clap</b>:debug</td><td align="right">1.6371s</td><td align="right">1.6529s</td><td align="right"> 0.96%</td><td align="right">395.58 MiB</td><td align="right">396.21 MiB</td><td align="right"> 0.16%</td><td align="right">460.98 MiB</td><td align="right">461.52 MiB</td><td align="right"> 0.12%</td></tr><tr><td>🟠 <b>hyper</b>:debug</td><td align="right">0.3248s</td><td align="right">0.3210s</td><td align="right">💚 -1.16%</td><td align="right">155.16 MiB</td><td align="right">155.19 MiB</td><td align="right"> 0.02%</td><td align="right">219.21 MiB</td><td align="right">219.30 MiB</td><td align="right"> 0.04%</td></tr><tr><td>🟠 <b>regex</b>:debug</td><td align="right">1.0148s</td><td align="right">0.9929s</td><td align="right">💚 -2.16%</td><td align="right">297.96 MiB</td><td align="right">295.07 MiB</td><td align="right"> -0.97%</td><td align="right">354.53 MiB</td><td align="right">351.58 MiB</td><td align="right"> -0.83%</td></tr><tr><td>🟠 <b>syn</b>:debug</td><td align="right">1.3614s</td><td align="right">1.3717s</td><td align="right"> 0.76%</td><td align="right">319.10 MiB</td><td align="right">321.19 MiB</td><td align="right"> 0.65%</td><td align="right">378.90 MiB</td><td align="right">381.27 MiB</td><td align="right"> 0.62%</td></tr><tr><td>Total</td><td align="right">4.3381s</td><td align="right">4.3386s</td><td align="right"> 0.01%</td><td align="right">1.14 GiB</td><td align="right">1.14 GiB</td><td align="right"> -0.01%</td><td align="right">1.38 GiB</td><td align="right">1.38 GiB</td><td align="right"> 0.00%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9960s</td><td align="right"> -0.40%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> -0.03%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> -0.01%</td></tr></table>
2 parents 2216f26 + 229e548 commit 42fea88

File tree

14 files changed

+129
-78
lines changed

14 files changed

+129
-78
lines changed

Cargo.lock

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,17 +3122,6 @@ dependencies = [
31223122
"tikv-jemalloc-sys",
31233123
]
31243124

3125-
[[package]]
3126-
name = "rustc-rayon"
3127-
version = "0.5.1"
3128-
source = "registry+https://github.com/rust-lang/crates.io-index"
3129-
checksum = "2cd9fb077db982d7ceb42a90471e5a69a990b58f71e06f0d8340bb2cf35eb751"
3130-
dependencies = [
3131-
"either",
3132-
"indexmap",
3133-
"rustc-rayon-core",
3134-
]
3135-
31363125
[[package]]
31373126
name = "rustc-rayon-core"
31383127
version = "0.5.0"
@@ -3504,7 +3493,7 @@ dependencies = [
35043493
"parking_lot",
35053494
"portable-atomic",
35063495
"rustc-hash 2.1.1",
3507-
"rustc-rayon",
3496+
"rustc-rayon-core",
35083497
"rustc-stable-hash",
35093498
"rustc_arena",
35103499
"rustc_graphviz",
@@ -3850,7 +3839,6 @@ dependencies = [
38503839
name = "rustc_interface"
38513840
version = "0.0.0"
38523841
dependencies = [
3853-
"rustc-rayon",
38543842
"rustc-rayon-core",
38553843
"rustc_abi",
38563844
"rustc_ast",

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -728,26 +728,34 @@ pub(crate) fn run_aot(
728728

729729
let concurrency_limiter = IntoDynSyncSend(ConcurrencyLimiter::new(todo_cgus.len()));
730730

731-
let modules = tcx.sess.time("codegen mono items", || {
732-
let mut modules: Vec<_> = par_map(todo_cgus, |(_, cgu)| {
733-
let dep_node = cgu.codegen_dep_node(tcx);
734-
tcx.dep_graph
735-
.with_task(
736-
dep_node,
737-
tcx,
738-
(global_asm_config.clone(), cgu.name(), concurrency_limiter.acquire(tcx.dcx())),
739-
module_codegen,
740-
Some(rustc_middle::dep_graph::hash_result),
731+
let modules: Vec<_> =
732+
tcx.sess.time("codegen mono items", || {
733+
let modules: Vec<_> = par_map(todo_cgus, |(_, cgu)| {
734+
let dep_node = cgu.codegen_dep_node(tcx);
735+
IntoDynSyncSend(
736+
tcx.dep_graph
737+
.with_task(
738+
dep_node,
739+
tcx,
740+
(
741+
global_asm_config.clone(),
742+
cgu.name(),
743+
concurrency_limiter.acquire(tcx.dcx()),
744+
),
745+
module_codegen,
746+
Some(rustc_middle::dep_graph::hash_result),
747+
)
748+
.0,
741749
)
742-
.0
743-
});
744-
modules.extend(
745-
done_cgus
750+
});
751+
modules
746752
.into_iter()
747-
.map(|(_, cgu)| OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))),
748-
);
749-
modules
750-
});
753+
.map(|module| module.0)
754+
.chain(done_cgus.into_iter().map(|(_, cgu)| {
755+
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
756+
}))
757+
.collect()
758+
});
751759

752760
let allocator_module = emit_allocator_module(tcx);
753761

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, AllocatorKind, global_fn_n
1010
use rustc_attr_parsing::OptimizeAttr;
1111
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1212
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
13-
use rustc_data_structures::sync::par_map;
13+
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
1414
use rustc_data_structures::unord::UnordMap;
1515
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1616
use rustc_hir::lang_items::LangItem;
@@ -757,7 +757,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
757757

758758
let pre_compiled_cgus = par_map(cgus, |(i, _)| {
759759
let module = backend.compile_codegen_unit(tcx, codegen_units[i].name());
760-
(i, module)
760+
(i, IntoDynSyncSend(module))
761761
});
762762

763763
total_codegen_time += start_time.elapsed();
@@ -777,7 +777,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
777777
match cgu_reuse {
778778
CguReuse::No => {
779779
let (module, cost) = if let Some(cgu) = pre_compiled_cgus.remove(&i) {
780-
cgu
780+
cgu.0
781781
} else {
782782
let start_time = Instant::now();
783783
let module = backend.compile_codegen_unit(tcx, cgu.name());

compiler/rustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ indexmap = "2.4.0"
1414
jobserver_crate = { version = "0.1.28", package = "jobserver" }
1515
measureme = "12.0.1"
1616
rustc-hash = "2.0.0"
17-
rustc-rayon = { version = "0.5.1", features = ["indexmap"] }
17+
rustc-rayon-core = { version = "0.5.0" }
1818
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
1919
rustc_arena = { path = "../rustc_arena" }
2020
rustc_graphviz = { path = "../rustc_graphviz" }

compiler/rustc_data_structures/src/marker.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ impl<T> FromDyn<T> {
179179
FromDyn(val)
180180
}
181181

182+
#[inline(always)]
183+
pub fn derive<O>(&self, val: O) -> FromDyn<O> {
184+
// We already did the check for `sync::is_dyn_thread_safe()` when creating `Self`
185+
FromDyn(val)
186+
}
187+
182188
#[inline(always)]
183189
pub fn into_inner(self) -> T {
184190
self.0
@@ -200,6 +206,13 @@ impl<T> std::ops::Deref for FromDyn<T> {
200206
}
201207
}
202208

209+
impl<T> std::ops::DerefMut for FromDyn<T> {
210+
#[inline(always)]
211+
fn deref_mut(&mut self) -> &mut Self::Target {
212+
&mut self.0
213+
}
214+
}
215+
203216
// A wrapper to convert a struct that is already a `Send` or `Sync` into
204217
// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
205218
// it automatically in some cases. (e.g. Box<dyn Send / Sync>)

compiler/rustc_data_structures/src/sync/parallel.rs

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::any::Any;
77
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
88

99
use parking_lot::Mutex;
10-
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelIterator};
1110

1211
use crate::FatalErrorMarker;
1312
use crate::sync::{DynSend, DynSync, FromDyn, IntoDynSyncSend, mode};
@@ -97,11 +96,11 @@ macro_rules! parallel {
9796
// This function only works when `mode::is_dyn_thread_safe()`.
9897
pub fn scope<'scope, OP, R>(op: OP) -> R
9998
where
100-
OP: FnOnce(&rayon::Scope<'scope>) -> R + DynSend,
99+
OP: FnOnce(&rayon_core::Scope<'scope>) -> R + DynSend,
101100
R: DynSend,
102101
{
103102
let op = FromDyn::from(op);
104-
rayon::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
103+
rayon_core::scope(|s| FromDyn::from(op.into_inner()(s))).into_inner()
105104
}
106105

107106
#[inline]
@@ -114,7 +113,7 @@ where
114113
let oper_a = FromDyn::from(oper_a);
115114
let oper_b = FromDyn::from(oper_b);
116115
let (a, b) = parallel_guard(|guard| {
117-
rayon::join(
116+
rayon_core::join(
118117
move || guard.run(move || FromDyn::from(oper_a.into_inner()())),
119118
move || guard.run(move || FromDyn::from(oper_b.into_inner()())),
120119
)
@@ -125,56 +124,99 @@ where
125124
}
126125
}
127126

128-
pub fn par_for_each_in<I, T: IntoIterator<Item = I> + IntoParallelIterator<Item = I>>(
127+
fn par_slice<I: DynSend>(
128+
items: &mut [I],
129+
guard: &ParallelGuard,
130+
for_each: impl Fn(&mut I) + DynSync + DynSend,
131+
) {
132+
struct State<'a, F> {
133+
for_each: FromDyn<F>,
134+
guard: &'a ParallelGuard,
135+
group: usize,
136+
}
137+
138+
fn par_rec<I: DynSend, F: Fn(&mut I) + DynSync + DynSend>(
139+
items: &mut [I],
140+
state: &State<'_, F>,
141+
) {
142+
if items.len() <= state.group {
143+
for item in items {
144+
state.guard.run(|| (state.for_each)(item));
145+
}
146+
} else {
147+
let (left, right) = items.split_at_mut(items.len() / 2);
148+
let mut left = state.for_each.derive(left);
149+
let mut right = state.for_each.derive(right);
150+
rayon_core::join(move || par_rec(*left, state), move || par_rec(*right, state));
151+
}
152+
}
153+
154+
let state = State {
155+
for_each: FromDyn::from(for_each),
156+
guard,
157+
group: std::cmp::max(items.len() / 128, 1),
158+
};
159+
par_rec(items, &state)
160+
}
161+
162+
pub fn par_for_each_in<I: DynSend, T: IntoIterator<Item = I>>(
129163
t: T,
130-
for_each: impl Fn(I) + DynSync + DynSend,
164+
for_each: impl Fn(&I) + DynSync + DynSend,
131165
) {
132166
parallel_guard(|guard| {
133167
if mode::is_dyn_thread_safe() {
134-
let for_each = FromDyn::from(for_each);
135-
t.into_par_iter().for_each(|i| {
136-
guard.run(|| for_each(i));
137-
});
168+
let mut items: Vec<_> = t.into_iter().collect();
169+
par_slice(&mut items, guard, |i| for_each(&*i))
138170
} else {
139171
t.into_iter().for_each(|i| {
140-
guard.run(|| for_each(i));
172+
guard.run(|| for_each(&i));
141173
});
142174
}
143175
});
144176
}
145177

146-
pub fn try_par_for_each_in<
147-
T: IntoIterator + IntoParallelIterator<Item = <T as IntoIterator>::Item>,
148-
E: Send,
149-
>(
178+
pub fn try_par_for_each_in<T: IntoIterator, E: DynSend>(
150179
t: T,
151-
for_each: impl Fn(<T as IntoIterator>::Item) -> Result<(), E> + DynSync + DynSend,
152-
) -> Result<(), E> {
180+
for_each: impl Fn(&<T as IntoIterator>::Item) -> Result<(), E> + DynSync + DynSend,
181+
) -> Result<(), E>
182+
where
183+
<T as IntoIterator>::Item: DynSend,
184+
{
153185
parallel_guard(|guard| {
154186
if mode::is_dyn_thread_safe() {
155-
let for_each = FromDyn::from(for_each);
156-
t.into_par_iter()
157-
.filter_map(|i| guard.run(|| for_each(i)))
158-
.reduce(|| Ok(()), Result::and)
187+
let mut items: Vec<_> = t.into_iter().collect();
188+
189+
let error = Mutex::new(None);
190+
191+
par_slice(&mut items, guard, |i| {
192+
if let Err(err) = for_each(&*i) {
193+
*error.lock() = Some(err);
194+
}
195+
});
196+
197+
if let Some(err) = error.into_inner() { Err(err) } else { Ok(()) }
159198
} else {
160-
t.into_iter().filter_map(|i| guard.run(|| for_each(i))).fold(Ok(()), Result::and)
199+
t.into_iter().filter_map(|i| guard.run(|| for_each(&i))).fold(Ok(()), Result::and)
161200
}
162201
})
163202
}
164203

165-
pub fn par_map<
166-
I,
167-
T: IntoIterator<Item = I> + IntoParallelIterator<Item = I>,
168-
R: std::marker::Send,
169-
C: FromIterator<R> + FromParallelIterator<R>,
170-
>(
204+
pub fn par_map<I: DynSend, T: IntoIterator<Item = I>, R: DynSend, C: FromIterator<R>>(
171205
t: T,
172206
map: impl Fn(I) -> R + DynSync + DynSend,
173207
) -> C {
174208
parallel_guard(|guard| {
175209
if mode::is_dyn_thread_safe() {
176210
let map = FromDyn::from(map);
177-
t.into_par_iter().filter_map(|i| guard.run(|| map(i))).collect()
211+
212+
let mut items: Vec<(Option<I>, Option<R>)> =
213+
t.into_iter().map(|i| (Some(i), None)).collect();
214+
215+
par_slice(&mut items, guard, |i| {
216+
i.1 = Some(map(i.0.take().unwrap()));
217+
});
218+
219+
items.into_iter().filter_map(|i| i.1).collect()
178220
} else {
179221
t.into_iter().filter_map(|i| guard.run(|| map(i))).collect()
180222
}

compiler/rustc_interface/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
rustc-rayon = { version = "0.5.0" }
98
rustc-rayon-core = { version = "0.5.0" }
109
rustc_ast = { path = "../rustc_ast" }
1110
rustc_ast_lowering = { path = "../rustc_ast_lowering" }

compiler/rustc_interface/src/util.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
179179
let current_gcx = FromDyn::from(CurrentGcx::new());
180180
let current_gcx2 = current_gcx.clone();
181181

182-
let builder = rayon::ThreadPoolBuilder::new()
182+
let builder = rayon_core::ThreadPoolBuilder::new()
183183
.thread_name(|_| "rustc".to_string())
184184
.acquire_thread_handler(jobserver::acquire_thread)
185185
.release_thread_handler(jobserver::release_thread)
@@ -234,7 +234,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
234234
builder
235235
.build_scoped(
236236
// Initialize each new worker thread when created.
237-
move |thread: rayon::ThreadBuilder| {
237+
move |thread: rayon_core::ThreadBuilder| {
238238
// Register the thread for use with the `WorkerLocal` type.
239239
registry.register();
240240

@@ -243,7 +243,9 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
243243
})
244244
},
245245
// Run `f` on the first thread in the thread pool.
246-
move |pool: &rayon::ThreadPool| pool.install(|| f(current_gcx.into_inner())),
246+
move |pool: &rayon_core::ThreadPool| {
247+
pool.install(|| f(current_gcx.into_inner()))
248+
},
247249
)
248250
.unwrap()
249251
})

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
21922192
}
21932193

21942194
let reachable_set = tcx.reachable_set(());
2195-
par_for_each_in(tcx.mir_keys(()), |&def_id| {
2195+
par_for_each_in(tcx.mir_keys(()), |&&def_id| {
21962196
let (encode_const, encode_opt) = should_encode_mir(tcx, reachable_set, def_id);
21972197

21982198
if encode_const {

compiler/rustc_middle/src/hir/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl<'tcx> TyCtxt<'tcx> {
342342

343343
#[inline]
344344
pub fn par_hir_body_owners(self, f: impl Fn(LocalDefId) + DynSend + DynSync) {
345-
par_for_each_in(&self.hir_crate_items(()).body_owners[..], |&def_id| f(def_id));
345+
par_for_each_in(&self.hir_crate_items(()).body_owners[..], |&&def_id| f(def_id));
346346
}
347347

348348
pub fn hir_ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {

0 commit comments

Comments
 (0)