diff --git a/Cargo.lock b/Cargo.lock index 2dabeedc4b..438b84f2f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,7 @@ dependencies = [ "miette", "num-bigint", "num-complex", + "once_cell", "qsc_eval", "qsc_frontend", "qsc_passes", diff --git a/Cargo.toml b/Cargo.toml index 21332826aa..50a3767045 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/compiler/qsc_wasm/Cargo.toml b/compiler/qsc_wasm/Cargo.toml index d7ed60e5ef..5167dab9cb 100644 --- a/compiler/qsc_wasm/Cargo.toml +++ b/compiler/qsc_wasm/Cargo.toml @@ -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} diff --git a/compiler/qsc_wasm/src/lib.rs b/compiler/qsc_wasm/src/lib.rs index ad41e13dfd..b19a91b146 100644 --- a/compiler/qsc_wasm/src/lib.rs +++ b/compiler/qsc_wasm/src/lib.rs @@ -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}; @@ -226,9 +227,13 @@ where } fn check_code_internal(code: &str) -> Vec { - 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 = vec![];