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

chore(reactivity): effectScope.ts variable declarations optimized and remove useless code in effect.ts #11721

Merged
merged 5 commits into from
Aug 28, 2024

Conversation

heggria
Copy link
Contributor

@heggria heggria commented Aug 27, 2024

This PR primarily modifies effectScope.ts, attempting to replace multiple let declarations within for loops with a single let declaration to enhance performance and maintain consistency with the stop function's style. Additionally, it removes redundant code from effect.ts.

@edison1105 edison1105 added the 🧹 p1-chore Priority 1: this doesn't change code behavior. label Aug 27, 2024
Copy link

github-actions bot commented Aug 27, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 99 kB (-25 B) 37.5 kB (+1 B) 33.8 kB (+45 B)
vue.global.prod.js 157 kB (-25 B) 57.3 kB (+2 B) 51 kB (+47 B)

Usages

Name Size Gzip Brotli
createApp 54.5 kB (-25 B) 21.1 kB (-1 B) 19.3 kB (-1 B)
createSSRApp 58.4 kB (-25 B) 22.8 kB (-3 B) 20.7 kB (-14 B)
defineCustomElement 59.1 kB (-25 B) 22.6 kB (-1 B) 20.6 kB (-5 B)
overall 68.1 kB (-25 B) 26.2 kB (-3 B) 23.8 kB (-47 B)

Copy link

pkg-pr-new bot commented Aug 27, 2024

commit: 5fd1a69

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

Open in Stackblitz

@heggria
Copy link
Contributor Author

heggria commented Aug 27, 2024

The decrease in batchDepth can be written in two ways. While the second method (--x) is simpler in terms of code, it doesn't quite match the coding style used elsewhere (where x-- is predominantly used). I'm not sure if it's worth making this change. I really appreciate the suggestion!

// type 1
batchDepth--
if (batchDepth > 0) {
  return
}

// type 2
if (--batchDepth > 0) {
  return
}

@edison1105
Copy link
Member

edison1105 commented Aug 28, 2024

@heggria
This change is incorrect. When batchDepth is 1, both type 1 and type 2 will early return. It's not consistent with the following logic.

  if (batchDepth > 1) {
    batchDepth--
    return
  }

  batchDepth--
  //...

@heggria
Copy link
Contributor Author

heggria commented Aug 28, 2024

@heggria This change is incorrect. When batchDepth is 1, both type 1 and type 2 will early return. It's not consistent with the following logic.

  if (batchDepth > 1) {
    batchDepth--
    return
  }

  batchDepth--
  //...

@edison1105 Thank you so much for your comment! However, after carefully examining and running the code, I reached the conclusion that the results are the same.

  • In the original code, the operation batchDepth - 1 is executed regardless of which logic branch is taken. Here are the different scenarios:

    • When batchDepth === 2, batchDepth is greater than 1, so it becomes 1 and the function returns early.

    • When batchDepth === 1, batchDepth is not greater than 1, so it becomes 0 and the function continues.

    • When batchDepth === 0, batchDepth is not greater than 1, so it becomes -1 and the function continues.

  • In both type 1 and type 2, the operation batchDepth - 1 is executed before any logic branch is taken. Here are the scenarios:

    • When batchDepth === 2, batchDepth becomes 1, which is greater than 0, and the function returns early.

    • When batchDepth === 1, batchDepth becomes 0, which is not greater than 0, and the function continues.

    • When batchDepth === 0, batchDepth becomes -1, which is not greater than 0, and the function continues.

Therefore, aside from the difference in timing between when batchDepth changes and when the function returns, I believe these two code implementations are equivalent. Since this is a synchronous function, there shouldn’t be any additional side effects.

Once again, thank you for taking the time to review my PR!

@edison1105
Copy link
Member

edison1105 commented Aug 28, 2024

uh... sorry, I think I got my thoughts mixed up just now. 😓
I prefer type 2 by the way.

@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label Aug 28, 2024
@heggria
Copy link
Contributor Author

heggria commented Aug 28, 2024

uh... sorry, I think I got my thoughts mixed up just now. 😓 I prefer type 2 by the way.

No worries, it happens to all of us. 😊 The code is now using type 2. Thanks for your suggestion!

@yyx990803 yyx990803 merged commit 64e1ca2 into vuejs:main Aug 28, 2024
13 checks passed
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.

3 participants