Unverified Commit 7830cfc3 authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Cache canonicalized paths without turbo-tasks, try to find the…

Turbopack: Cache canonicalized paths without turbo-tasks, try to find the longest cached prefix first (#99270)

When converting from an absolute system path to a filesystem-relative
path, we'd first try to lexically `strip_prefix`, but if that failed,
we'd fall back to trying to canonicalize the root path before stripping.

Because we don't know how many of the path segments refer to the root,
we have to canonicalize one path segment at a time.

With turbo-tasks, we have to probe the cache once per path segment,
starting from the shortest possible path segment.

This removes the `turbo_tasks::function` we were using to cache the
canonicalization calls, and instead stores caches in `BTreeMap`s. This
lets us efficiently look up the longest matching prefix in the cache,
which (in the happy path) should be a match for a filesystem root.

## Possible concerns

- ~We have duplicated canonicalization caches, one for the map, one for
each `DiskFileSystem`. Would a per-process (`OnceLock`) cache of
canonicalization calls make more sense?~ We now prefer lookups in the
map, which effectively solves this.
- A `PatriciaMap` would likely be more space efficient. `PatriciaMap` is
currently only used in `AliasMap`:
https://github.com/vercel/next.js/blob/ff6ac752af572467f35187e421dd7e558797b412/turbopack/crates/turbopack-core/src/resolve/alias_map.rs#L16

## Things I don't think are concerns

- `RwLock`: If we have a lot of writes, that means we're spending a lot
of time doing canonicalization, which would be a bigger problem than
locking.
parent e4801332
use std::{
collections::BTreeMap,
fs::canonicalize,
ops::{Bound, ControlFlow},
path::{Path, PathBuf},
sync::Arc,
};
use parking_lot::RwLock;
use smallvec::SmallVec;
use turbo_rcstr::RcStr;
use turbo_tasks::ResolvedVc;
use turbo_unix_path::{normalize_path, sys_to_unix};
use crate::{DiskFileSystem, FileSystemPath, retry::retry_blocking};
/// Cached canonicalization information about paths outside of the project root for the sake
/// of converting absolute paths into root-relative ones. These canonicalization operations are
/// potentially expensive, hence why we cache them.
///
/// These paths are unwatched (we only watch inside of roots), but we do not persist them, so if
/// there is stale data here, restarting the process will resolve the issue.
#[derive(Default)]
pub(crate) struct CanonicalizedPathWalkCache(RwLock<BTreeMap<PathBuf, CheckedPrefix>>);
enum CheckedPrefix {
/// The prefix is non-UTF-8, canonicalization failed or produced a non-UTF-8 path, or `visit`
/// rejected the canonical path.
Invalid,
/// The canonical path is outside the tracked roots; use it to check the next prefix.
Unmatched(Arc<PathBuf>),
/// The prefix reached a tracked root; append the untouched suffix in that filesystem.
Matched(ResolvedVc<DiskFileSystem>),
}
fn strip_longest_prefix<'a, 'p, T>(
prefixes: &'a BTreeMap<PathBuf, T>,
path: &'p Path,
) -> Option<(&'p Path, &'a T)> {
let mut upper = path;
loop {
let (prefix, value) = prefixes.upper_bound(Bound::Included(upper)).peek_prev()?;
if let Ok(rest) = path.strip_prefix(prefix) {
return Some((rest, value));
}
// Find the longest common ancestor before doing another lookup in the map
upper = prefix
.ancestors()
.find(|ancestor| path.starts_with(ancestor))?;
}
}
impl CanonicalizedPathWalkCache {
/// Performs untracked canonicalization on successive prefixes of `target_sys_path`, from the
/// system root toward the full path. Passes each canonical prefix to `visit`, until `visit`
/// finds a filesystem root.
///
/// This is different than canonicalizing the full path because we want to avoid untracked
/// canonicalization inside of the watched filesystem root. We only want to canonicalize and
/// cache paths leading up to the root. This can be useful when translating an absolute system
/// path to a filesystem-relative path if cheaper lexical prefix-stripping has failed.
///
/// This operation is cached, so it starts looking from the longest known canonicalized prefix.
///
/// The result of `visit` is cached for each prefix, so every call on this cache must use a
/// visitor with the same behavior for a given canonical path.
///
/// This is a helper for [`DiskFileSystem::resolve_path_ancestry_slow_path`].
pub(crate) async fn walk_canonicalized_ancestry(
&self,
target: &Path,
visit: impl Fn(&Path) -> ControlFlow<Option<ResolvedVc<DiskFileSystem>>>,
) -> Option<FileSystemPath> {
let unmatched_checkpoint = {
let cache = self.0.read();
match strip_longest_prefix(&cache, target) {
Some((_, CheckedPrefix::Invalid)) => return None,
Some((rest, CheckedPrefix::Matched(found))) => {
return path_from_root(*found, rest);
}
Some((rest, CheckedPrefix::Unmatched(canonical))) => {
Some((rest.ancestors().count() - 1, canonical.clone()))
}
None => None,
}
};
let ancestors: SmallVec<[&Path; 8]> = target.ancestors().collect();
let (remaining, mut canonical_parent) =
if let Some((remaining, canonical)) = unmatched_checkpoint {
(remaining, Some(canonical))
} else {
(ancestors.len() - 1, None)
};
for prefix in ancestors[..remaining].iter().rev() {
let canonical = if prefix.to_str().is_some() {
let path = if let Some(parent) = &canonical_parent {
let suffix = prefix
.strip_prefix(prefix.parent().expect("prefix has a parent"))
.expect("parent is an ancestor");
parent.join(suffix)
} else {
prefix.to_path_buf()
};
retry_blocking(|| canonicalize(&path))
.await
.ok()
.filter(|path| path.to_str().is_some())
.map(Arc::new)
} else {
None
};
let Some(canonical) = canonical else {
self.0
.write()
.insert(prefix.to_path_buf(), CheckedPrefix::Invalid);
return None;
};
let mut cache = self.0.write();
match visit(canonical.as_path()) {
ControlFlow::Continue(()) => {
// `visit` has found a partial match
cache.insert(
prefix.to_path_buf(),
CheckedPrefix::Unmatched(canonical.clone()),
);
canonical_parent = Some(canonical);
}
ControlFlow::Break(Some(found)) => {
// `visit` has found an exact match for a filesystem root
cache.insert(prefix.to_path_buf(), CheckedPrefix::Matched(found));
let rest = target.strip_prefix(prefix).expect("prefix is an ancestor");
return path_from_root(found, rest);
}
// `visit` has found a path it considers invalid (e.g. the canonicalization read
// gave us a path inside of the root)
ControlFlow::Break(None) => {
cache.insert(prefix.to_path_buf(), CheckedPrefix::Invalid);
return None;
}
}
}
None
}
}
fn path_from_root(root: ResolvedVc<DiskFileSystem>, rest: &Path) -> Option<FileSystemPath> {
let rest = rest.to_str()?;
// The walk preserves the original path until it finds a root: removing `link/..` earlier
// could skip a symlink and select a different root. Normalize only the suffix here, without
// reading paths inside the watched root.
let path = if rest.is_empty() {
RcStr::default()
} else {
RcStr::from(normalize_path(&sys_to_unix(rest))?)
};
Some(FileSystemPath::new_normalized_unchecked(
ResolvedVc::upcast(root),
path,
))
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use super::*;
#[tokio::test]
async fn walk_caches_invalid_prefix() {
let cache = CanonicalizedPathWalkCache::default();
let target = canonicalize(".").unwrap();
let calls = Cell::new(0);
let visit = |_: &Path| {
calls.set(calls.get() + 1);
ControlFlow::Break(None)
};
assert!(
cache
.walk_canonicalized_ancestry(&target, &visit)
.await
.is_none()
);
assert_eq!(calls.get(), 1);
assert!(
cache
.walk_canonicalized_ancestry(&target, &visit)
.await
.is_none()
);
assert_eq!(calls.get(), 1);
}
#[test]
fn strip_longest_prefix_matches_path_ancestors() {
#[cfg(unix)]
let paths = ["/a/b/c", "/a/../b", "/a//b", "/a/./b"];
#[cfg(windows)]
let paths = [r"C:\a\b", r"\\?\C:\a\b", r"\\server\share\a\b", r"C:/a/b"];
for path in paths {
let path = Path::new(path);
for prefix in path.ancestors() {
let prefixes = BTreeMap::from([(prefix.to_path_buf(), ())]);
let (rest, _) = strip_longest_prefix(&prefixes, path).unwrap();
assert_eq!(rest, path.strip_prefix(prefix).unwrap());
assert_eq!(
rest.ancestors().count() - 1,
path.ancestors()
.position(|ancestor| ancestor == prefix)
.unwrap()
);
}
}
let mut prefixes = BTreeMap::from([
(PathBuf::from("/a/b"), ()),
(PathBuf::from("/a/b/earlier-sibling"), ()),
(PathBuf::from("/a/bc/sibling"), ()),
]);
assert_eq!(
strip_longest_prefix(&prefixes, Path::new("/a/b/later-sibling")).map(|(rest, _)| rest),
Some(Path::new("later-sibling"))
);
assert!(strip_longest_prefix(&prefixes, Path::new("/a/bc/file")).is_none());
prefixes.insert(PathBuf::from("/a"), ());
prefixes.insert(PathBuf::from("/a/bc/earlier-sibling"), ());
assert_eq!(
strip_longest_prefix(&prefixes, Path::new("/a/bc/later-sibling")).map(|(rest, _)| rest),
Some(Path::new("bc/later-sibling"))
);
}
}
...@@ -166,8 +166,8 @@ pub(crate) enum FileComparison { ...@@ -166,8 +166,8 @@ pub(crate) enum FileComparison {
/// The target of a symbolic link, as read from a filesystem. /// The target of a symbolic link, as read from a filesystem.
/// ///
/// Every variant carries the `resolved` path the link points at, computed once by /// Every variant carries the `resolved` path the link points at, computed once by
/// [`crate::FileSystem::read_link`], which is also what guarantees the target stays inside the /// [`crate::FileSystem::read_link`]. A link whose target cannot be resolved into its filesystem
/// filesystem root — a link whose target leaves the root is [`LinkContent::Invalid`] instead. /// or another configured filesystem is [`LinkContent::Invalid`] instead.
#[derive(Clone, Debug, Hash, PartialEq, Eq, NonLocalValue, Encode, Decode)] #[derive(Clone, Debug, Hash, PartialEq, Eq, NonLocalValue, Encode, Decode)]
pub enum LinkTarget { pub enum LinkTarget {
/// The link is an absolute path on disk. /// The link is an absolute path on disk.
......
This diff is collapsed.
...@@ -8,14 +8,27 @@ use turbo_rcstr::RcStr; ...@@ -8,14 +8,27 @@ use turbo_rcstr::RcStr;
use turbo_tasks::{OperationVc, ResolvedVc, Vc}; use turbo_tasks::{OperationVc, ResolvedVc, Vc};
use turbo_unix_path::sys_to_unix; use turbo_unix_path::sys_to_unix;
use crate::{DiskFileSystem, FileSystemPath}; use crate::{DiskFileSystem, FileSystemPath, canonicalized_path_cache::CanonicalizedPathWalkCache};
/// An ordered set of canonical system roots and their owning filesystems. /// An ordered set of canonical system roots and their owning filesystems.
/// ///
/// The roots must not overlap: no root may be an ancestor of another root. [`Self::lookup`] /// The roots must not overlap: no root may be an ancestor of another root. [`Self::lookup`]
/// relies on this invariant when selecting the nearest preceding root in path order. /// relies on this invariant when selecting the nearest preceding root in path order.
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared, eq = "manual")]
pub struct DiskFileSystemMap(FrozenMap<PathBuf, ResolvedVc<DiskFileSystem>>); pub struct DiskFileSystemMap {
roots: FrozenMap<PathBuf, ResolvedVc<DiskFileSystem>>,
#[turbo_tasks(debug_ignore, unsafe_ignore)]
#[bincode(skip)]
pub(crate) canonicalized_paths: CanonicalizedPathWalkCache,
}
impl PartialEq for DiskFileSystemMap {
fn eq(&self, other: &Self) -> bool {
self.roots == other.roots
}
}
impl Eq for DiskFileSystemMap {}
impl FromIterator<(PathBuf, ResolvedVc<DiskFileSystem>)> for DiskFileSystemMap { impl FromIterator<(PathBuf, ResolvedVc<DiskFileSystem>)> for DiskFileSystemMap {
fn from_iter<T: IntoIterator<Item = (PathBuf, ResolvedVc<DiskFileSystem>)>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = (PathBuf, ResolvedVc<DiskFileSystem>)>>(iter: T) -> Self {
...@@ -29,27 +42,53 @@ impl FromIterator<(PathBuf, ResolvedVc<DiskFileSystem>)> for DiskFileSystemMap { ...@@ -29,27 +42,53 @@ impl FromIterator<(PathBuf, ResolvedVc<DiskFileSystem>)> for DiskFileSystemMap {
root.display() root.display()
); );
} }
DiskFileSystemMap(filesystems) DiskFileSystemMap {
roots: filesystems,
canonicalized_paths: Default::default(),
}
} }
} }
impl DiskFileSystemMap { impl DiskFileSystemMap {
pub fn has_file_system_other_than(&self, current: ResolvedVc<DiskFileSystem>) -> bool { pub(crate) fn contains(&self, root: &Path, current: ResolvedVc<DiskFileSystem>) -> bool {
self.0.values().any(|file_system| *file_system != current) self.roots.get(root).is_some_and(|fs| *fs == current)
}
pub fn len(&self) -> usize {
self.roots.len()
}
pub fn is_empty(&self) -> bool {
self.roots.is_empty()
}
/// Finds the containing root; the suffix is empty only when `path` is that root.
pub(crate) fn lookup_sys_path_suffix<'a>(
&self,
path: &'a Path,
) -> Option<LookupSysPathSuffix<'a>> {
let (root, fs) = self
.roots
.range::<Path, _>((Bound::Unbounded, Bound::Included(path)))
.next_back()?;
Some(LookupSysPathSuffix {
fs: *fs,
remaining: path.strip_prefix(root).ok()?,
})
} }
/// Converts an absolute system path into a path owned by one of the installed filesystems. /// Converts an absolute system path into a path owned by one of the installed filesystems.
/// ///
/// Returns `None` if the file path does not exist inside any other root, or if the relative /// Returns `None` if the file path does not exist inside any other root, or if the relative
/// path would not be valid unicode. /// path would not be valid unicode.
pub fn lookup(&self, path: &Path) -> Option<FileSystemPath> { pub fn lookup_fs_path(&self, path: &Path) -> Option<FileSystemPath> {
let (root, fs) = self let LookupSysPathSuffix {
.0 fs,
.range::<Path, _>((Bound::Unbounded, Bound::Included(path))) remaining: relative,
.next_back()?; } = self.lookup_sys_path_suffix(path)?;
let relative = path.strip_prefix(root).ok()?.to_str()?; let relative = relative.to_str()?;
Some(FileSystemPath::new_normalized_unchecked( Some(FileSystemPath::new_normalized_unchecked(
ResolvedVc::upcast(*fs), ResolvedVc::upcast(fs),
RcStr::from(sys_to_unix(relative)), RcStr::from(sys_to_unix(relative)),
)) ))
} }
...@@ -59,12 +98,21 @@ impl DiskFileSystemMap { ...@@ -59,12 +98,21 @@ impl DiskFileSystemMap {
pub fn empty() -> OperationVc<DiskFileSystemMap> { pub fn empty() -> OperationVc<DiskFileSystemMap> {
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
pub fn operation() -> Vc<DiskFileSystemMap> { pub fn operation() -> Vc<DiskFileSystemMap> {
DiskFileSystemMap(FrozenMap::new()).cell() DiskFileSystemMap {
roots: FrozenMap::new(),
canonicalized_paths: Default::default(),
}
.cell()
} }
operation() operation()
} }
} }
pub struct LookupSysPathSuffix<'a> {
pub fs: ResolvedVc<DiskFileSystem>,
pub remaining: &'a Path,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
...@@ -80,12 +128,18 @@ mod tests { ...@@ -80,12 +128,18 @@ mod tests {
.to_resolved() .to_resolved()
.await?; .await?;
let map: DiskFileSystemMap = [(PathBuf::from("/tmp/root"), fs)].into_iter().collect(); let map: DiskFileSystemMap = [(PathBuf::from("/tmp/root"), fs)].into_iter().collect();
assert!(!map.has_file_system_other_than(fs)); assert_eq!(map.len(), 1);
assert!(map.contains(Path::new("/tmp/root"), fs));
assert_eq!( assert_eq!(
map.lookup(Path::new("/tmp/root/file")).unwrap().path, map.lookup_fs_path(Path::new("/tmp/root/file"))
.unwrap()
.path,
"file" "file"
); );
assert!(map.lookup(Path::new("/tmp/root-other/file")).is_none()); assert!(
map.lookup_fs_path(Path::new("/tmp/root-other/file"))
.is_none()
);
Ok(()) Ok(())
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this #![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
#![allow(clippy::mutable_key_type)] #![allow(clippy::mutable_key_type)]
mod canonicalized_path_cache;
mod content; mod content;
mod disk; mod disk;
pub mod embed; pub mod embed;
......
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