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: shrink instrumentations requires toRaw parameter #9051

Closed

Conversation

chenfan0
Copy link
Contributor

When using the array functions such as indexOf includes lastIndexOf to search, we only need to perform toRaw on the first parameter (searchElement), and do not need to perform toRaw on other parameters.

@vercel
Copy link

vercel bot commented Aug 26, 2023

@chenfan0 is attempting to deploy a commit to the vuejs Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Aug 26, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated (UTC)
sfc-playground ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2023 5:08am

@github-actions
Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 85.9 kB (+10 B) 32.7 kB (+8 B) 29.5 kB (+16 B)
vue.global.prod.js 132 kB (+10 B) 49.3 kB (+3 B) 44.3 kB (+22 B)

Usages

Name Size Gzip Brotli
createApp 47.9 kB (+10 B) 18.8 kB (+2 B) 17.2 kB (+3 B)
createSSRApp 50.7 kB (+10 B) 20 kB (+3 B) 18.2 kB (+3 B)
defineCustomElement 50.3 kB (+10 B) 19.6 kB (+2 B) 17.9 kB (+4 B)
overall 61.2 kB (+10 B) 23.7 kB (+3 B) 21.5 kB (-43 B)

@@ -66,7 +66,7 @@ function createArrayInstrumentations() {
const res = arr[key](...args)
if (res === -1 || res === false) {
// if that didn't work, run it again using raw values.
return arr[key](...args.map(toRaw))
return arr[key](toRaw(args[0]), ...args.slice(1))
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 this is necessary, because the parameters passed in by the user are uncontrollable. The parameters passed in by the user may all be responsive objects. Only use toRaw to process the first parameter. If the subsequent parameters are responsive objects, Incur access tracking overhead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is necessary, because the parameters passed in by the user are uncontrollable. The parameters passed in by the user may all be responsive objects. Only use toRaw to process the first parameter. If the subsequent parameters are responsive objects, Incur access tracking overhead

The purpose here is to use the original value to re-check whether it exists, so only the first parameter needs to be toRaw, and other parameters do not require additional processing. At the same time, the modification here will not cause additional overhead, because before executing this code, it will execute const res = arr[key](...args)

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be great if we could add or point out a test case to clarify it further.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be great if we could add or point out a test case to clarify it further.

// reactiveArray.spec.ts
test('Array identity methods should work with raw values', () => {
    const raw = {}
    const arr = reactive([{}, {}])
    arr.push(raw)
    expect(arr.indexOf(raw)).toBe(2)
    expect(arr.indexOf(raw, 3)).toBe(-1)

    // should work also for the observed version
    const observed = arr[2]
    expect(arr.indexOf(observed)).toBe(2)
    expect(arr.indexOf(observed, 3)).toBe(-1)
  })

if the code is

arr.indexOf(observed, 3, anotherObserved, anotherObserved, anotherObserved, anotherObserved, anotherObserved)

we just need to use toRaw for the first param, and we dont need to use toRaw for anotherObserved.

@Alfred-Skyblue
Copy link
Member

Alfred-Skyblue commented Sep 7, 2023

toRaw will return the original object only if the parameter is an object created by reactive or readonly, otherwise it will return the parameter itself, but the second parameter of the three methods indexOf, lastIndexOf and includes is value of type number. In theory, parameters of type number do not need to be called by toRaw

@baiwusanyu-c
Copy link
Member

toRaw will return the original object only if the parameter is an object created by reactive or readonly, otherwise it will return the parameter itself, but the second parameter of the three methods indexOf, lastIndexOf and includes is value of type number. In theory, parameters of type number do not need to be called by toRaw

You are right. The type passed in the second parameter of these methods is number. If reactive is passed in, then it is an unreasonable approach.

@edison1105
Copy link
Member

The code here has changed since #9511 was merged. Therefore, this PR is no longer needed.

args[0] = toRaw(args[0])

@edison1105 edison1105 closed this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Rejected
Development

Successfully merging this pull request may close these issues.

5 participants