fix(npm): memoize peer cache hit checks to prevent combinatorial explosion#32609
Merged
bartlomieju merged 3 commits intomainfrom Mar 10, 2026
Merged
fix(npm): memoize peer cache hit checks to prevent combinatorial explosion#32609bartlomieju merged 3 commits intomainfrom
bartlomieju merged 3 commits intomainfrom
Conversation
…osion When resolving peer dependencies for packages with many mutual peer deps (e.g. AWS CDK v1), `find_peers_cache_hit_inner` was called recursively millions of times. Since `parent_pkgs` is constant within a single `find_peers_cache_hit` call, the result for any given `nv` is always the same. Adding a memo HashMap eliminates redundant recursive checks, turning `deno add npm:@aws-cdk/aws-ecs` from hanging indefinitely to completing in seconds. Closes #26430 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kajukitli
approved these changes
Mar 9, 2026
Contributor
kajukitli
left a comment
There was a problem hiding this comment.
lgtm, classic memoization fix for exponential blowup
the key insight is correct: parent_pkgs is constant for the entire call tree, so once you know whether nv X has a matching cache entry, that answer won't change. memoizing it collapses O(2^n) back to O(n).
good that you don't memoize the cycle-detection early return (checking.insert returns false) — that result is path-dependent.
2.5M calls → memoized lookups is a nice win 👍
dsherret
suggested changes
Mar 10, 2026
Contributor
dsherret
left a comment
There was a problem hiding this comment.
We should get a test for this before merging. Ideally this would be a test in deno_npm.
Adds a test with 25 mutually peer-dependent packages that hangs indefinitely without the memoization fix but completes in <1s with it. Regression test for #26430 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
bartlomieju
commented
Mar 10, 2026
| /// millions of redundant recursive calls and hangs indefinitely. | ||
| /// Regression test for https://github.com/denoland/deno/issues/26430 | ||
| #[tokio::test] | ||
| async fn peer_deps_mutual_many_packages_no_hang() { |
Member
Author
There was a problem hiding this comment.
I confirmed this test hangs on main and burns 100% CPU. It passes fine in this PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
deno add npm:@aws-cdk/aws-ecs(and similar large peer dep trees) hanging indefinitelyfind_peers_cache_hit_innerinlibs/npm/resolution/graph.rsrecursively checks whether cached peer resolutions match the current context. For packages with many mutual peer dependencies (like AWS CDK v1 with ~33 packages and 1200+ cache entries), this causes combinatorial explosion — a single cache lookup for@aws-cdk/aws-kmstriggered 2.5 million recursive calls taking 17+ secondsparent_pkgsis constant within a singlefind_peers_cache_hitcall, the answer for any givennvnever changes. AHashMapmemo eliminates all redundant recursive workBefore/After
deno add npm:@aws-cdk/aws-ecsTest plan
deno_npmunit tests passdeno add npm:@aws-cdk/aws-ecscompletes successfullyCloses #26430
🤖 Generated with Claude Code