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 <flokli@flokli.de>
This commit is contained in:
Vincent Ambo 2023-02-02 13:32:39 +03:00 committed by tazjin
parent 9d6f29a72b
commit e6235e2932

View file

@ -6,6 +6,7 @@
//! is still being implemented in Tvix. //! is still being implemented in Tvix.
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::rc::Rc; use std::rc::Rc;
@ -25,6 +26,12 @@ pub struct NixCompatIO {
/// Ingested paths must be reported to this known paths tracker /// Ingested paths must be reported to this known paths tracker
/// for accurate build reference scanning. /// for accurate build reference scanning.
known_paths: Rc<RefCell<KnownPaths>>, 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 { impl EvalIO for NixCompatIO {
@ -34,10 +41,20 @@ impl EvalIO for NixCompatIO {
// Pass path imports through to `nix-store --add` // Pass path imports through to `nix-store --add`
fn import_path(&self, path: &Path) -> Result<PathBuf, ErrorKind> { 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), error: std::rc::Rc::new(error),
path: Some(path.to_path_buf()), 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` // 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 { pub fn new(known_paths: Rc<RefCell<KnownPaths>>) -> Self {
NixCompatIO { NixCompatIO {
underlying: StdIO, underlying: StdIO,
import_cache: RefCell::new(HashMap::new()),
known_paths, known_paths,
} }
} }