Skip to content

Commit

Permalink
refact: use rayon scope, avoid channel on generate stage
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Mar 19, 2024
1 parent 16942b3 commit fd2bd6b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
8 changes: 1 addition & 7 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Instant, UNIX_EPOCH};

Expand Down Expand Up @@ -377,14 +376,9 @@ impl Compiler {
}
let result = {
mako_core::mako_profile_scope!("Generate Stage");
let (rs, rr) = channel::<Result<()>>();
// need to put all rayon parallel iterators run in the existed scope, or else rayon
// will create a new thread pool for those parallel iterators
thread_pool::install(|| {
let res = self.generate();
rs.send(res).unwrap();
});
rr.recv().unwrap()
thread_pool::scope(|_| self.generate())
};
let t_compiler_duration = t_compiler.elapsed();
if result.is_ok() {
Expand Down
8 changes: 4 additions & 4 deletions crates/mako/src/thread_pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::OnceLock;

use mako_core::rayon::{ThreadPool, ThreadPoolBuilder};
use mako_core::rayon::{Scope, ThreadPool, ThreadPoolBuilder};

static THREAD_POOL: OnceLock<ThreadPool> = OnceLock::new();

Expand All @@ -18,10 +18,10 @@ where
THREAD_POOL.get_or_init(build_rayon_thread_pool).spawn(func)
}

pub fn install<OP, R>(op: OP) -> R
pub fn scope<'scope, OP, R>(op: OP) -> R
where
OP: FnOnce() -> R + Send,
OP: FnOnce(&Scope<'scope>) -> R + Send,
R: Send,
{
THREAD_POOL.get_or_init(build_rayon_thread_pool).install(op)
THREAD_POOL.get_or_init(build_rayon_thread_pool).scope(op)
}

0 comments on commit fd2bd6b

Please sign in to comment.