kit icon indicating copy to clipboard operation
kit copied to clipboard

perf: cache dynamic imports of nodes

Open gtm-nayan opened this issue 2 years ago • 5 comments

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:

  • [x] 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

  • [x] 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:.

gtm-nayan avatar Jun 01 '23 15:06 gtm-nayan

🦋 Changeset detected

Latest commit: f7df598ea165f1bee08fecda5587044874bde69f

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

changeset-bot[bot] avatar Jun 01 '23 15:06 changeset-bot[bot]

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

Rich-Harris avatar Jun 01 '23 16:06 Rich-Harris

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

gtm-nayan avatar Jun 01 '23 16:06 gtm-nayan

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?

benmccann avatar Jun 01 '23 16:06 benmccann

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.

gtm-nayan avatar Jun 01 '23 16:06 gtm-nayan

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

gtm-nayan avatar Jun 28 '23 13:06 gtm-nayan