Skip to content

Commit 75bb172

Browse files
committed
faster cached builds
1 parent 63113d6 commit 75bb172

2 files changed

Lines changed: 216 additions & 40 deletions

File tree

src/build/cache.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use super::portable::{PModuleExports, StringTableBuilder, StringTableReader};
2121
#[derive(Serialize, Deserialize, Default)]
2222
struct CacheIndex {
2323
modules: HashMap<String, CacheIndexEntry>,
24+
/// Maps file paths to module names for fast lookup during incremental builds
25+
#[serde(default)]
26+
path_to_module: HashMap<String, String>,
2427
}
2528

2629
#[derive(Serialize, Deserialize, Clone)]
@@ -95,6 +98,8 @@ pub struct ModuleCache {
9598
entries: HashMap<String, CachedModule>,
9699
/// Reverse dependency graph: module → modules that import it
97100
dependents: HashMap<String, Vec<String>>,
101+
/// Maps file paths to module names for skipping parse on warm builds
102+
path_index: HashMap<String, String>,
98103
/// Directory for per-module cache files
99104
cache_dir: Option<PathBuf>,
100105
/// Whether the index needs to be rewritten
@@ -106,6 +111,7 @@ impl Default for ModuleCache {
106111
Self {
107112
entries: HashMap::new(),
108113
dependents: HashMap::new(),
114+
path_index: HashMap::new(),
109115
cache_dir: None,
110116
index_dirty: false,
111117
}
@@ -158,6 +164,24 @@ impl ModuleCache {
158164
}
159165
}
160166

167+
/// Look up the module name associated with a file path.
168+
pub fn module_name_for_path(&self, path: &str) -> Option<&str> {
169+
self.path_index.get(path).map(|s| s.as_str())
170+
}
171+
172+
/// Register a file path → module name mapping.
173+
pub fn register_path(&mut self, path: String, module_name: String) {
174+
if self.path_index.get(&path).map(|s| s.as_str()) != Some(&module_name) {
175+
self.path_index.insert(path, module_name);
176+
self.index_dirty = true;
177+
}
178+
}
179+
180+
/// Get the cached imports for a module by name.
181+
pub fn get_imports(&self, module_name: &str) -> Option<&[String]> {
182+
self.entries.get(module_name).map(|c| c.imports())
183+
}
184+
161185
/// Get cached exports for a module, loading from disk if needed.
162186
pub fn get_exports(&mut self, module_name: &str) -> Option<&ModuleExports> {
163187
// Check if we need to load from disk first
@@ -265,6 +289,7 @@ impl ModuleCache {
265289
let before = self.entries.len();
266290
self.entries.retain(|k, _| module_names.contains(k));
267291
if self.entries.len() != before {
292+
self.path_index.retain(|_, v| module_names.contains(v));
268293
self.index_dirty = true;
269294
}
270295
}
@@ -300,6 +325,7 @@ impl ModuleCache {
300325
imports: cached.imports().to_vec(),
301326
})
302327
}).collect(),
328+
path_to_module: self.path_index.clone(),
303329
};
304330

305331
let index_path = cache_dir.join("index.bin");
@@ -333,6 +359,7 @@ impl ModuleCache {
333359
let mut cache = ModuleCache {
334360
entries,
335361
dependents: HashMap::new(),
362+
path_index: index.path_to_module,
336363
cache_dir: Some(cache_dir.to_path_buf()),
337364
index_dirty: false,
338365
};

0 commit comments

Comments
 (0)