mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
Merge pull request #67 from rtfeldman/spawn-blocking
Use spawn_blocking for load_module
This commit is contained in:
commit
cf602f4690
2 changed files with 15 additions and 10 deletions
|
@ -10,7 +10,7 @@ petgraph = { version = "0.4.5", optional = true }
|
||||||
im = "14.0.0" # im and im-rc should always have the same version!
|
im = "14.0.0" # im and im-rc should always have the same version!
|
||||||
im-rc = "14.0.0" # im and im-rc should always have the same version!
|
im-rc = "14.0.0" # im and im-rc should always have the same version!
|
||||||
wyhash = "0.3"
|
wyhash = "0.3"
|
||||||
tokio = { version = "0.2", features = ["fs", "sync", "rt-threaded"] }
|
tokio = { version = "0.2", features = ["blocking", "fs", "sync", "rt-threaded"] }
|
||||||
bumpalo = "2.6"
|
bumpalo = "2.6"
|
||||||
# NOTE: Breaking API changes get pushed directly to this Inkwell branch, so be
|
# NOTE: Breaking API changes get pushed directly to this Inkwell branch, so be
|
||||||
# very careful when running `cargo update` to get a new revision into Cargo.lock.
|
# very careful when running `cargo update` to get a new revision into Cargo.lock.
|
||||||
|
|
|
@ -16,11 +16,12 @@ use crate::types::Constraint;
|
||||||
use crate::unify::Problems;
|
use crate::unify::Problems;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
|
use std::fs::read_to_string;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::fs::read_to_string;
|
|
||||||
use tokio::sync::mpsc::{self, Receiver, Sender};
|
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Loaded {
|
pub struct Loaded {
|
||||||
|
@ -90,9 +91,10 @@ pub async fn load<'a>(
|
||||||
let main_tx = tx.clone();
|
let main_tx = tx.clone();
|
||||||
let arc_var_store = Arc::new(VarStore::new(vars_created));
|
let arc_var_store = Arc::new(VarStore::new(vars_created));
|
||||||
let var_store = Arc::clone(&arc_var_store);
|
let var_store = Arc::clone(&arc_var_store);
|
||||||
let handle =
|
|
||||||
tokio::spawn(async move { load_filename(&env, filename, main_tx, &var_store).await });
|
|
||||||
|
|
||||||
|
// Use spawn_blocking here so that we can proceed to the recv() loop
|
||||||
|
// while this is doing blocking work like reading and parsing the file.
|
||||||
|
let handle = spawn_blocking(move || load_filename(&env, filename, main_tx, &var_store));
|
||||||
let requested_module = handle
|
let requested_module = handle
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|err| panic!("Unable to load requested module: {:?}", err));
|
.unwrap_or_else(|err| panic!("Unable to load requested module: {:?}", err));
|
||||||
|
@ -118,7 +120,10 @@ pub async fn load<'a>(
|
||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
let var_store = Arc::clone(&arc_var_store);
|
let var_store = Arc::clone(&arc_var_store);
|
||||||
|
|
||||||
tokio::spawn(async move { load_module(&env, dep, tx, &var_store).await })
|
// Use spawn_blocking here because we're canonicalizing these in
|
||||||
|
// parallel, and canonicalization can potentially block the
|
||||||
|
// executor for awhile.
|
||||||
|
spawn_blocking(move || load_module(&env, &dep, tx, &var_store))
|
||||||
}))
|
}))
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -142,9 +147,9 @@ pub async fn load<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_module(
|
fn load_module(
|
||||||
env: &Env,
|
env: &Env,
|
||||||
module_name: Box<str>,
|
module_name: &str,
|
||||||
tx: Sender<DepNames>,
|
tx: Sender<DepNames>,
|
||||||
var_store: &VarStore,
|
var_store: &VarStore,
|
||||||
) -> LoadedModule {
|
) -> LoadedModule {
|
||||||
|
@ -160,16 +165,16 @@ async fn load_module(
|
||||||
// End with .roc
|
// End with .roc
|
||||||
filename.set_extension("roc");
|
filename.set_extension("roc");
|
||||||
|
|
||||||
load_filename(env, filename, tx, var_store).await
|
load_filename(env, filename, tx, var_store)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_filename(
|
fn load_filename(
|
||||||
env: &Env,
|
env: &Env,
|
||||||
filename: PathBuf,
|
filename: PathBuf,
|
||||||
tx: Sender<DepNames>,
|
tx: Sender<DepNames>,
|
||||||
var_store: &VarStore,
|
var_store: &VarStore,
|
||||||
) -> LoadedModule {
|
) -> LoadedModule {
|
||||||
match read_to_string(&filename).await {
|
match read_to_string(&filename) {
|
||||||
Ok(src) => {
|
Ok(src) => {
|
||||||
let arena = Bump::new();
|
let arena = Bump::new();
|
||||||
// TODO instead of env.arena.alloc(src), we should create a new buffer
|
// TODO instead of env.arena.alloc(src), we should create a new buffer
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue