-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor reactivity system to use version counting and doubly-linked …
…list tracking (#10397) Bug fixes close #10236 close #10069 PRs made stale by this one close #10290 close #10354 close #10189 close #9480
- Loading branch information
Showing
38 changed files
with
1,629 additions
and
1,122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { bench, describe } from 'vitest' | ||
import { type ComputedRef, type Ref, computed, effect, ref } from '../src' | ||
|
||
describe('computed', () => { | ||
bench('create computed', () => { | ||
computed(() => 100) | ||
}) | ||
|
||
{ | ||
const v = ref(100) | ||
computed(() => v.value * 2) | ||
let i = 0 | ||
bench("write ref, don't read computed (without effect)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
effect(() => c.value) | ||
let i = 0 | ||
bench("write ref, don't read computed (with effect)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
let i = 0 | ||
bench('write ref, read computed (without effect)', () => { | ||
v.value = i++ | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
effect(() => c.value) | ||
let i = 0 | ||
bench('write ref, read computed (with effect)', () => { | ||
v.value = i++ | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench("write ref, don't read 1000 computeds (without effect)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
effect(() => c.value) | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench( | ||
"write ref, don't read 1000 computeds (with multiple effects)", | ||
() => { | ||
v.value = i++ | ||
}, | ||
) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
computeds.push(c) | ||
} | ||
effect(() => { | ||
for (let i = 0; i < 1000; i++) { | ||
computeds[i].value | ||
} | ||
}) | ||
let i = 0 | ||
bench("write ref, don't read 1000 computeds (with single effect)", () => { | ||
v.value = i++ | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench('write ref, read 1000 computeds (no effect)', () => { | ||
v.value = i++ | ||
computeds.forEach(c => c.value) | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
effect(() => c.value) | ||
computeds.push(c) | ||
} | ||
let i = 0 | ||
bench('write ref, read 1000 computeds (with multiple effects)', () => { | ||
v.value = i++ | ||
computeds.forEach(c => c.value) | ||
}) | ||
} | ||
|
||
{ | ||
const v = ref(100) | ||
const computeds: ComputedRef<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
const c = computed(() => { | ||
return v.value * 2 | ||
}) | ||
effect(() => c.value) | ||
computeds.push(c) | ||
} | ||
effect(() => { | ||
for (let i = 0; i < 1000; i++) { | ||
computeds[i].value | ||
} | ||
}) | ||
let i = 0 | ||
bench('write ref, read 1000 computeds (with single effect)', () => { | ||
v.value = i++ | ||
computeds.forEach(c => c.value) | ||
}) | ||
} | ||
|
||
{ | ||
const refs: Ref<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
refs.push(ref(i)) | ||
} | ||
const c = computed(() => { | ||
let total = 0 | ||
refs.forEach(ref => (total += ref.value)) | ||
return total | ||
}) | ||
let i = 0 | ||
const n = refs.length | ||
bench('1000 refs, read 1 computed (without effect)', () => { | ||
refs[i++ % n].value++ | ||
c.value | ||
}) | ||
} | ||
|
||
{ | ||
const refs: Ref<number>[] = [] | ||
for (let i = 0, n = 1000; i < n; i++) { | ||
refs.push(ref(i)) | ||
} | ||
const c = computed(() => { | ||
let total = 0 | ||
refs.forEach(ref => (total += ref.value)) | ||
return total | ||
}) | ||
effect(() => c.value) | ||
let i = 0 | ||
const n = refs.length | ||
bench('1000 refs, read 1 computed (with effect)', () => { | ||
refs[i++ % n].value++ | ||
c.value | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { bench, describe } from 'vitest' | ||
import { type Ref, effect, ref } from '../src' | ||
|
||
describe('effect', () => { | ||
{ | ||
let i = 0 | ||
const n = ref(0) | ||
effect(() => n.value) | ||
bench('single ref invoke', () => { | ||
n.value = i++ | ||
}) | ||
} | ||
|
||
function benchEffectCreate(size: number) { | ||
bench(`create an effect that tracks ${size} refs`, () => { | ||
const refs: Ref[] = [] | ||
for (let i = 0; i < size; i++) { | ||
refs.push(ref(i)) | ||
} | ||
effect(() => { | ||
for (let i = 0; i < size; i++) { | ||
refs[i].value | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
benchEffectCreate(1) | ||
benchEffectCreate(10) | ||
benchEffectCreate(100) | ||
benchEffectCreate(1000) | ||
|
||
function benchEffectCreateAndStop(size: number) { | ||
bench(`create and stop an effect that tracks ${size} refs`, () => { | ||
const refs: Ref[] = [] | ||
for (let i = 0; i < size; i++) { | ||
refs.push(ref(i)) | ||
} | ||
const e = effect(() => { | ||
for (let i = 0; i < size; i++) { | ||
refs[i].value | ||
} | ||
}) | ||
e.effect.stop() | ||
}) | ||
} | ||
|
||
benchEffectCreateAndStop(1) | ||
benchEffectCreateAndStop(10) | ||
benchEffectCreateAndStop(100) | ||
benchEffectCreateAndStop(1000) | ||
|
||
function benchWithRefs(size: number) { | ||
let j = 0 | ||
const refs: Ref[] = [] | ||
for (let i = 0; i < size; i++) { | ||
refs.push(ref(i)) | ||
} | ||
effect(() => { | ||
for (let i = 0; i < size; i++) { | ||
refs[i].value | ||
} | ||
}) | ||
bench(`1 effect, mutate ${size} refs`, () => { | ||
for (let i = 0; i < size; i++) { | ||
refs[i].value = i + j++ | ||
} | ||
}) | ||
} | ||
|
||
benchWithRefs(10) | ||
benchWithRefs(100) | ||
benchWithRefs(1000) | ||
|
||
function benchWithBranches(size: number) { | ||
const toggle = ref(true) | ||
const refs: Ref[] = [] | ||
for (let i = 0; i < size; i++) { | ||
refs.push(ref(i)) | ||
} | ||
effect(() => { | ||
if (toggle.value) { | ||
for (let i = 0; i < size; i++) { | ||
refs[i].value | ||
} | ||
} | ||
}) | ||
bench(`${size} refs branch toggle`, () => { | ||
toggle.value = !toggle.value | ||
}) | ||
} | ||
|
||
benchWithBranches(10) | ||
benchWithBranches(100) | ||
benchWithBranches(1000) | ||
|
||
function benchMultipleEffects(size: number) { | ||
let i = 0 | ||
const n = ref(0) | ||
for (let i = 0; i < size; i++) { | ||
effect(() => n.value) | ||
} | ||
bench(`1 ref invoking ${size} effects`, () => { | ||
n.value = i++ | ||
}) | ||
} | ||
|
||
benchMultipleEffects(10) | ||
benchMultipleEffects(100) | ||
benchMultipleEffects(1000) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/reactivity/__benchmarks__/reactiveObject.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { bench } from 'vitest' | ||
import { reactive } from '../src' | ||
|
||
bench('create reactive obj', () => { | ||
reactive({ a: 1 }) | ||
}) | ||
|
||
{ | ||
const r = reactive({ a: 1 }) | ||
bench('read reactive obj property', () => { | ||
r.a | ||
}) | ||
} | ||
|
||
{ | ||
let i = 0 | ||
const r = reactive({ a: 1 }) | ||
bench('write reactive obj property', () => { | ||
r.a = i++ | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
05eb4e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @yyx990803 , just updated to
3.5
and noticedpauseScheduling
andresetScheduling
where deleted, I was using it on a lib of mine, what I can use instead? I saw nothing on the changelog05eb4e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the calls, all my tests passes so nevermind, should be good