sui_eval/builtins/import_cache.rs
1//! Import cache infrastructure.
2//!
3//! CppNix caches `import` results so that `import ./lib.nix` evaluated
4//! from different call sites returns the same thunk/value. This is
5//! critical for nixpkgs performance — without it, ~500 unique files
6//! times 50+ overlay applications produce 25,000+ redundant parse-
7//! and-evaluate cycles, easily blowing the eval depth limit.
8
9use std::cell::RefCell;
10use std::collections::HashMap;
11
12use crate::value::Value;
13
14thread_local! {
15 /// Cache of imported file values, keyed by canonical absolute path.
16 ///
17 /// The cache persists for the entire evaluation session (including
18 /// recursive `evaluate_flake` calls for flake inputs) so that shared
19 /// dependencies like nixpkgs are evaluated only once.
20 pub(crate) static IMPORT_CACHE: RefCell<HashMap<std::path::PathBuf, Value>> = RefCell::new(HashMap::new());
21}
22
23/// Clear the import cache.
24///
25/// Call at the start of a fresh top-level evaluation when you need to
26/// guarantee that no stale values survive from a previous session.
27/// During normal flake evaluation this should **not** be called — the
28/// cache intentionally spans recursive `evaluate_flake` calls.
29pub fn clear_import_cache() {
30 IMPORT_CACHE.with(|c| c.borrow_mut().clear());
31}