From e6235e2932cc76b18fe8cc8acf209c5fe2e8b79f Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 2 Feb 2023 13:32:39 +0300 Subject: feat(tvix/cli): cache imported paths in NixCompatIO Creates a cache of imported literal files (e.g. `./default-builder.sh`) which avoids shelling out to Nix for each instance of the same file. Note that a better way to tackle this is to create memoizable thunks for these expressions in the compiler, but we are lacking a little bit of infrastructure for that at the moment. Change-Id: Ibc062b20d81e97dd3986e734d225a744e1779fe7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8015 Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/cli/src/nix_compat.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tvix/cli/src/nix_compat.rs b/tvix/cli/src/nix_compat.rs index b20953f2b0..2bee43882f 100644 --- a/tvix/cli/src/nix_compat.rs +++ b/tvix/cli/src/nix_compat.rs @@ -6,6 +6,7 @@ //! is still being implemented in Tvix. use std::cell::RefCell; +use std::collections::HashMap; use std::path::Path; use std::process::Command; use std::rc::Rc; @@ -25,6 +26,12 @@ pub struct NixCompatIO { /// Ingested paths must be reported to this known paths tracker /// for accurate build reference scanning. known_paths: Rc>, + + /// Cache paths for identical files being imported to the store. + // TODO(tazjin): This could be done better by having a thunk cache + // for these calls on the eval side, but that is a little more + // complex. + import_cache: RefCell>, } impl EvalIO for NixCompatIO { @@ -34,10 +41,20 @@ impl EvalIO for NixCompatIO { // Pass path imports through to `nix-store --add` fn import_path(&self, path: &Path) -> Result { - self.add_to_store(path).map_err(|error| ErrorKind::IO { + let path = path.to_owned(); + if let Some(path) = self.import_cache.borrow().get(&path) { + return Ok(path.to_path_buf()); + } + + let store_path = self.add_to_store(&path).map_err(|error| ErrorKind::IO { error: std::rc::Rc::new(error), path: Some(path.to_path_buf()), - }) + })?; + + self.import_cache + .borrow_mut() + .insert(path, store_path.clone()); + Ok(store_path) } // Pass the rest of the functions through to `Self::underlying` @@ -58,6 +75,7 @@ impl NixCompatIO { pub fn new(known_paths: Rc>) -> Self { NixCompatIO { underlying: StdIO, + import_cache: RefCell::new(HashMap::new()), known_paths, } } -- cgit 1.4.1