Unverified Commit 6bcde44e authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Check the invalidator map before adding a path to a batch (#99711)

We don't need to add a file path to a batch and extend the batch
deadline if the path isn't contained in the invalidator map.

Prevents us from getting logspam like this:

```
Turbopack has seen frequent file updates and is waiting for the filesystem to settle (0.5s elapsed so far; last modified: "/Users/bgw/v0/chat/.next/dev/server/next-font-manifest.js").
```

This is much less critical with the non-recursive watcher codepath, as
we're only watching paths we might care about, but keeping it around in
both cases helps ensure we get some CI coverage of this. I could put it
behind `#[cfg(debug_assert)]` for non-recursive mode, IDK.

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7c16cc9f
......@@ -10,6 +10,9 @@ use std::{
///
/// In the future, this may use a more efficient representation, like a radix tree or trie.
pub trait OrderedPathMapExt<K, V> {
/// Returns whether the map contains `path` or any of its children.
fn contains_path_or_children(&self, path: &Path) -> bool;
fn extract_path_with_children<'a>(
&'a mut self,
path: &'a Path,
......@@ -17,6 +20,12 @@ pub trait OrderedPathMapExt<K, V> {
}
impl<K: Borrow<Path> + Ord, V> OrderedPathMapExt<K, V> for BTreeMap<K, V> {
fn contains_path_or_children(&self, path: &Path) -> bool {
self.lower_bound(Bound::Included(path))
.peek_next()
.is_some_and(|(key, _)| key.borrow().starts_with(path))
}
/// Iterates over and removes `path` and all of its children.
fn extract_path_with_children<'a>(
&'a mut self,
......@@ -96,6 +105,16 @@ impl<'a> Iterator for PathSetIterPathChildren<'a> {
mod tests {
use super::*;
#[test]
fn test_map_contains_path_or_children() {
let mut map = BTreeMap::default();
map.insert(PathBuf::from("a/b"), 2);
assert!(map.contains_path_or_children(Path::new("a")));
assert!(map.contains_path_or_children(Path::new("a/b")));
assert!(!map.contains_path_or_children(Path::new("a/b/c")));
}
#[test]
fn test_map_extract_path_with_children() {
let mut map = BTreeMap::default();
......@@ -108,6 +127,9 @@ mod tests {
map.insert(PathBuf::from("x/y/z"), 7);
map.insert(PathBuf::from("z/a/b"), 8);
assert!(map.contains_path_or_children(Path::new("x/y")));
assert!(!map.contains_path_or_children(Path::new("a/b/c/d")));
let parent_path = PathBuf::from("a/b");
let extracted: Vec<_> = map.extract_path_with_children(&parent_path).collect();
......
......@@ -26,6 +26,10 @@ pub struct MockFileSystem {
pub dir_invalidator_map: InvalidatorMap,
pub invalidation_lock: RwLock<()>,
pub run_counts: Mutex<FxHashMap<Arc<PathBuf>, u64>>,
/// Tracked reads wait for a read lock on this before reading. Tests can hold the write lock to
/// delay re-executions of invalidated reads, e.g. to simulate a filesystem change happening
/// before a dependent task has re-registered its read.
pub read_gate: RwLock<()>,
transient_handle: TransientInstance<MockFsHandle>,
_temp_dir: TempDir,
}
......@@ -48,6 +52,7 @@ impl MockFileSystem {
dir_invalidator_map: InvalidatorMap::new(),
invalidation_lock: RwLock::new(()),
run_counts: Mutex::new(FxHashMap::default()),
read_gate: RwLock::new(()),
transient_handle: TransientInstance::new(MockFsHandle(weak.clone())),
_temp_dir: temp_dir,
})
......@@ -58,6 +63,7 @@ impl MockFileSystem {
async fn tracked_read_operation(fs: TransientInstance<MockFsHandle>, path: RcStr) {
let fs = fs.0.upgrade().unwrap();
let path = Arc::new(PathBuf::from(&*path));
let _gate = fs.read_gate.read().await;
if metadata(&*path).unwrap().is_dir() {
fs.dir_invalidator_map
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment