Skip to content

Conversation

@rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Aug 27, 2025

The lazy property getters we inject to avoid having to read all files of the entire aws-cdk-lib library upon module loading (so to speed up
time-to-executing-first-statement) is adding significant overhead for programs with a large number of statements.

Roughly:

// This avoids having to read the './core' module at startup, only do it
// if and when 'AspectPriority' gets accessed
Object.defineProperty(exports, "AspectPriority", { get: () => require("./core").AspectPriority });

However, the result of this is that every property access requires running one or more getters (one for every level of indirecting file). This may have a noticeable effect, on the order of ~1us per property access.

To cut down on that, we will do the lazy getters differently: after the initial lazy load, we overwrite the getter with the actual value. That way subsequent accesses can bypass the getters and be fast:

// Only execute getter on first access
Object.defineProperty(exports, "AspectPriority", { get: () => {
  const value = require("./core").AspectPriority;

  // Overwrite getter with real value once we have it
  Object.defineProperty(exports, "AspectPriority", { value });

  return value;
});

This should achieve both goals of fast initial load and high throughput for subsequent accesses.


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

This PR is a bit speculative; we think that the lazy property getters we
inject to avoid having to read all files of the entire `aws-cdk-lib`
library upon module loading (so to speed up
time-to-executing-first-statement) is adding significant overhead for
programs with a large number of statements.

Roughly:

```ts
// This avoids having to read the './core' module at startup, only do it
// if and when 'AspectPriority' gets accessed
Object.defineProperty(exports, "AspectPriority", { get: () => require("./core").AspectPriority });
```

However, the result of this is that every property access requires
running one or more getters (one for every level of indirecting file).
This may have a noticeable effect, on the order of ~1us per property
access.

To cut down on that, we will do the lazy getters differently: after
the initial lazy load, we overwrite the getter with the actual value.
That way subsequent accesses can bypass the getters and be fast:

```ts
// Only execute getter on first access
Object.defineProperty(exports, "AspectPriority", { get: () => {
  const value = require("./core").AspectPriority;

  // Overwrite getter with real value once we have it
  Object.defineProperty(exports, "AspectPriority", { value });

  return value;
});
```

This should achieve both goals of fast initial load and high throughput
for subsequent accesses.
@rix0rrr rix0rrr requested a review from a team August 27, 2025 09:59
@aws-cdk-automation aws-cdk-automation requested a review from a team August 27, 2025 09:59
@github-actions github-actions bot added the p2 label Aug 27, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 27, 2025
Copy link
Collaborator

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

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 added pr-linter/exempt-test The PR linter will not require test changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Aug 27, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review August 27, 2025 11:00

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

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Aug 28, 2025

Confirmed that it has the desired effect on a large CDK app inside Amazon

@rix0rrr rix0rrr marked this pull request as ready for review August 28, 2025 06:56
@rix0rrr rix0rrr changed the title fix: lazy getters slow down large programs fix: method to improve library load time slows down large programs Aug 28, 2025
@rix0rrr rix0rrr changed the title fix: method to improve library load time slows down large programs fix: lazy loading to startup time slows down large programs Aug 28, 2025
@rix0rrr rix0rrr changed the title fix: lazy loading to startup time slows down large programs fix: lazy loading to improve startup time slows down large programs Aug 28, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Aug 28, 2025
@mergify
Copy link
Contributor

mergify bot commented Aug 28, 2025

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).

@kumvprat kumvprat removed the pr-linter/exempt-test The PR linter will not require test changes label Aug 28, 2025
@mergify
Copy link
Contributor

mergify bot commented Aug 28, 2025

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 merged commit 6755a4e into main Aug 28, 2025
23 checks passed
@mergify mergify bot deleted the huijbers/reify-lazy-loaded-properties branch August 28, 2025 14:48
@github-actions
Copy link
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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2025
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/needs-maintainer-review This PR needs a review from a Core Team Member 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.

3 participants