summaryrefslogtreecommitdiff
path: root/ext/node/package_json.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-04-01 09:10:04 -0400
committerGitHub <noreply@github.com>2024-04-01 09:10:04 -0400
commit240b362c002d17bc2b676673ed1b9406683ff0c2 (patch)
treed0c3cdc099b7cac4f29ae17f7ba03ee01a01c877 /ext/node/package_json.rs
parent8d158058e5abab14d122712a716979b865620995 (diff)
perf(node): put pkg json into an `Rc` (#23156)
Was doing a bit of debugging on why some stuff is not working in a personal project and ran a quick debug profile and saw it cloning the pkg json a lot. We should put this in an Rc.
Diffstat (limited to 'ext/node/package_json.rs')
-rw-r--r--ext/node/package_json.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs
index 9ac3a0969..77352ae1d 100644
--- a/ext/node/package_json.rs
+++ b/ext/node/package_json.rs
@@ -18,9 +18,10 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::io::ErrorKind;
use std::path::PathBuf;
+use std::rc::Rc;
thread_local! {
- static CACHE: RefCell<HashMap<PathBuf, PackageJson>> = RefCell::new(HashMap::new());
+ static CACHE: RefCell<HashMap<PathBuf, Rc<PackageJson>>> = RefCell::new(HashMap::new());
}
#[derive(Clone, Debug, Serialize)]
@@ -66,7 +67,7 @@ impl PackageJson {
resolver: &dyn NpmResolver,
permissions: &dyn NodePermissions,
path: PathBuf,
- ) -> Result<PackageJson, AnyError> {
+ ) -> Result<Rc<PackageJson>, AnyError> {
resolver.ensure_read_permission(permissions, &path)?;
Self::load_skip_read_permission(fs, path)
}
@@ -74,7 +75,7 @@ impl PackageJson {
pub fn load_skip_read_permission(
fs: &dyn deno_fs::FileSystem,
path: PathBuf,
- ) -> Result<PackageJson, AnyError> {
+ ) -> Result<Rc<PackageJson>, AnyError> {
assert!(path.is_absolute());
if CACHE.with(|cache| cache.borrow().contains_key(&path)) {
@@ -84,7 +85,7 @@ impl PackageJson {
let source = match fs.read_text_file_sync(&path) {
Ok(source) => source,
Err(err) if err.kind() == ErrorKind::NotFound => {
- return Ok(PackageJson::empty(path));
+ return Ok(Rc::new(PackageJson::empty(path)));
}
Err(err) => bail!(
"Error loading package.json at {}. {:#}",
@@ -93,7 +94,7 @@ impl PackageJson {
),
};
- let package_json = Self::load_from_string(path, source)?;
+ let package_json = Rc::new(Self::load_from_string(path, source)?);
CACHE.with(|cache| {
cache
.borrow_mut()