From 80ea3c26e54e0a023330882af1be769a69054881 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 14 Feb 2018 23:39:13 -0800 Subject: [PATCH 1/3] Update to rayon 1.0 --- Cargo.toml | 2 +- src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cd87a6f..eec7205 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ travis-ci = { repository = "alexcrichton/cc-rs" } appveyor = { repository = "alexcrichton/cc-rs" } [dependencies] -rayon = { version = "0.9", optional = true } +rayon = { version = "1.0", optional = true } [features] parallel = ["rayon"] diff --git a/src/lib.rs b/src/lib.rs index d5c125c..8f2b6d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -897,13 +897,13 @@ impl Build { fn compile_objects(&self, objs: &[Object]) -> Result<(), Error> { use self::rayon::prelude::*; - let mut cfg = rayon::Configuration::new(); + let mut builder = rayon::ThreadPoolBuilder::new(); if let Ok(amt) = env::var("NUM_JOBS") { if let Ok(amt) = amt.parse() { - cfg = cfg.num_threads(amt); + builder = builder.num_threads(amt); } } - drop(rayon::initialize(cfg)); + drop(builder.build_global()); let results: Mutex>> = Mutex::new(Vec::new()); From 9e9b8b17f829561c8fc4c34128e086536fd61277 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 15 Feb 2018 00:00:31 -0800 Subject: [PATCH 2/3] Only build the threadpool if customized --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8f2b6d2..49ea740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -897,13 +897,13 @@ impl Build { fn compile_objects(&self, objs: &[Object]) -> Result<(), Error> { use self::rayon::prelude::*; - let mut builder = rayon::ThreadPoolBuilder::new(); if let Ok(amt) = env::var("NUM_JOBS") { if let Ok(amt) = amt.parse() { - builder = builder.num_threads(amt); + let _ = rayon::ThreadPoolBuilder::new() + .num_threads(amt) + .build_global(); } } - drop(builder.build_global()); let results: Mutex>> = Mutex::new(Vec::new()); From f95a268167acec4b831134369ac51cf322078660 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 15 Feb 2018 00:08:07 -0800 Subject: [PATCH 3/3] Simplify the parallel Result with collect() --- src/lib.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 49ea740..171d4b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -905,23 +905,11 @@ impl Build { } } - let results: Mutex>> = Mutex::new(Vec::new()); - - objs.par_iter().with_max_len(1).for_each( - |obj| { - let res = self.compile_object(obj); - results.lock().unwrap().push(res) - }, - ); - // Check for any errors and return the first one found. - for result in results.into_inner().unwrap().iter() { - if result.is_err() { - return result.clone(); - } - } - - Ok(()) + objs.par_iter() + .with_max_len(1) + .map(|obj| self.compile_object(obj)) + .collect() } #[cfg(not(feature = "parallel"))]