fix: cache negative package.json lookups

This commit is contained in:
losfair 2024-08-14 23:52:19 +08:00
parent 68f03a7ebf
commit 2d770b85f8

View File

@ -5,6 +5,7 @@ use deno_config::package_json::PackageJsonRc;
use deno_fs::DenoConfigFsAdapter; use deno_fs::DenoConfigFsAdapter;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@ -42,6 +43,15 @@ pub fn load_pkg_json(
fs: &dyn deno_fs::FileSystem, fs: &dyn deno_fs::FileSystem,
path: &Path, path: &Path,
) -> Result<Option<PackageJsonRc>, PackageJsonLoadError> { ) -> Result<Option<PackageJsonRc>, PackageJsonLoadError> {
// XXX: Quick hack to workaround missing negative lookup cache
thread_local! {
static NEGATIVE_CACHE: RefCell<HashSet<PathBuf>> = RefCell::new(HashSet::new());
}
if NEGATIVE_CACHE.with(|cache| cache.borrow().contains(path)) {
return Ok(None);
}
let result = PackageJson::load_from_path( let result = PackageJson::load_from_path(
path, path,
&DenoConfigFsAdapter::new(fs), &DenoConfigFsAdapter::new(fs),
@ -51,7 +61,12 @@ pub fn load_pkg_json(
Ok(pkg_json) => Ok(Some(pkg_json)), Ok(pkg_json) => Ok(Some(pkg_json)),
Err(deno_config::package_json::PackageJsonLoadError::Io { Err(deno_config::package_json::PackageJsonLoadError::Io {
source, .. source, ..
}) if source.kind() == ErrorKind::NotFound => Ok(None), }) if source.kind() == ErrorKind::NotFound => {
NEGATIVE_CACHE.with(|cache| {
cache.borrow_mut().insert(path.to_path_buf());
});
Ok(None)
}
Err(err) => Err(PackageJsonLoadError(err)), Err(err) => Err(PackageJsonLoadError(err)),
} }
} }