about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-02-02T10·32+0300
committertazjin <tazjin@tvl.su>2023-02-02T17·50+0000
commite6235e2932cc76b18fe8cc8acf209c5fe2e8b79f (patch)
tree15994deb09f3e33bec566dd2bc2f994433179882
parent9d6f29a72b3b466dd697c2eaa97f9a41b767fdff (diff)
feat(tvix/cli): cache imported paths in NixCompatIO r/5826
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 <flokli@flokli.de>
-rw-r--r--tvix/cli/src/nix_compat.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/tvix/cli/src/nix_compat.rs b/tvix/cli/src/nix_compat.rs
index b20953f2b002..2bee43882f09 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<RefCell<KnownPaths>>,
+
+    /// 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<HashMap<PathBuf, PathBuf>>,
 }
 
 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<PathBuf, ErrorKind> {
-        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<RefCell<KnownPaths>>) -> Self {
         NixCompatIO {
             underlying: StdIO,
+            import_cache: RefCell::new(HashMap::new()),
             known_paths,
         }
     }