Skip to content

Commit

Permalink
Use once_cell to cache stdlib compilation in wasm (#124)
Browse files Browse the repository at this point in the history
This adds simple caching of the stdlib compilation during check calls
using OnceCell. Since the stdlib is static in the wasm layer and the
store does not need to be mutable (the compilation unit for the checked
user's code is not added to it), this can be done once for the lifetime
of the loaded package and reused over and over. In local testing, this
produced a 95% perf improvement on the check calls performed as the user
types (from 2.3 ms to 0.1 ms).
  • Loading branch information
swernli authored Apr 3, 2023
1 parent 8c653bb commit cb6e594
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ thiserror = "1.0.39"
num-bigint = "0.4.3"
num-complex = "0.4"
num-traits = "0.2.15"
once_cell = "1.17.1"
serde = "1.0"
serde-wasm-bindgen = "0.5"
wasm-bindgen = "0.2.84"
Expand Down
1 change: 1 addition & 0 deletions compiler/qsc_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ js-sys = { workspace = true }
miette = { workspace = true}
num-bigint = { workspace = true }
num-complex = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true, features = ["derive"]}
serde-wasm-bindgen = { workspace = true}
wasm-bindgen = { workspace = true}
13 changes: 9 additions & 4 deletions compiler/qsc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use num_bigint::BigUint;
use num_complex::Complex64;
use once_cell::sync::OnceCell;
use qsc_eval::output::Receiver;
use qsc_eval::{output, Error, Evaluator};
use qsc_frontend::compile::{compile, std, PackageStore};
use qsc_frontend::compile::{compile, std, PackageId, PackageStore};
use qsc_passes::globals::extract_callables;

use miette::{Diagnostic, Severity};
Expand Down Expand Up @@ -226,9 +227,13 @@ where
}

fn check_code_internal(code: &str) -> Vec<VSDiagnostic> {
let mut store = PackageStore::new();
let std = store.insert(std());
let unit = compile(&store, [std], [code], "");
static STDLIB_STORE: OnceCell<(PackageId, PackageStore)> = OnceCell::new();
let (std, ref store) = STDLIB_STORE.get_or_init(|| {
let mut store = PackageStore::new();
let std = store.insert(std());
(std, store)
});
let unit = compile(store, [*std], [code], "");

let mut result: Vec<VSDiagnostic> = vec![];

Expand Down

0 comments on commit cb6e594

Please sign in to comment.