Skip to content

Commit

Permalink
feat(memory): significantly reduce memory use during resolution (#203)
Browse files Browse the repository at this point in the history
Fixes: #202

With this patch, memory use during resolution is significantly decreased. There's a small perf sacrifice, but I think it's fine and very worth it.

| pm | max res memory |
|---|---|
| oro (w/ this PR) | 274.5 mb |
| oro (pre-patch) | 724.5 mb |
| Yarn | 663.3 mb |
| pnpm | 943.8 mb |
| NPM | 1,028.8 mb |
| bun | 2,761.6 mb |
  • Loading branch information
zkat committed Mar 13, 2023
1 parent e36356c commit f7fb85d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions crates/nassun/src/client.rs
Expand Up @@ -27,6 +27,7 @@ pub struct NassunOpts {
base_dir: Option<PathBuf>,
default_tag: Option<String>,
registries: HashMap<Option<String>, Url>,
memoize_metadata: bool,
}

impl NassunOpts {
Expand Down Expand Up @@ -65,6 +66,15 @@ impl NassunOpts {
self
}

/// Whether to memoize package metadata. This will keep any processed
/// packuments in memory for the lifetime of this `Nassun` instance.
/// Setting this to `true` may increase performance when fetching many
/// packages, at the cost of significant additional memory usage.
pub fn memoize_metadata(mut self, memoize: bool) -> Self {
self.memoize_metadata = memoize;
self
}

/// Build a new Nassun instance from this options object.
pub fn build(self) -> Nassun {
let registry = self
Expand Down Expand Up @@ -104,6 +114,7 @@ impl NassunOpts {
#[allow(clippy::redundant_clone)]
client.clone(),
self.registries,
self.memoize_metadata,
)),
#[cfg(not(target_arch = "wasm32"))]
dir_fetcher: Arc::new(DirFetcher::new()),
Expand Down
26 changes: 20 additions & 6 deletions crates/nassun/src/fetch/npm.rs
Expand Up @@ -18,17 +18,23 @@ use crate::resolver::PackageResolution;
pub(crate) struct NpmFetcher {
client: OroClient,
registries: HashMap<Option<String>, Url>,
cache_packuments: bool,
packuments: DashMap<String, Arc<Packument>>,
corgi_packuments: DashMap<String, Arc<CorgiPackument>>,
}

impl NpmFetcher {
pub(crate) fn new(client: OroClient, registries: HashMap<Option<String>, Url>) -> Self {
pub(crate) fn new(
client: OroClient,
registries: HashMap<Option<String>, Url>,
cache_packuments: bool,
) -> Self {
Self {
client,
registries,
packuments: DashMap::new(),
corgi_packuments: DashMap::new(),
cache_packuments,
}
}
}
Expand Down Expand Up @@ -99,12 +105,16 @@ impl PackageFetcher for NpmFetcher {
} = spec.target()
{
if let Some(packument) = self.corgi_packuments.get(name) {
return Ok(packument.value().clone());
if self.cache_packuments {
return Ok(packument.value().clone());
}
}
let client = self.client.with_registry(self.pick_registry(scope));
let packument = Arc::new(client.corgi_packument(&name).await?);
self.corgi_packuments
.insert(name.clone(), packument.clone());
if self.cache_packuments {
self.corgi_packuments
.insert(name.clone(), packument.clone());
}
Ok(packument)
} else {
unreachable!("How did a non-Npm resolution get here?");
Expand All @@ -126,11 +136,15 @@ impl PackageFetcher for NpmFetcher {
} = pkg
{
if let Some(packument) = self.packuments.get(name) {
return Ok(packument.value().clone());
if self.cache_packuments {
return Ok(packument.value().clone());
}
}
let client = self.client.with_registry(self.pick_registry(scope));
let packument = Arc::new(client.packument(&name).await?);
self.packuments.insert(name.clone(), packument.clone());
if self.cache_packuments {
self.packuments.insert(name.clone(), packument.clone());
}
Ok(packument)
} else {
unreachable!()
Expand Down

0 comments on commit f7fb85d

Please sign in to comment.