Skip to content

fix(core): stack.node.addDependency gets slower as stacks grow - #38314

Merged
mergify[bot] merged 20 commits into
mainfrom
huijbers/dependencies-quadratic
Jul 17, 2026
Merged

mergify[bot] merged 20 commits into
mainfrom
huijbers/dependencies-quadratic

Conversation

@rix0rrr

@rix0rrr rix0rrr commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

source.node.addDependency(target) is used to confer dependencies between all resources under source to all resources under target.

The way this was done is to first expand the two sets into a Cartesian product, and then apply the dependency individually for each pair. The applied dependency can lead to a CloudFormation DependsOn relationship between the target resource (or the Nested Stack containing it), or a Stack-to-Stack dependency if the resources are in different stacks.

However, if this method of adding dependencies is applied to Stacks, we would first make all pairs of resources from (sourceStack, targetStack), apply the dependency, and find out that each of those dependencies would ultimately just lead to a single stack-to-stack dependency. This method of applying dependencies is quadratic in the size of the stack, whereas it should be linear in the number of stack-to-stack dependencies applied.

In this PR, we rework the dependencies mechanism to remove the quadraticness if node.addDependency is used to add dependencies between stacks: we resolve the constructs that should be involved in the dependency operation first, and only expand to the cartesian product of individual resources if we end up inside the same Stack (in which case the construct groups are on the order of single digits). This makes the cross-stack case a lot faster.

(Note: I'm saying "superlinear" in the body because although it seems like the work should be quadratic, from testing it seems to be a lot worse than quadratic -- perhaps even exponential. In any case anything worse than linear is baaaaaddd so that's what I'm writing it down as 😉 )

Because there are multiple ways of adding dependencies, renamed some of them to make the distinction more clear:

  • construct.node.addDependency(construct): add a construct-level description of a dependency that will get reified at synthesis time.
  • stack.addStackDependency(stack), resource.addResourceDependency(resource): directly applies a dependencies between stacks or resources.
    • These used to be called stack.addDependency and resource.addDependency, which is confusing because now stack.addDependency and stack.node.addDependency look very similar but do very different things. So I renamed them.
    • Unfortunately resource.addResourceDependency also performs for cross-stack dependencies, which I'd much prefer it hadn't so it could stay simple and predictable, but we can't take this away anymore.
  • Because we need to account for resource dependencies leading to stack dependencies (grr), we need to make a distinction between public API and mutation API, which are now under _addStackDependency, _addResourceDependency etc.

The flow is now this:

┌───────────────────┐       ┌───────────────────┐      ┌───────────────────┐
│                   │       │stack.addStackDepen│      │resoure.addResource│
│node.addDependency │       │       dency       │      │    Dependency     │
│                   │       │                   │      │                   │
└───────────────────┘       └───────────────────┘      └───────────────────┘
          │                           │                          │          
   (at synth time)              (immediately)              (immediately)    
          └───────────────────────────┼──────────────────────────┘          
                                      │                                     
                                      ▼                                     
                    ┌───────────────────────────────────┐                   
                    │                                   │                   
                    │    dispatchDependencyOperation    │                   
                    │                                   │                   
                    └───────────────────────────────────┘                   
                                      │                                     
                                      │                                     
                        ┌─────────────┴────────────┐                        
                        │                          │                        
                        │                          │                        
                        ▼                          ▼                        
              ┌───────────────────┐      ┌───────────────────┐              
              │stack._addStackDepe│      │resoure._addResourc│              
              │      ndency       │      │    eDependency    │              
              │                   │      │                   │              
              └───────────────────┘      └───────────────────┘              

Everything dispatches through the same code so that the behavior is the same regardless of the entry API that is chosen.

CHEAKING BRANGE: This PR removes obtainResourceDependencies; this was used as an internal function with nonstandard semantics. It has been moved to fully internal and is no longer usable externally (using different words because there is no way for me to format the PR title for a break in a way that the PR linter accepts). I tried fix!:`` and fix(core)!` and it keeps on complaining that there's something wrong. So I'm giving up.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

rix0rrr added 2 commits July 14, 2026 15:31
Because there are multiple dependency types, reword some methods
to make the distinction clear:

- `stack.addDependency(stack)`: now called `addStackDependency`
- `res.addDependency(res)`: now called `addResourceDependency`
@rix0rrr
rix0rrr requested a review from a team as a code owner July 15, 2026 11:47
@github-actions github-actions Bot added the p2 label Jul 15, 2026
@aws-cdk-automation
aws-cdk-automation requested a review from a team July 15, 2026 11:48
@mergify mergify Bot added the contribution/core This is a PR that came from AWS. label Jul 15, 2026
@mergify
mergify Bot temporarily deployed to automation July 15, 2026 11:48 Inactive
@mergify
mergify Bot temporarily deployed to automation July 15, 2026 11:48 Inactive
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This pull request description does not follow the correct template structure.

PRs without a linked issue will receive lower priority for review and merging. Please update the description to follow the PR template and include a line like Closes #123 in the Issue section. If no existing issue matches your change, create one first.

@aws-cdk-automation aws-cdk-automation left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(This review is outdated)

@rix0rrr rix0rrr changed the title fix: stack.node.addDependency is superlinear in size of stacks fix: stack.node.addDependency gets slower as stacks grow Jul 15, 2026

@ShadowCat567 ShadowCat567 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks reasonable, some tests relating to disallowing parent/child dependencies (parent target/child source) are still broken

*
* @internal
*/
public _addAssemblyDependency(target: Stack, reason: StackDependencyReason = {}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why can we remove these _[add/remove/obtain]AssemblyDependency methods? Does _[add/remove/obtain]StackDependency do the work the assemblyDependency method used to do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The work has now been diffused between dispatchDependencyOperation (check most of the prerequisites, except cycles) and _addStackDependency.

@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Jul 17, 2026

@aws-cdk-automation aws-cdk-automation left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(This review is outdated)

@rix0rrr rix0rrr changed the title fix: stack.node.addDependency gets slower as stacks grow fix!: stack.node.addDependency gets slower as stacks grow Jul 17, 2026
@rix0rrr rix0rrr changed the title fix!: stack.node.addDependency gets slower as stacks grow fix(core)!: stack.node.addDependency gets slower as stacks grow Jul 17, 2026
@rix0rrr rix0rrr changed the title fix(core)!: stack.node.addDependency gets slower as stacks grow fix(core): stack.node.addDependency gets slower as stacks grow Jul 17, 2026
@aws-cdk-automation
aws-cdk-automation dismissed their stale review July 17, 2026 09:38

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@mergify

mergify Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify

mergify Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-17 09:51 UTC · Rule: default-squash · triggered by rule automatic merge
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-07-17 10:27 UTC · at 2beccef581b6cccdc6ede89e61de16e65ddc450b · squash

This pull request spent 35 minutes 51 seconds in the queue, including 35 minutes 32 seconds running CI.

Required conditions to merge

@mergify
mergify Bot temporarily deployed to automation July 17, 2026 09:51 Inactive
@mergify

mergify Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@github-actions

Copy link
Copy Markdown
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@mrgrain

mrgrain commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Partially reverted in #38417 due to regression #38406

Added test to protect against regression in #38431

This branch was previously deployed

1 inactive deployment
automation — 2beccef5 Deployed Jul 17, 2026 by mergify[bot] via validate-pr #355051
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants