From 2d770b85f8c8f7660d4831245a81cd519d032622 Mon Sep 17 00:00:00 2001 From: losfair Date: Wed, 14 Aug 2024 23:52:19 +0800 Subject: [PATCH] fix: cache negative package.json lookups --- ext/node/package_json.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs index b28207db8b..1c6eaff5e2 100644 --- a/ext/node/package_json.rs +++ b/ext/node/package_json.rs @@ -5,6 +5,7 @@ use deno_config::package_json::PackageJsonRc; use deno_fs::DenoConfigFsAdapter; use std::cell::RefCell; use std::collections::HashMap; +use std::collections::HashSet; use std::io::ErrorKind; use std::path::Path; use std::path::PathBuf; @@ -42,6 +43,15 @@ pub fn load_pkg_json( fs: &dyn deno_fs::FileSystem, path: &Path, ) -> Result, PackageJsonLoadError> { + // XXX: Quick hack to workaround missing negative lookup cache + thread_local! { + static NEGATIVE_CACHE: RefCell> = RefCell::new(HashSet::new()); + } + + if NEGATIVE_CACHE.with(|cache| cache.borrow().contains(path)) { + return Ok(None); + } + let result = PackageJson::load_from_path( path, &DenoConfigFsAdapter::new(fs), @@ -51,7 +61,12 @@ pub fn load_pkg_json( Ok(pkg_json) => Ok(Some(pkg_json)), Err(deno_config::package_json::PackageJsonLoadError::Io { 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)), } }