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

fix(reactivity): optimising getDepFromReactive #12007

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

yangxiuxiu1115
Copy link
Contributor

ObjectRefImpl in toRef depends on getDepFromReactive

Copy link

github-actions bot commented Sep 23, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+1 B) 38 kB (-10 B) 34.2 kB (-44 B)
vue.global.prod.js 160 kB (+1 B) 58 kB (-9 B) 51.5 kB (+51 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 49 kB 18.9 kB 17.2 kB
createApp 55.6 kB 21.4 kB 19.6 kB
createSSRApp 59.6 kB 23.1 kB 21 kB
defineCustomElement 60.4 kB 22.9 kB 20.9 kB
overall 69.3 kB 26.5 kB 24 kB

Copy link

pkg-pr-new bot commented Sep 23, 2024

Open in Stackblitz

@vue/compiler-core

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

@vue/compiler-sfc

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 619c58e

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🧹 p1-chore Priority 1: this doesn't change code behavior. labels Sep 23, 2024
@skirtles-code
Copy link
Contributor

Avoiding ?. is intended to reduce the file size, but it looks like this has increased it by 3 bytes. I was curious about why...

It looks like the ?. version gets inlined into the get dep getter in ObjectRefImpl. Even though it expands to something fairly verbose, the inlining shaves off lots of bytes. Removing the ?. seems to prevent that inlining, leading to a larger build.

I did a bit of experimenting, and the best I could do just by rewriting getDepFromReactive was this:

export const getDepFromReactive = (
  object: any,
  key: string | number | symbol,
  depMap = targetMap.get(object),
): Dep | undefined => {
  return depMap && depMap.get(key)
}

It's a bit hacky, but it got it down from +3 to -8.

But this still doesn't benefit from the inlining seen with the original code.

So I tried just rewriting the get dep in ref.ts:

  // `import` changes not shown

  get dep(): Dep | undefined {
    const depMap = targetMap.get(toRaw(this._object))
    return depMap && depMap.get(this._key)
  }

This got it down to -26, with small improvements in the zipped versions too:

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (-26 B) 38 kB (-16 B) 34.2 kB (-43 B)
vue.global.prod.js 160 kB (-26 B) 58 kB (-15 B) 51.5 kB (+12 B)

The downside being direct access of targetMap in ref.ts.

I'll let others decide whether any of this matters... 😆

@yyx990803 yyx990803 merged commit c0e9434 into vuejs:main Sep 24, 2024
13 checks passed
@yangxiuxiu1115 yangxiuxiu1115 deleted the fix/ObjectRefImpl branch September 26, 2024 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧹 p1-chore Priority 1: this doesn't change code behavior. ready to merge The PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants