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

Turbopack: Use a FrozenMap for DiskFileSystemMap (#99569)

Minor follow-up to
https://github.com/vercel/next.js/pull/98469/changes#r3983805536
parent c6345b09
...@@ -9342,6 +9342,7 @@ dependencies = [ ...@@ -9342,6 +9342,7 @@ dependencies = [
"tracing", "tracing",
"triomphe 0.1.12", "triomphe 0.1.12",
"turbo-bincode", "turbo-bincode",
"turbo-frozenmap",
"turbo-rcstr", "turbo-rcstr",
"turbo-tasks", "turbo-tasks",
"turbo-tasks-backend", "turbo-tasks-backend",
......
...@@ -51,6 +51,7 @@ tokio = { workspace = true } ...@@ -51,6 +51,7 @@ tokio = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
triomphe = { workspace = true } triomphe = { workspace = true }
turbo-bincode = { workspace = true } turbo-bincode = { workspace = true }
turbo-frozenmap = { workspace = true }
turbo-rcstr = { workspace = true } turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true } turbo-tasks = { workspace = true }
turbo-tasks-hash = { workspace = true } turbo-tasks-hash = { workspace = true }
......
use std::{ use std::{
collections::BTreeMap,
ops::Bound, ops::Bound,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use turbo_frozenmap::FrozenMap;
use turbo_rcstr::RcStr; 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;
...@@ -15,21 +15,21 @@ use crate::{DiskFileSystem, FileSystemPath}; ...@@ -15,21 +15,21 @@ use crate::{DiskFileSystem, FileSystemPath};
/// 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)]
pub struct DiskFileSystemMap(BTreeMap<PathBuf, ResolvedVc<DiskFileSystem>>); pub struct DiskFileSystemMap(FrozenMap<PathBuf, ResolvedVc<DiskFileSystem>>);
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 {
let filesystems = BTreeMap::from_iter(iter); let filesystems = FrozenMap::from_iter(iter);
let mut map = DiskFileSystemMap(BTreeMap::new()); for pair in filesystems.as_slice().windows(2) {
for (root, fs) in filesystems { let (previous_root, _) = &pair[0];
let (root, _) = &pair[1];
assert!( assert!(
map.lookup(&root).is_none(), !root.starts_with(previous_root),
"filesystem root {} overlaps another filesystem root", "filesystem root {} overlaps another filesystem root",
root.display() root.display()
); );
map.0.insert(root, fs);
} }
map DiskFileSystemMap(filesystems)
} }
} }
...@@ -43,7 +43,10 @@ impl DiskFileSystemMap { ...@@ -43,7 +43,10 @@ impl DiskFileSystemMap {
/// 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(&self, path: &Path) -> Option<FileSystemPath> {
let (root, fs) = self.0.upper_bound(Bound::Included(path)).peek_prev()?; let (root, fs) = self
.0
.range::<Path, _>((Bound::Unbounded, Bound::Included(path)))
.next_back()?;
let relative = path.strip_prefix(root).ok()?.to_str()?; let relative = path.strip_prefix(root).ok()?.to_str()?;
Some(FileSystemPath::new_normalized_unchecked( Some(FileSystemPath::new_normalized_unchecked(
ResolvedVc::upcast(*fs), ResolvedVc::upcast(*fs),
...@@ -56,7 +59,7 @@ impl DiskFileSystemMap { ...@@ -56,7 +59,7 @@ 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(BTreeMap::new()).cell() DiskFileSystemMap(FrozenMap::new()).cell()
} }
operation() operation()
} }
......
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