Skip to content
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

perf: reduce size of injected __vite__mapDeps code #16184

Merged
merged 5 commits into from Apr 5, 2024

Conversation

panstromek
Copy link
Contributor

@panstromek panstromek commented Mar 17, 2024

Description

This reduces size of injected __vite__mapDeps code by roughly a factor of 2 (not counting the dependency list) using few minification tricks.

The main ideas are:

  • replace function with arrow function, which is shorter (Especially when braces can be ommited)
  • extract dependency list to a separate variable which
    • allows both variables to be grouped
    • avoid referencing the long names multiple times
  • remove redundant whitespace and parens
  • shorten parameter names

Note that it also removes the lazy initialization of the dependency list array. I honestly don't see the benefit of doing it, but if it's desirable to keep it, there's another version of the function that keeps the behaviour unchanged (except for renaming the internal function object property, which is also optional, though):

const __vite__mapDeps=(i,f=__vite__mapDeps,d=(f.f||(f.f=[/*dependency list*/])))=>i.map(i=>d[i])

This version uses optional parameters to:

  • assign function name to a variable to avoid referencing the long name multiple times
  • lazy intialize the internal file list property and assign the result to local variable
  • optional parameters are used to avoid adding parenthesis to the function and adding const|let keyword

[edit]
I found a comment on original PR that explains why the lazy initialization is there. https://github.com/vitejs/vite/pull/14550/files#r1350449734

I might have to re-add it, but let's see if there's a demand for merging this in the first place. I tested this locally on my project and patched it in GitHub UI, so I haven't seen the impact on tests, yet.

Additional context

This optimization was first added here: #14550

cc @gajus who contributed the original implemetation.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

This also replaces the lazy initialization of with eager one.
Copy link

stackblitz bot commented Mar 17, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@panstromek panstromek changed the title Reduce size of injected __vite__mapDeps code perf: Reduce size of injected __vite__mapDeps code Mar 17, 2024
@panstromek
Copy link
Contributor Author

Lint failed but I couldn't find any linter output, so I don't see what is the problem

@bluwy bluwy changed the title perf: Reduce size of injected __vite__mapDeps code perf: reduce size of injected __vite__mapDeps code Mar 18, 2024
bluwy
bluwy previously approved these changes Mar 18, 2024
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Thanks! This looks great to me. Something I'd also like to followup later is to remove the mapDeps altogether if no preloading is needed for the file. Currently it's added unconditionally. And the changes could get a bit big.

Also related: #15300 (comment)

@bluwy bluwy added p2-nice-to-have Not breaking anything but nice to have (priority) performance Performance related enhancement labels Mar 18, 2024
@panstromek
Copy link
Contributor Author

Thanks! This looks great to me. Something I'd also like to followup later is to remove the mapDeps altogether if no preloading is needed for the file. Currently it's added unconditionally. And the changes could get a bit big.

Yea, I noticed that too. It can also be removed when there's only single callsite, which is another pretty common case (at least in my codebase).

(Even more generalized version could also omit it when the imported files at each callsite are distinct or distinct enough to outweigh the added code)

sapphi-red
sapphi-red previously approved these changes Mar 21, 2024
@patak-dev patak-dev added this to the 5.3 milestone Mar 21, 2024
@bluwy
Copy link
Member

bluwy commented Mar 21, 2024

I think this should be a simple fix in a patch and not for 5.3 🤔

@patak-dev
Copy link
Member

In the past, there have been downstream projects doing regexes over our preload handling to inject or modify stuff. So I'm always a bit worried about touching this part. I'll trigger a ecosystem CI run. I'm ok if you want to move forward with this in a patch if it is all green.

/ecosystem-ci run

@sapphi-red
Copy link
Member

I felt this to be in the blur between minor and patch. If the ecosystem-ci passes, I'm fine with merging it with a patch.
BTW the "/ecosystem-ci run" needs to be in the beginning of the comment (

if: github.repository == 'vitejs/vite' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/ecosystem-ci run')
).

@sapphi-red
Copy link
Member

/ecosystem-ci run

@bluwy
Copy link
Member

bluwy commented Mar 21, 2024

The Nuxt fail seems like a fluke since it's a dev fixture test, but I reran it again. I haven't seen downstream patching this in the past, so I figured it should be safe (but still, it's on them).

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 0418a94: Open

suite result latest scheduled
nuxt failure success

analogjs, astro, histoire, ladle, laravel, marko, previewjs, quasar, qwik, rakkas, remix, sveltekit, unocss, vike, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-pages, vite-plugin-react-swc, vite-plugin-svelte, vite-plugin-vue, vite-setup-catalogue, vitepress, vitest

@sapphi-red
Copy link
Member

I guess it's failing because the base commit was a bit old.

@sapphi-red
Copy link
Member

/ecosystem-ci run

@sapphi-red sapphi-red dismissed stale reviews from bluwy and themself via ecba0e8 March 29, 2024 07:34
@bluwy
Copy link
Member

bluwy commented Apr 5, 2024

Thanks for resolving it Sapphi, the fail had me defeated 😄 Since it passes on ecosystem-ci, I'll go ahead and merge this then.

@bluwy bluwy removed this from the 5.3 milestone Apr 5, 2024
@bluwy bluwy merged commit c0ec6be into vitejs:main Apr 5, 2024
10 checks passed
@panstromek
Copy link
Contributor Author

Thanks for carrying this through!

patak-dev pushed a commit that referenced this pull request Apr 5, 2024
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
Co-authored-by: 翠 / green <green@sapphi.red>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants