Skip to content

Commit beccbef

Browse files
committed
Remove the use of Rayon iterators
1 parent 48994b1 commit beccbef

File tree

13 files changed

+128
-77
lines changed

13 files changed

+128
-77
lines changed

Cargo.lock

+1-13
Original file line numberDiff line numberDiff line change
@@ -3154,17 +3154,6 @@ dependencies = [
31543154
"tikv-jemalloc-sys",
31553155
]
31563156

3157-
[[package]]
3158-
name = "rustc-rayon"
3159-
version = "0.5.1"
3160-
source = "registry+https://github.com/rust-lang/crates.io-index"
3161-
checksum = "2cd9fb077db982d7ceb42a90471e5a69a990b58f71e06f0d8340bb2cf35eb751"
3162-
dependencies = [
3163-
"either",
3164-
"indexmap",
3165-
"rustc-rayon-core",
3166-
]
3167-
31683157
[[package]]
31693158
name = "rustc-rayon-core"
31703159
version = "0.5.0"
@@ -3536,7 +3525,7 @@ dependencies = [
35363525
"parking_lot",
35373526
"portable-atomic",
35383527
"rustc-hash 2.1.1",
3539-
"rustc-rayon",
3528+
"rustc-rayon-core",
35403529
"rustc-stable-hash",
35413530
"rustc_arena",
35423531
"rustc_graphviz",
@@ -3882,7 +3871,6 @@ dependencies = [
38823871
name = "rustc_interface"
38833872
version = "0.0.0"
38843873
dependencies = [
3885-
"rustc-rayon",
38863874
"rustc-rayon-core",
38873875
"rustc_abi",
38883876
"rustc_ast",

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+26-18
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

+3-3
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

+1-1
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

+13
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

+71-29
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

-1
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

+5-3
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)
@@ -223,7 +223,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
223223
builder
224224
.build_scoped(
225225
// Initialize each new worker thread when created.
226-
move |thread: rayon::ThreadBuilder| {
226+
move |thread: rayon_core::ThreadBuilder| {
227227
// Register the thread for use with the `WorkerLocal` type.
228228
registry.register();
229229

@@ -232,7 +232,9 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
232232
})
233233
},
234234
// Run `f` on the first thread in the thread pool.
235-
move |pool: &rayon::ThreadPool| pool.install(|| f(current_gcx.into_inner())),
235+
move |pool: &rayon_core::ThreadPool| {
236+
pool.install(|| f(current_gcx.into_inner()))
237+
},
236238
)
237239
.unwrap()
238240
})

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
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

+1-1
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)