Skip to content

Commit

Permalink
Refactor reactivity system to use version counting and doubly-linked …
Browse files Browse the repository at this point in the history
…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
yyx990803 authored Feb 25, 2024
1 parent 272ab9f commit 05eb4e0
Show file tree
Hide file tree
Showing 38 changed files with 1,629 additions and 1,122 deletions.
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

2 comments on commit 05eb4e0

@victorgarciaesgi
Copy link

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 noticed pauseScheduling and resetScheduling where deleted, I was using it on a lib of mine, what I can use instead? I saw nothing on the changelog

@victorgarciaesgi
Copy link

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

Please sign in to comment.