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

refactor(reactivity): reduce size of collectionHandlers #12152

Merged
merged 9 commits into from
Oct 11, 2024

Conversation

skirtles-code
Copy link
Contributor

@skirtles-code skirtles-code commented Oct 11, 2024

The diff looks horrendous... the change really isn't that complicated.

Bundle size

My primary goal was to reduce the bundle size. I've removed a lot of redundancy from collectionHandlers.ts. That involved moving around a lot of code, but it essentially all still works the same way.

To make this a bit easier to review I've split it up into individual commits. The first commit is the key change to understand what I've done. createInstrumentations was previously creating 4 lots of instrumentations and then returning them. The new version just creates the instrumentations for a single case, based on the arguments readonly and shallow. Just that one change reduces the bundle size by about 566 bytes, or roughly 135 bytes after gzipping.

The other commits gradually inline each of the functions. I checked after each commit to ensure that they really did reduce the bundle size further.

The final result is about 220 bytes saved after gzipping. In the Usages size checks that's a saving of about 1%.

Fixes minor bugs

A key part of the new approach is that readonly and shallow are arguments passed to createInstrumentations, which are then caught in the closure of the specific handlers. Previously these argument were passed directly to the handlers.

The old approach could lead to minor bugs in edge cases, due to the ghost parameters:

The reason for the inconsistency in this example is that the add method takes _isShallow as its second argument. This is only supposed to be passed internally, but it also gets passed automatically by forEach in the example above.

With the refactoring in this PR, those ghost parameters are removed, so these kinds of inconsistencies should no longer occur:

Performance

I don't think this PR should have a significant impact on performance. Inlining the handlers may have slightly improved performance in some cases, as it removes an extra function call.

For example, this Playground gives slightly quicker times for me when testing in Chrome:

Impact on other PRs

Such a large amount of churn, albeit within a single file, is a cause for concern. I've checked how this would impact existing PRs.

These PRs would conflict with this PR:

These PRs make changes to collectionHandler.ts, but I don't think this PR would cause any conflicts:

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (-807 B) 37.9 kB (-210 B) 34.1 kB (-152 B)
vue.global.prod.js 159 kB (-807 B) 57.9 kB (-204 B) 51.5 kB (-156 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.4 kB (-807 B) 18.8 kB (-235 B) 17.1 kB (-194 B)
createApp 55 kB (-807 B) 21.3 kB (-215 B) 19.4 kB (-177 B)
createSSRApp 59 kB (-807 B) 23 kB (-234 B) 20.9 kB (-187 B)
defineCustomElement 59.8 kB (-807 B) 22.8 kB (-216 B) 20.8 kB (-172 B)
overall 68.7 kB (-807 B) 26.3 kB (-222 B) 24 kB (-199 B)

Copy link

pkg-pr-new bot commented Oct 11, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@12152

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@12152

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@12152

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@12152

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@12152

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@12152

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@12152

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@12152

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@12152

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@12152

vue

pnpm add https://pkg.pr.new/vue@12152

commit: f08f58e

@Justineo
Copy link
Member

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Oct 11, 2024

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools failure failure
nuxt success success
pinia success success
primevue success success
quasar success success
radix-vue success success
router success success
test-utils success success
vant success success
vite-plugin-vue success success
vitepress success success
vue-i18n success success
vue-macros success success
vuetify success success
vueuse success success
vue-simple-compiler success success

@yyx990803 yyx990803 merged commit c82b662 into vuejs:main Oct 11, 2024
13 checks passed
@caendesilva
Copy link

Flawless PR description! Great job Skirtles!

@zhangenming
Copy link
Contributor

"I've checked how this would impact existing PRs."

What method did you use to accomplish this?

@skirtles-code
Copy link
Contributor Author

"I've checked how this would impact existing PRs."

What method did you use to accomplish this?

I have some scripts at https://github.com/skirtles-code/github-analysis-scripts that I used to download the open PRs. Once I had the changes locally I just searched for changes to collectionHandlers.ts.

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.

6 participants