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 system to use version counting and doubly-linked list tracking #10397

Merged
merged 43 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
2e5dd06
wip: save
yyx990803 Feb 21, 2024
f9579b0
wip: save
yyx990803 Feb 21, 2024
bb07894
wip: save
yyx990803 Feb 21, 2024
e5b303e
wip: save
yyx990803 Feb 21, 2024
3206d0a
wip: remove old files
yyx990803 Feb 21, 2024
75f66fc
wip: passing some computed tests
yyx990803 Feb 21, 2024
cb5b6f4
wip: avoid recursive side effects in computed
yyx990803 Feb 22, 2024
a6b1704
wip: pass computed tests
yyx990803 Feb 22, 2024
3b354f7
wip: trigger debug info for refs
yyx990803 Feb 22, 2024
a42812d
wip: computed ssr behavior
yyx990803 Feb 22, 2024
ac829f1
wip: remove outdated lines
yyx990803 Feb 22, 2024
4a5bf26
wip: move reactivity benchmarks
yyx990803 Feb 22, 2024
73bca37
wip: tests except effect passing
yyx990803 Feb 22, 2024
132f386
wip: all effect tests passing
yyx990803 Feb 22, 2024
82d93c7
wip: remove unused
yyx990803 Feb 22, 2024
0c26b41
wip: all reactivity tests passing
yyx990803 Feb 22, 2024
bea3a7a
wip: improve benchmarks
yyx990803 Feb 22, 2024
51da8c9
wip: scope handling
yyx990803 Feb 22, 2024
7676d4e
wip: make tests pass without property dep cleanup
yyx990803 Feb 23, 2024
c74d191
wip: pass all watcher tests
yyx990803 Feb 23, 2024
36773c6
wip: more tests passing
yyx990803 Feb 23, 2024
8136bb1
wip: fix computed tracking in untracked zone
yyx990803 Feb 23, 2024
1318017
wip: avoid toRaw in ref value getters
yyx990803 Feb 23, 2024
3f092dc
wip: track depsTail for stable traversal
yyx990803 Feb 23, 2024
5c6626e
wip: tweaks
yyx990803 Feb 23, 2024
decca5e
wip: fix dts tests
yyx990803 Feb 23, 2024
5adb08c
wip: computed effect backwards compat
yyx990803 Feb 23, 2024
9a79838
wip: more type alignment
yyx990803 Feb 23, 2024
88c794f
wip: allow computed side effect for backwards compat
yyx990803 Feb 23, 2024
07cbb8a
wip: avoid custom formatter affecting behavior when debugging
yyx990803 Feb 23, 2024
81c9861
wip: fix vueuse cases
yyx990803 Feb 23, 2024
72e2d3e
wip: pinia compat
yyx990803 Feb 23, 2024
7a9d552
wip: fix chained computed sub
yyx990803 Feb 23, 2024
87e7306
wip: add test case for #10236
yyx990803 Feb 23, 2024
d69a797
wip: move recursion logic into computed.notify
yyx990803 Feb 24, 2024
bad9b1d
wip: ensure computed deps are only evaluated when effect is run
yyx990803 Feb 24, 2024
07b6fd3
wip: add test case for computed dep mutation during ssr
yyx990803 Feb 24, 2024
4fd6026
chore: adjust benchmarks
yyx990803 Feb 24, 2024
40f8d08
chore: use hasChanged helper
yyx990803 Feb 24, 2024
b297ad4
chore: add internal annoatations
yyx990803 Feb 24, 2024
4ab62d9
chore: fix hasChanged condition
yyx990803 Feb 24, 2024
8172434
chore: remove no longer needed scheduler flag
yyx990803 Feb 25, 2024
f6d949b
chore: provide oldValue in ref trigger debugger event
yyx990803 Feb 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions packages/reactivity/__benchmarks__/computed.bench.ts
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
})
}
})
111 changes: 111 additions & 0 deletions packages/reactivity/__benchmarks__/effect.bench.ts
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)
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { computed, reactive, readonly, shallowRef, triggerRef } from '../src'

for (let amount = 1e1; amount < 1e4; amount *= 10) {
{
const rawArray = []
const rawArray: any[] = []
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
Expand All @@ -21,7 +21,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
}

{
const rawArray = []
const rawArray: any[] = []
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
Expand All @@ -40,7 +40,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
}

{
const rawArray = []
const rawArray: any[] = []
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
Expand All @@ -56,7 +56,7 @@ for (let amount = 1e1; amount < 1e4; amount *= 10) {
}

{
const rawArray = []
const rawArray: any[] = []
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bench('create reactive map', () => {

{
const r = reactive(createMap({ a: 1 }))
const computeds = []
const computeds: any[] = []
for (let i = 0, n = 1000; i < n; i++) {
const c = computed(() => {
return r.get('a') * 2
Expand All @@ -94,7 +94,7 @@ bench('create reactive map', () => {

{
const r = reactive(createMap({ a: 1 }))
const computeds = []
const computeds: any[] = []
for (let i = 0, n = 1000; i < n; i++) {
const c = computed(() => {
return r.get('a') * 2
Expand Down
21 changes: 21 additions & 0 deletions packages/reactivity/__benchmarks__/reactiveObject.bench.ts
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++
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('ref', () => {
const v = ref(100)
bench('write/read ref', () => {
v.value = i++

v.value
})
}
Expand Down
Loading
Loading