Skip to content

Commit e33599c

Browse files
SparrowLiideltragon
and
deltragon
committed
refactor WorkerLocal into enum
Co-authored-by: SparrowLii <[email protected]> Co-authored-by: deltragon <[email protected]>
1 parent 84dd6df commit e33599c

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

compiler/rustc_data_structures/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ indexmap = { version = "1.9.1" }
1414
jobserver_crate = { version = "0.1.13", package = "jobserver" }
1515
libc = "0.2"
1616
measureme = "10.0.0"
17-
rayon-core = { version = "0.4.0", package = "rustc-rayon-core", optional = true }
17+
rayon-core = { version = "0.4.0", package = "rustc-rayon-core" }
1818
rayon = { version = "0.4.0", package = "rustc-rayon", optional = true }
1919
rustc_graphviz = { path = "../rustc_graphviz" }
2020
rustc-hash = "1.1.0"
@@ -50,4 +50,4 @@ features = [
5050
memmap2 = "0.2.1"
5151

5252
[features]
53-
rustc_use_parallel_compiler = ["indexmap/rustc-rayon", "rayon", "rayon-core"]
53+
rustc_use_parallel_compiler = ["indexmap/rustc-rayon", "rayon"]

compiler/rustc_data_structures/src/sync.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub use vec::AppendOnlyVec;
3030

3131
mod vec;
3232

33+
static PARALLEL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
34+
3335
cfg_if! {
3436
if #[cfg(not(parallel_compiler))] {
3537
pub auto trait Send {}
@@ -182,33 +184,6 @@ cfg_if! {
182184

183185
use std::cell::Cell;
184186

185-
#[derive(Debug)]
186-
pub struct WorkerLocal<T>(OneThread<T>);
187-
188-
impl<T> WorkerLocal<T> {
189-
/// Creates a new worker local where the `initial` closure computes the
190-
/// value this worker local should take for each thread in the thread pool.
191-
#[inline]
192-
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
193-
WorkerLocal(OneThread::new(f(0)))
194-
}
195-
196-
/// Returns the worker-local value for each thread
197-
#[inline]
198-
pub fn into_inner(self) -> Vec<T> {
199-
vec![OneThread::into_inner(self.0)]
200-
}
201-
}
202-
203-
impl<T> Deref for WorkerLocal<T> {
204-
type Target = T;
205-
206-
#[inline(always)]
207-
fn deref(&self) -> &T {
208-
&self.0
209-
}
210-
}
211-
212187
pub type MTRef<'a, T> = &'a mut T;
213188

214189
#[derive(Debug, Default)]
@@ -328,8 +303,6 @@ cfg_if! {
328303
};
329304
}
330305

331-
pub use rayon_core::WorkerLocal;
332-
333306
pub use rayon::iter::ParallelIterator;
334307
use rayon::iter::IntoParallelIterator;
335308

@@ -364,6 +337,49 @@ cfg_if! {
364337
}
365338
}
366339

340+
#[derive(Debug)]
341+
pub enum WorkerLocal<T> {
342+
SingleThread(T),
343+
Rayon(rayon_core::WorkerLocal<T>),
344+
}
345+
346+
impl<T> WorkerLocal<T> {
347+
/// Creates a new worker local where the `initial` closure computes the
348+
/// value this worker local should take for each thread in the thread pool.
349+
#[inline]
350+
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
351+
if !PARALLEL.load(Ordering::Relaxed) {
352+
WorkerLocal::SingleThread(f(0))
353+
} else {
354+
WorkerLocal::Rayon(rayon_core::WorkerLocal::new(f))
355+
}
356+
}
357+
358+
/// Returns the worker-local value for each thread
359+
#[inline]
360+
pub fn into_inner(self) -> Vec<T> {
361+
match self {
362+
WorkerLocal::SingleThread(inner) => vec![inner],
363+
WorkerLocal::Rayon(mt_inner) => mt_inner.into_inner(),
364+
}
365+
}
366+
}
367+
368+
impl<T> Deref for WorkerLocal<T> {
369+
type Target = T;
370+
371+
#[inline(always)]
372+
fn deref(&self) -> &T {
373+
match self {
374+
WorkerLocal::SingleThread(inner) => inner,
375+
WorkerLocal::Rayon(mt_inner) => mt_inner.deref(),
376+
}
377+
}
378+
}
379+
380+
// Just for speed test
381+
unsafe impl<T: Send> std::marker::Sync for WorkerLocal<T> {}
382+
367383
pub fn assert_sync<T: ?Sized + Sync>() {}
368384
pub fn assert_send<T: ?Sized + Send>() {}
369385
pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}

0 commit comments

Comments
 (0)