Skip to content

Commit 67955e0

Browse files
committed
refactor WorkerLocal
1 parent 2ddd981 commit 67955e0

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-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.3" }
1414
jobserver_crate = { version = "0.1.13", package = "jobserver" }
1515
libc = "0.2"
1616
measureme = "10.0.0"
17-
rustc-rayon-core = { version = "0.5.0", optional = true }
17+
rustc-rayon-core = { version = "0.5.0" }
1818
rustc-rayon = { version = "0.5.0", optional = true }
1919
rustc_graphviz = { path = "../rustc_graphviz" }
2020
rustc-hash = "1.1.0"
@@ -51,4 +51,4 @@ features = [
5151
memmap2 = "0.2.1"
5252

5353
[features]
54-
rustc_use_parallel_compiler = ["indexmap/rustc-rayon", "rustc-rayon", "rustc-rayon-core"]
54+
rustc_use_parallel_compiler = ["indexmap/rustc-rayon", "rustc-rayon"]

compiler/rustc_data_structures/src/sync.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -259,33 +259,6 @@ cfg_if! {
259259

260260
use std::cell::Cell;
261261

262-
#[derive(Debug)]
263-
pub struct WorkerLocal<T>(OneThread<T>);
264-
265-
impl<T> WorkerLocal<T> {
266-
/// Creates a new worker local where the `initial` closure computes the
267-
/// value this worker local should take for each thread in the thread pool.
268-
#[inline]
269-
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
270-
WorkerLocal(OneThread::new(f(0)))
271-
}
272-
273-
/// Returns the worker-local value for each thread
274-
#[inline]
275-
pub fn into_inner(self) -> Vec<T> {
276-
vec![OneThread::into_inner(self.0)]
277-
}
278-
}
279-
280-
impl<T> Deref for WorkerLocal<T> {
281-
type Target = T;
282-
283-
#[inline(always)]
284-
fn deref(&self) -> &T {
285-
&self.0
286-
}
287-
}
288-
289262
pub type MTLockRef<'a, T> = &'a mut MTLock<T>;
290263

291264
#[derive(Debug, Default)]
@@ -445,8 +418,6 @@ cfg_if! {
445418
};
446419
}
447420

448-
pub use rayon_core::WorkerLocal;
449-
450421
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelIterator};
451422

452423
pub fn par_for_each_in<I, T: IntoIterator<Item = I> + IntoParallelIterator<Item = I>>(
@@ -851,6 +822,48 @@ impl<T: Clone> Clone for RwLock<T> {
851822
}
852823
}
853824

825+
#[derive(Debug)]
826+
pub struct WorkerLocal<T> {
827+
single_thread: bool,
828+
inner: T,
829+
mt_inner: Option<rayon_core::WorkerLocal<T>>,
830+
}
831+
832+
impl<T> WorkerLocal<T> {
833+
/// Creates a new worker local where the `initial` closure computes the
834+
/// value this worker local should take for each thread in the thread pool.
835+
#[inline]
836+
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
837+
if !active() {
838+
WorkerLocal { single_thread: true, inner: f(0), mt_inner: None }
839+
} else {
840+
WorkerLocal {
841+
single_thread: false,
842+
inner: unsafe { MaybeUninit::uninit().assume_init() },
843+
mt_inner: Some(rayon_core::WorkerLocal::new(f)),
844+
}
845+
}
846+
}
847+
848+
/// Returns the worker-local value for each thread
849+
#[inline]
850+
pub fn into_inner(self) -> Vec<T> {
851+
if self.single_thread { vec![self.inner] } else { self.mt_inner.unwrap().into_inner() }
852+
}
853+
}
854+
855+
impl<T> Deref for WorkerLocal<T> {
856+
type Target = T;
857+
858+
#[inline(always)]
859+
fn deref(&self) -> &T {
860+
if self.single_thread { &self.inner } else { self.mt_inner.as_ref().unwrap().deref() }
861+
}
862+
}
863+
864+
// Just for speed test
865+
unsafe impl<T: Send> std::marker::Sync for WorkerLocal<T> {}
866+
854867
/// A type which only allows its inner value to be used in one thread.
855868
/// It will panic if it is used on multiple threads.
856869
#[derive(Debug)]

0 commit comments

Comments
 (0)