diff options
author | Adam Joseph <adam@westernsemico.com> | 2022-11-23T08·32-0800 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-12-02T13·32+0000 |
commit | 8eb32fb2d79699ffc429f3bd355958da185cb6d9 (patch) | |
tree | 073c2a26d317d45a5d35aaf56856c5ecc6f7c998 /tvix/eval/src/builtins/impure.rs | |
parent | 7ffeb4f7f1657d29df9832460ce6fa33a2184bad (diff) |
feat(tvix/eval): crude caching builtins.import r/5366
Before this, tvix was spending most of its time furiously re-parsing and re-compiling nixpkgs, each time hoping to get a different result... Change-Id: I1c0cfbf9af622c276275b1f2fb8d4e976f1b5533 Signed-off-by: Adam Joseph <adam@westernsemico.com> Reviewed-on: https://cl.tvl.fyi/c/depot/+/7361 Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins/impure.rs')
-rw-r--r-- | tvix/eval/src/builtins/impure.rs | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/tvix/eval/src/builtins/impure.rs b/tvix/eval/src/builtins/impure.rs index f28609fa763f..dc62e93ada40 100644 --- a/tvix/eval/src/builtins/impure.rs +++ b/tvix/eval/src/builtins/impure.rs @@ -122,6 +122,12 @@ pub fn builtins_import(globals: &Weak<GlobalsMap>, source: SourceCode) -> Builti path.push("default.nix"); } + let current_span = vm.current_span(); + let entry = match vm.import_cache.entry(path.clone()) { + std::collections::btree_map::Entry::Occupied(oe) => return Ok(oe.get().clone()), + std::collections::btree_map::Entry::Vacant(ve) => ve, + }; + let contents = std::fs::read_to_string(&path).map_err(|err| ErrorKind::ReadFileError { path: path.clone(), @@ -167,16 +173,20 @@ pub fn builtins_import(globals: &Weak<GlobalsMap>, source: SourceCode) -> Builti }); } + // Compilation succeeded, we can construct a thunk from whatever it spat + // out and return that. + let res = entry + .insert(Value::Thunk(Thunk::new_suspended( + result.lambda, + current_span, + ))) + .clone(); + for warning in result.warnings { vm.push_warning(warning); } - // Compilation succeeded, we can construct a thunk from whatever it spat - // out and return that. - Ok(Value::Thunk(Thunk::new_suspended( - result.lambda, - vm.current_span(), - ))) + Ok(res) }, ) } |