From d8c6ec4b7f8c1e750245ca77937698b51b06d74b Mon Sep 17 00:00:00 2001 From: David Michon Date: Thu, 30 Jan 2025 22:56:10 +0000 Subject: [PATCH 1/3] src: cache negative results in GetPackageJSON This change ensures that GetPackageJSON caches which folders do *not* contain a package.json, to prevent excessive file system probing. --- src/node_modules.cc | 9 +++++++++ src/node_modules.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/node_modules.cc b/src/node_modules.cc index 38d2c65c7f3282..035c1a8c159fbc 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -100,10 +100,19 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( return &cache_entry->second; } + auto missing_cache_entry = binding_data->missing_package_configs_.find(path.data()); + if (missing_cache_entry != binding_data->missing_package_configs_.end()) { + return nullptr; + } + PackageConfig package_config{}; package_config.file_path = path; // No need to exclude BOM since simdjson will skip it. if (ReadFileSync(&package_config.raw_json, path.data()) < 0) { + // Cache the failed read so that other queries in the same folder don't + // keep probing the file system + binding_data->missing_package_configs_.insert( + std::string(path)); return nullptr; } // In some systems, std::string is annotated to generate an diff --git a/src/node_modules.h b/src/node_modules.h index 17909b2270454b..f70cefb1850996 100644 --- a/src/node_modules.h +++ b/src/node_modules.h @@ -74,6 +74,7 @@ class BindingData : public SnapshotableObject { private: std::unordered_map package_configs_; + std::unordered_set missing_package_configs_; simdjson::ondemand::parser json_parser; // returns null on error static const PackageConfig* GetPackageJSON( From 56fdf78818142c3c869ae7cf2b8db75c2a417b58 Mon Sep 17 00:00:00 2001 From: David Michon Date: Fri, 31 Jan 2025 00:17:25 +0000 Subject: [PATCH 2/3] use .contains() --- src/node_modules.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/node_modules.cc b/src/node_modules.cc index 035c1a8c159fbc..29c1a919266fd3 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -100,8 +100,7 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( return &cache_entry->second; } - auto missing_cache_entry = binding_data->missing_package_configs_.find(path.data()); - if (missing_cache_entry != binding_data->missing_package_configs_.end()) { + if (!binding_data->missing_package_configs_.contains(path.data())) { return nullptr; } From 4c928e6f3cf0a933d373f1a5f167b9816eafc8b2 Mon Sep 17 00:00:00 2001 From: David Michon Date: Fri, 31 Jan 2025 11:29:50 -0800 Subject: [PATCH 3/3] Update src/node_modules.cc Fixed incorrect negation Co-authored-by: Yagiz Nizipli --- src/node_modules.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_modules.cc b/src/node_modules.cc index 29c1a919266fd3..2841c5815240e6 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -100,7 +100,7 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( return &cache_entry->second; } - if (!binding_data->missing_package_configs_.contains(path.data())) { + if (binding_data->missing_package_configs_.contains(std::string(path))) { return nullptr; }