Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: node resolution cache #27838

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use deno_runtime::deno_web::BlobStore;
use deno_runtime::inspector_server::InspectorServer;
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
use node_resolver::analyze::NodeCodeTranslator;
use node_resolver::cache::NodeResolutionThreadLocalCache;
use once_cell::sync::OnceCell;
use sys_traits::EnvCurrentDir;

Expand Down Expand Up @@ -643,6 +644,7 @@ impl CliFactory {
self.workspace_factory()?.clone(),
ResolverFactoryOptions {
conditions_from_resolution_mode: Default::default(),
node_resolution_cache: Some(Arc::new(NodeResolutionThreadLocalCache)),
no_sloppy_imports_cache: false,
npm_system_info: self.flags.subcommand.npm_system_info(),
specified_import_map: Some(Box::new(CliSpecifiedImportMapProvider {
Expand Down
13 changes: 10 additions & 3 deletions cli/lsp/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq;
use indexmap::IndexMap;
use node_resolver::cache::NodeResolutionSys;
use node_resolver::cache::NodeResolutionThreadLocalCache;
use node_resolver::DenoIsBuiltInNodeModuleChecker;
use node_resolver::NodeResolutionKind;
use node_resolver::PackageJsonThreadLocalCache;
Expand Down Expand Up @@ -259,7 +261,7 @@ impl LspScopeResolver {
root_node_modules_dir: byonm_npm_resolver
.root_node_modules_path()
.map(|p| p.to_path_buf()),
sys: factory.sys.clone(),
sys: factory.node_resolution_sys.clone(),
pkg_json_resolver: self.pkg_json_resolver.clone(),
},
)
Expand Down Expand Up @@ -673,6 +675,7 @@ struct ResolverFactoryServices {
struct ResolverFactory<'a> {
config_data: Option<&'a Arc<ConfigData>>,
pkg_json_resolver: Arc<CliPackageJsonResolver>,
node_resolution_sys: NodeResolutionSys<CliSys>,
sys: CliSys,
services: ResolverFactoryServices,
}
Expand All @@ -688,6 +691,10 @@ impl<'a> ResolverFactory<'a> {
Self {
config_data,
pkg_json_resolver,
node_resolution_sys: NodeResolutionSys::new(
sys.clone(),
Some(Arc::new(NodeResolutionThreadLocalCache)),
),
sys,
services: Default::default(),
}
Expand All @@ -706,7 +713,7 @@ impl<'a> ResolverFactory<'a> {
let sys = CliSys::default();
let options = if enable_byonm {
CliNpmResolverCreateOptions::Byonm(CliByonmNpmResolverCreateOptions {
sys: self.sys.clone(),
sys: self.node_resolution_sys.clone(),
pkg_json_resolver: self.pkg_json_resolver.clone(),
root_node_modules_dir: self.config_data.and_then(|config_data| {
config_data.node_modules_dir.clone().or_else(|| {
Expand Down Expand Up @@ -930,7 +937,7 @@ impl<'a> ResolverFactory<'a> {
DenoIsBuiltInNodeModuleChecker,
npm_resolver.clone(),
self.pkg_json_resolver.clone(),
self.sys.clone(),
self.node_resolution_sys.clone(),
node_resolver::ConditionsFromResolutionMode::default(),
)))
})
Expand Down
3 changes: 3 additions & 0 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use indexmap::IndexMap;
use indexmap::IndexSet;
use lazy_regex::lazy_regex;
use log::error;
use node_resolver::cache::NodeResolutionThreadLocalCache;
use node_resolver::ResolutionMode;
use once_cell::sync::Lazy;
use regex::Captures;
Expand Down Expand Up @@ -4622,6 +4623,7 @@ fn op_resolve_inner(
let state = state.borrow_mut::<State>();
let mark = state.performance.mark_with_args("tsc.op.op_resolve", &args);
let referrer = state.specifier_map.normalize(&args.base)?;
NodeResolutionThreadLocalCache::clear();
let specifiers = state
.state_snapshot
.documents
Expand All @@ -4640,6 +4642,7 @@ fn op_resolve_inner(
})
})
.collect();
NodeResolutionThreadLocalCache::clear();
Copy link
Member Author

@dsherret dsherret Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should improve this to be smarter. Right now I just have it clearing before and after each op_resolve.

state.performance.measure(mark);
Ok(specifiers)
}
Expand Down
6 changes: 4 additions & 2 deletions cli/rt/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use deno_runtime::WorkerExecutionMode;
use deno_runtime::WorkerLogLevel;
use deno_semver::npm::NpmPackageReqReference;
use node_resolver::analyze::NodeCodeTranslator;
use node_resolver::cache::NodeResolutionSys;
use node_resolver::errors::ClosestPkgJsonError;
use node_resolver::DenoIsBuiltInNodeModuleChecker;
use node_resolver::NodeResolutionKind;
Expand Down Expand Up @@ -689,6 +690,7 @@ pub async fn run(
};
NpmRegistryReadPermissionChecker::new(sys.clone(), mode)
};
let node_resolution_sys = NodeResolutionSys::new(sys.clone(), None);
let (in_npm_pkg_checker, npm_resolver) = match metadata.node_modules {
Some(NodeModules::Managed { node_modules_dir }) => {
// create an npmrc that uses the fake npm_registry_url to resolve packages
Expand Down Expand Up @@ -738,7 +740,7 @@ pub async fn run(
DenoInNpmPackageChecker::new(CreateInNpmPkgCheckerOptions::Byonm);
let npm_resolver = NpmResolver::<DenoRtSys>::new::<DenoRtSys>(
NpmResolverCreateOptions::Byonm(ByonmNpmResolverCreateOptions {
sys: sys.clone(),
sys: node_resolution_sys.clone(),
pkg_json_resolver: pkg_json_resolver.clone(),
root_node_modules_dir,
}),
Expand Down Expand Up @@ -782,7 +784,7 @@ pub async fn run(
DenoIsBuiltInNodeModuleChecker,
npm_resolver.clone(),
pkg_json_resolver.clone(),
sys.clone(),
node_resolution_sys,
node_resolver::ConditionsFromResolutionMode::default(),
));
let cjs_tracker = Arc::new(CjsTracker::new(
Expand Down
5 changes: 5 additions & 0 deletions ext/node/ops/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use deno_package_json::PackageJsonRc;
use deno_path_util::normalize_path;
use deno_path_util::url_from_file_path;
use deno_path_util::url_to_file_path;
use node_resolver::cache::NodeResolutionThreadLocalCache;
use node_resolver::errors::ClosestPkgJsonError;
use node_resolver::InNpmPackageChecker;
use node_resolver::NodeResolutionKind;
Expand Down Expand Up @@ -523,6 +524,8 @@ pub fn op_require_try_self<
TSys,
>>();
let referrer = UrlOrPathRef::from_path(&pkg.path);
// invalidate the resolution cache in case things have changed
NodeResolutionThreadLocalCache::clear();
let r = node_resolver.package_exports_resolve(
&pkg.path,
&expansion,
Expand Down Expand Up @@ -626,6 +629,7 @@ pub fn op_require_resolve_exports<
} else {
Some(PathBuf::from(parent_path))
};
NodeResolutionThreadLocalCache::clear();
let r = node_resolver.package_exports_resolve(
&pkg.path,
&format!(".{expansion}"),
Expand Down Expand Up @@ -708,6 +712,7 @@ pub fn op_require_package_imports_resolve<
TNpmPackageFolderResolver,
TSys,
>>();
NodeResolutionThreadLocalCache::clear();
let url = node_resolver.package_imports_resolve(
&request,
Some(&UrlOrPathRef::from_path(&referrer_path)),
Expand Down
11 changes: 9 additions & 2 deletions resolvers/deno/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use deno_npm::NpmSystemInfo;
use deno_path_util::fs::canonicalize_path_maybe_not_exists;
use deno_path_util::normalize_path;
use futures::future::FutureExt;
use node_resolver::cache::NodeResolutionSys;
use node_resolver::ConditionsFromResolutionMode;
use node_resolver::DenoIsBuiltInNodeModuleChecker;
use node_resolver::NodeResolver;
Expand Down Expand Up @@ -555,6 +556,7 @@ pub struct ResolverFactoryOptions {
pub conditions_from_resolution_mode: ConditionsFromResolutionMode,
pub no_sloppy_imports_cache: bool,
pub npm_system_info: NpmSystemInfo,
pub node_resolution_cache: Option<node_resolver::NodeResolutionCacheRc>,
pub package_json_cache: Option<node_resolver::PackageJsonCacheRc>,
pub package_json_dep_resolution: Option<PackageJsonDepResolution>,
pub specified_import_map: Option<Box<dyn SpecifiedImportMapProvider>>,
Expand Down Expand Up @@ -584,6 +586,7 @@ pub struct ResolverFactory<
+ 'static,
> {
options: ResolverFactoryOptions,
sys: NodeResolutionSys<TSys>,
deno_resolver: async_once_cell::OnceCell<DefaultDenoResolverRc<TSys>>,
in_npm_package_checker: Deferred<DenoInNpmPackageChecker>,
node_resolver: Deferred<
Expand Down Expand Up @@ -639,6 +642,10 @@ impl<
options: ResolverFactoryOptions,
) -> Self {
Self {
sys: NodeResolutionSys::new(
workspace_factory.sys.clone(),
options.node_resolution_cache.clone(),
),
deno_resolver: Default::default(),
in_npm_package_checker: Default::default(),
node_resolver: Default::default(),
Expand Down Expand Up @@ -725,7 +732,7 @@ impl<
DenoIsBuiltInNodeModuleChecker,
self.npm_resolver()?.clone(),
self.pkg_json_resolver().clone(),
self.workspace_factory.sys.clone(),
self.sys.clone(),
self.options.conditions_from_resolution_mode.clone(),
)))
})
Expand Down Expand Up @@ -760,7 +767,7 @@ impl<
self.npm_resolver.get_or_try_init(|| {
Ok(NpmResolver::<TSys>::new::<TSys>(if self.use_byonm()? {
NpmResolverCreateOptions::Byonm(ByonmNpmResolverCreateOptions {
sys: self.workspace_factory.sys.clone(),
sys: self.sys.clone(),
pkg_json_resolver: self.pkg_json_resolver().clone(),
root_node_modules_dir: Some(
match self.workspace_factory.node_modules_dir_path()? {
Expand Down
13 changes: 7 additions & 6 deletions resolvers/deno/npm/byonm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use deno_path_util::url_to_file_path;
use deno_semver::package::PackageReq;
use deno_semver::StackString;
use deno_semver::Version;
use node_resolver::cache::NodeResolutionSys;
use node_resolver::errors::PackageFolderResolveError;
use node_resolver::errors::PackageFolderResolveIoError;
use node_resolver::errors::PackageJsonLoadError;
Expand Down Expand Up @@ -48,7 +49,7 @@ pub enum ByonmResolvePkgFolderFromDenoReqError {
pub struct ByonmNpmResolverCreateOptions<TSys: FsRead> {
// todo(dsherret): investigate removing this
pub root_node_modules_dir: Option<PathBuf>,
pub sys: TSys,
pub sys: NodeResolutionSys<TSys>,
pub pkg_json_resolver: PackageJsonResolverRc<TSys>,
}

Expand All @@ -60,7 +61,7 @@ pub type ByonmNpmResolverRc<TSys> =
pub struct ByonmNpmResolver<
TSys: FsCanonicalize + FsRead + FsMetadata + FsReadDir,
> {
sys: TSys,
sys: NodeResolutionSys<TSys>,
pkg_json_resolver: PackageJsonResolverRc<TSys>,
root_node_modules_dir: Option<PathBuf>,
}
Expand Down Expand Up @@ -136,14 +137,14 @@ impl<TSys: FsCanonicalize + FsRead + FsMetadata + FsReadDir>
referrer: &Url,
) -> Result<PathBuf, ByonmResolvePkgFolderFromDenoReqError> {
fn node_resolve_dir<TSys: FsCanonicalize + FsMetadata>(
sys: &TSys,
sys: &NodeResolutionSys<TSys>,
alias: &str,
start_dir: &Path,
) -> std::io::Result<Option<PathBuf>> {
for ancestor in start_dir.ancestors() {
let node_modules_folder = ancestor.join("node_modules");
let sub_dir = join_package_name(Cow::Owned(node_modules_folder), alias);
if sys.fs_is_dir_no_err(&sub_dir) {
if sys.is_dir(&sub_dir) {
return Ok(Some(
deno_path_util::fs::canonicalize_path_maybe_not_exists(
sys, &sub_dir,
Expand Down Expand Up @@ -385,7 +386,7 @@ impl<TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir>
referrer: &UrlOrPathRef,
) -> Result<PathBuf, PackageFolderResolveError> {
fn inner<TSys: FsMetadata>(
sys: &TSys,
sys: &NodeResolutionSys<TSys>,
name: &str,
referrer: &UrlOrPathRef,
) -> Result<PathBuf, PackageFolderResolveError> {
Expand All @@ -402,7 +403,7 @@ impl<TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir>
};

let sub_dir = join_package_name(node_modules_folder, name);
if sys.fs_is_dir_no_err(&sub_dir) {
if sys.is_dir(&sub_dir) {
return Ok(sub_dir);
}
}
Expand Down
Loading