Skip to content

perf: cache dynamic imports of nodes #10080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 28, 2023
Merged

perf: cache dynamic imports of nodes #10080

merged 12 commits into from
Jun 28, 2023

Conversation

gtm-nayan
Copy link
Contributor

Dunno why but node spends a lot of time in esm loader land even for modules that have already been imported. This PR makes it so that the dynamic imports are only called once and that value is cached for any future calls. This increases rps by a little over 1.5x on my machine.

Before:
image

After:
image

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com./sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Jun 1, 2023

🦋 Changeset detected

Latest commit: f7df598

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gtm-nayan gtm-nayan marked this pull request as ready for review June 1, 2023 16:00
@Rich-Harris
Copy link
Member

Is there any reason we wouldn't always cache loaders? There's no danger of a memory leak since you can't clear the module cache anyway

@gtm-nayan
Copy link
Contributor Author

I was thinking of dev mode and hosts which would create a new environment for each request.

@benmccann
Copy link
Member

I was curious based off your comments on Discord, would we implement this differently if we could make breaking changes? just wondering if we should add a TODO for kit v2?

@gtm-nayan
Copy link
Contributor Author

gtm-nayan commented Jun 1, 2023

Maybe? Not necessarily Kit v2 but it'd be a breaking change for adapters.

I was thinking of adding something like injectImport to builder.generateManifest params and then using that to pull the __memo function from @sveltejs/kit/private-utils, same for all the component wrappers.

But thinking about it more, that isn't much cleaner so I'd likely leave it as is. Unless a better solution comes along.

@@ -65,8 +65,8 @@ export function generate_manifest({ build_data, relative_path, routes }) {

// prettier-ignore
// String representation of
/** @type {import('@sveltejs/kit').SSRManifest} */
return dedent`
/** @template {import('@sveltejs/kit').SSRManifest} T */
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's correct to use @template here. That's for type parameters and not template literals: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template. Unless maybe I'm missing where the T is?

Copy link
Contributor Author

@gtm-nayan gtm-nayan Jun 7, 2023

Choose a reason for hiding this comment

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

it's a hack to get VSCode to create a jump link for the SSRManifest. setting it as @type gave an error because the string is not of the type SSRManifest

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Anything else that needs to be adressed here (besides my comment suggestions) or is this good to go?

headers.set('content-length', encoder.encode(body).byteLength.toString());
const encoded = encoder.encode(body);
headers.set('content-length', encoded.byteLength.toString());
return new Response(encoded, {
Copy link
Member

Choose a reason for hiding this comment

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

is this a drive-by fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another perf thing to avoid duplicating the encoding work inside the Response constructor.

@@ -44,7 +44,7 @@ export function generate_manifest({ build_data, relative_path, routes }) {
);

/** @type {(path: string) => string} */
const loader = (path) => `() => import('${path}')`;
const loader = (path) => `__memo(() => import('${path}'))`;
Copy link
Member

Choose a reason for hiding this comment

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

Small comment maybe why we memo the import

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment near the __memo declaration below. That should suffice.

return { ${Array.from(matchers).join(', ')} };
}
}
}
`;

return dedent`
Copy link
Member

Choose a reason for hiding this comment

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

Small comment maybe why we need the memo

@gtm-nayan
Copy link
Contributor Author

Ugh, the lint, I should really stop using Github for edits lol. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants