mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
parent
b963e0e502
commit
54b5e28206
1 changed files with 60 additions and 63 deletions
|
@ -37,7 +37,7 @@ const ROC_FILE_EXTENSION: &str = "roc";
|
||||||
const MODULE_SEPARATOR: char = '.';
|
const MODULE_SEPARATOR: char = '.';
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct LoadedModule {
|
pub struct LoadedModule<'a> {
|
||||||
pub module_id: ModuleId,
|
pub module_id: ModuleId,
|
||||||
pub interns: Interns,
|
pub interns: Interns,
|
||||||
pub solved: Solved<Subs>,
|
pub solved: Solved<Subs>,
|
||||||
|
@ -45,7 +45,7 @@ pub struct LoadedModule {
|
||||||
pub type_problems: Vec<solve::TypeError>,
|
pub type_problems: Vec<solve::TypeError>,
|
||||||
pub declarations_by_id: MutMap<ModuleId, Vec<Declaration>>,
|
pub declarations_by_id: MutMap<ModuleId, Vec<Declaration>>,
|
||||||
pub exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
pub exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
||||||
pub src: Box<str>,
|
pub src: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -240,43 +240,43 @@ type MsgReceiver<'a> = Receiver<Msg<'a>>;
|
||||||
/// and then linking them together, and possibly caching them by the hash of their
|
/// and then linking them together, and possibly caching them by the hash of their
|
||||||
/// specializations, so if none of their specializations changed, we don't even need
|
/// specializations, so if none of their specializations changed, we don't even need
|
||||||
/// to rebuild the module and can link in the cached one directly.)
|
/// to rebuild the module and can link in the cached one directly.)
|
||||||
// #[allow(clippy::cognitive_complexity)]
|
#[allow(clippy::cognitive_complexity)]
|
||||||
// pub fn load<'a>(
|
pub fn load<'a>(
|
||||||
// arena: &'a Bump,
|
arena: &'a Bump,
|
||||||
// stdlib: &StdLib,
|
stdlib: &StdLib,
|
||||||
// src_dir: &Path,
|
src_dir: &Path,
|
||||||
// filename: PathBuf,
|
filename: PathBuf,
|
||||||
// exposed_types: SubsByModule,
|
exposed_types: SubsByModule,
|
||||||
// ) -> Result<LoadedModule, LoadingProblem> {
|
) -> Result<LoadedModule<'a>, LoadingProblem> {
|
||||||
// use self::MaybeShared::*;
|
use self::MaybeShared::*;
|
||||||
|
|
||||||
// let (msg_tx, msg_rx) = bounded(1024);
|
let (msg_tx, msg_rx) = bounded(1024);
|
||||||
// let arc_modules = Arc::new(Mutex::new(ModuleIds::default()));
|
let arc_modules = Arc::new(Mutex::new(ModuleIds::default()));
|
||||||
// let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
|
let root_exposed_ident_ids = IdentIds::exposed_builtins(0);
|
||||||
// let ident_ids_by_module = Arc::new(Mutex::new(root_exposed_ident_ids));
|
let ident_ids_by_module = Arc::new(Mutex::new(root_exposed_ident_ids));
|
||||||
|
|
||||||
// // Load the root module synchronously; we can't proceed until we have its id.
|
// Load the root module synchronously; we can't proceed until we have its id.
|
||||||
// let root_id = load_filename(
|
let root_id = load_filename(
|
||||||
// &arena,
|
&arena,
|
||||||
// filename,
|
filename,
|
||||||
// msg_tx.clone(),
|
msg_tx.clone(),
|
||||||
// Shared(Arc::clone(&arc_modules), Arc::clone(&ident_ids_by_module)),
|
Shared(Arc::clone(&arc_modules), Arc::clone(&ident_ids_by_module)),
|
||||||
// // TODO FIXME go back to using Unique here, not Shared
|
// TODO FIXME go back to using Unique here, not Shared
|
||||||
// // Unique(&mut module_ids, &mut root_exposed_ident_ids),
|
// Unique(&mut module_ids, &mut root_exposed_ident_ids),
|
||||||
// )?;
|
)?;
|
||||||
|
|
||||||
// load_deps(
|
load_deps(
|
||||||
// &arena,
|
&arena,
|
||||||
// root_id,
|
root_id,
|
||||||
// msg_tx,
|
msg_tx,
|
||||||
// msg_rx,
|
msg_rx,
|
||||||
// stdlib,
|
stdlib,
|
||||||
// src_dir,
|
src_dir,
|
||||||
// arc_modules,
|
arc_modules,
|
||||||
// ident_ids_by_module,
|
ident_ids_by_module,
|
||||||
// exposed_types,
|
exposed_types,
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
|
|
||||||
/// Add a task to the queue, and notify all the listeners.
|
/// Add a task to the queue, and notify all the listeners.
|
||||||
fn enqueue_task<'a, 'b>(
|
fn enqueue_task<'a, 'b>(
|
||||||
|
@ -295,33 +295,30 @@ fn enqueue_task<'a, 'b>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_deps(
|
fn load_deps<'a>(
|
||||||
|
arena: &'a Bump,
|
||||||
root_id: ModuleId,
|
root_id: ModuleId,
|
||||||
|
msg_tx: MsgSender<'a>,
|
||||||
|
msg_rx: MsgReceiver<'a>,
|
||||||
stdlib: &StdLib,
|
stdlib: &StdLib,
|
||||||
src_dir: &Path,
|
src_dir: &Path,
|
||||||
arc_modules: Arc<Mutex<ModuleIds>>,
|
arc_modules: Arc<Mutex<ModuleIds>>,
|
||||||
ident_ids_by_module: Arc<Mutex<IdentIdsByModule>>,
|
ident_ids_by_module: Arc<Mutex<IdentIdsByModule>>,
|
||||||
exposed_types: SubsByModule,
|
exposed_types: SubsByModule,
|
||||||
) -> Result<LoadedModule, LoadingProblem> {
|
) -> Result<LoadedModule<'a>, LoadingProblem> {
|
||||||
// Reserve one CPU for the main thread, and let all the others be eligible
|
// Reserve one CPU for the main thread, and let all the others be eligible
|
||||||
// to spawn workers.
|
// to spawn workers.
|
||||||
let num_workers = num_cpus::get() - 1;
|
let num_workers = num_cpus::get() - 1;
|
||||||
|
|
||||||
let arena = Bump::new();
|
|
||||||
|
|
||||||
// We'll add tasks to this, and then worker threads will take tasks from it.
|
// We'll add tasks to this, and then worker threads will take tasks from it.
|
||||||
let injector = Injector::new();
|
let injector = Injector::new();
|
||||||
|
|
||||||
// We need to allocate worker *queues* on the main thread and then move them
|
// We need to allocate worker *queues* on the main thread and then move them
|
||||||
// into the worker threads, because those workers' stealers need to be
|
// into the worker threads, because those workers' stealers need to be
|
||||||
// shared bet,een all threads, and this coordination work is much easier
|
// shared between all threads, and this coordination work is much easier
|
||||||
// on the main thread.
|
// on the main thread.
|
||||||
let mut worker_queues = bumpalo::collections::Vec::with_capacity_in(num_workers, &arena);
|
let mut worker_queues = bumpalo::collections::Vec::with_capacity_in(num_workers, arena);
|
||||||
let mut stealers = bumpalo::collections::Vec::with_capacity_in(num_workers, &arena);
|
let mut stealers = bumpalo::collections::Vec::with_capacity_in(num_workers, arena);
|
||||||
|
|
||||||
thread::scope(|thread_scope| {
|
|
||||||
// let arenas = bumpalo::collections::Vec::with_capacity_in(num_workers, &arena);
|
|
||||||
let (msg_tx, msg_rx) = bounded(1024);
|
|
||||||
|
|
||||||
for _ in 0..num_workers {
|
for _ in 0..num_workers {
|
||||||
let worker = Worker::new_lifo();
|
let worker = Worker::new_lifo();
|
||||||
|
@ -334,6 +331,7 @@ fn load_deps(
|
||||||
// reference to each worker. (Slices are Sync, but bumpalo Vecs are not.)
|
// reference to each worker. (Slices are Sync, but bumpalo Vecs are not.)
|
||||||
let stealers = stealers.into_bump_slice();
|
let stealers = stealers.into_bump_slice();
|
||||||
|
|
||||||
|
thread::scope(|thread_scope| {
|
||||||
let mut headers_parsed = MutSet::default();
|
let mut headers_parsed = MutSet::default();
|
||||||
|
|
||||||
// We've already parsed the root's header. (But only its header, so far.)
|
// We've already parsed the root's header. (But only its header, so far.)
|
||||||
|
@ -367,11 +365,10 @@ fn load_deps(
|
||||||
unsolved_modules: MutMap::default(),
|
unsolved_modules: MutMap::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut worker_listeners = bumpalo::collections::Vec::with_capacity_in(num_workers, &arena);
|
let mut worker_listeners = bumpalo::collections::Vec::with_capacity_in(num_workers, arena);
|
||||||
|
|
||||||
for _ in 0..num_workers {
|
for _ in 0..num_workers {
|
||||||
let arena = Bump::new();
|
let arena = Bump::new();
|
||||||
let msg_tx = msg_tx.clone();
|
|
||||||
let worker = worker_queues.pop().unwrap();
|
let worker = worker_queues.pop().unwrap();
|
||||||
let (worker_msg_tx, worker_msg_rx) = bounded(1024);
|
let (worker_msg_tx, worker_msg_rx) = bounded(1024);
|
||||||
|
|
||||||
|
@ -384,6 +381,8 @@ fn load_deps(
|
||||||
|
|
||||||
// Record this thread's handle so the main thread can join it later.
|
// Record this thread's handle so the main thread can join it later.
|
||||||
thread_scope.spawn(move |_| {
|
thread_scope.spawn(move |_| {
|
||||||
|
let msg_tx = msg_tx.clone();
|
||||||
|
|
||||||
// Keep listening until we receive a Shutdown msg
|
// Keep listening until we receive a Shutdown msg
|
||||||
for msg in worker_msg_rx.iter() {
|
for msg in worker_msg_rx.iter() {
|
||||||
match msg {
|
match msg {
|
||||||
|
@ -392,7 +391,7 @@ fn load_deps(
|
||||||
// shut down the thread, so when the main thread
|
// shut down the thread, so when the main thread
|
||||||
// blocks on joining with all the worker threads,
|
// blocks on joining with all the worker threads,
|
||||||
// it can finally exit too!
|
// it can finally exit too!
|
||||||
return arena;
|
return;
|
||||||
}
|
}
|
||||||
WorkerMsg::TaskAdded => {
|
WorkerMsg::TaskAdded => {
|
||||||
// Find a task - either from this thread's queue,
|
// Find a task - either from this thread's queue,
|
||||||
|
@ -417,8 +416,6 @@ fn load_deps(
|
||||||
// Needed to prevent a borrow checker error about this closure
|
// Needed to prevent a borrow checker error about this closure
|
||||||
// outliving its enclosing function.
|
// outliving its enclosing function.
|
||||||
drop(worker_msg_rx);
|
drop(worker_msg_rx);
|
||||||
|
|
||||||
arena
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -782,7 +779,7 @@ fn finish<'a>(
|
||||||
problems: Vec<solve::TypeError>,
|
problems: Vec<solve::TypeError>,
|
||||||
exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
||||||
src: &'a str,
|
src: &'a str,
|
||||||
) -> LoadedModule {
|
) -> LoadedModule<'a> {
|
||||||
state.type_problems.extend(problems);
|
state.type_problems.extend(problems);
|
||||||
|
|
||||||
let module_ids = Arc::try_unwrap(state.arc_modules)
|
let module_ids = Arc::try_unwrap(state.arc_modules)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue