Skip to content

refactor(scheduler): use bitwise flags for scheduler jobs + optimize queueJob #10407

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

Merged
merged 46 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 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
5c1961f
refactor: use bitwise flags for scheduler jobs
yyx990803 Feb 25, 2024
87fdf1f
refactor: improve queueJob check performance by using queued state
yyx990803 Feb 25, 2024
7ba8c76
perf(scheduler): queueJob fast path when id is larger than tail
yyx990803 Feb 25, 2024
9ea9e82
chore: Merge branch 'minor' into scheduler-refactor
yyx990803 Feb 26, 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
9 changes: 4 additions & 5 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ export class ReactiveEffect<T = any>
* @internal
*/
nextEffect?: ReactiveEffect = undefined
/**
* @internal
*/
allowRecurse?: boolean

scheduler?: EffectScheduler = undefined
onStop?: () => void
Expand All @@ -144,7 +140,10 @@ export class ReactiveEffect<T = any>
* @internal
*/
notify() {
if (this.flags & EffectFlags.RUNNING && !this.allowRecurse) {
if (
this.flags & EffectFlags.RUNNING &&
!(this.flags & EffectFlags.ALLOW_RECURSE)
) {
return
}
if (this.flags & EffectFlags.NO_BATCH) {
Expand Down
66 changes: 34 additions & 32 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
type SchedulerJob,
SchedulerJobFlags,
flushPostFlushCbs,
flushPreFlushCbs,
invalidateJob,
Expand Down Expand Up @@ -119,12 +121,12 @@ describe('scheduler', () => {
const job1 = () => {
calls.push('job1')
}
const cb1 = () => {
const cb1: SchedulerJob = () => {
// queueJob in postFlushCb
calls.push('cb1')
queueJob(job1)
}
cb1.pre = true
cb1.flags! |= SchedulerJobFlags.PRE

queueJob(cb1)
await nextTick()
Expand All @@ -138,25 +140,25 @@ describe('scheduler', () => {
}
job1.id = 1

const cb1 = () => {
const cb1: SchedulerJob = () => {
calls.push('cb1')
queueJob(job1)
// cb2 should execute before the job
queueJob(cb2)
queueJob(cb3)
}
cb1.pre = true
cb1.flags! |= SchedulerJobFlags.PRE

const cb2 = () => {
const cb2: SchedulerJob = () => {
calls.push('cb2')
}
cb2.pre = true
cb2.flags! |= SchedulerJobFlags.PRE
cb2.id = 1

const cb3 = () => {
const cb3: SchedulerJob = () => {
calls.push('cb3')
}
cb3.pre = true
cb3.flags! |= SchedulerJobFlags.PRE
cb3.id = 1

queueJob(cb1)
Expand All @@ -166,37 +168,37 @@ describe('scheduler', () => {

it('should insert jobs after pre jobs with the same id', async () => {
const calls: string[] = []
const job1 = () => {
const job1: SchedulerJob = () => {
calls.push('job1')
}
job1.id = 1
job1.pre = true
const job2 = () => {
job1.flags! |= SchedulerJobFlags.PRE
const job2: SchedulerJob = () => {
calls.push('job2')
queueJob(job5)
queueJob(job6)
}
job2.id = 2
job2.pre = true
const job3 = () => {
job2.flags! |= SchedulerJobFlags.PRE
const job3: SchedulerJob = () => {
calls.push('job3')
}
job3.id = 2
job3.pre = true
const job4 = () => {
job3.flags! |= SchedulerJobFlags.PRE
const job4: SchedulerJob = () => {
calls.push('job4')
}
job4.id = 3
job4.pre = true
const job5 = () => {
job4.flags! |= SchedulerJobFlags.PRE
const job5: SchedulerJob = () => {
calls.push('job5')
}
job5.id = 2
const job6 = () => {
const job6: SchedulerJob = () => {
calls.push('job6')
}
job6.id = 2
job6.pre = true
job6.flags! |= SchedulerJobFlags.PRE

// We need several jobs to test this properly, otherwise
// findInsertionIndex can yield the correct index by chance
Expand All @@ -221,16 +223,16 @@ describe('scheduler', () => {
flushPreFlushCbs()
calls.push('job1')
}
const cb1 = () => {
const cb1: SchedulerJob = () => {
calls.push('cb1')
// a cb triggers its parent job, which should be skipped
queueJob(job1)
}
cb1.pre = true
const cb2 = () => {
cb1.flags! |= SchedulerJobFlags.PRE
const cb2: SchedulerJob = () => {
calls.push('cb2')
}
cb2.pre = true
cb2.flags! |= SchedulerJobFlags.PRE

queueJob(job1)
await nextTick()
Expand All @@ -240,8 +242,8 @@ describe('scheduler', () => {
// #3806
it('queue preFlushCb inside postFlushCb', async () => {
const spy = vi.fn()
const cb = () => spy()
cb.pre = true
const cb: SchedulerJob = () => spy()
cb.flags! |= SchedulerJobFlags.PRE
queuePostFlushCb(() => {
queueJob(cb)
})
Expand Down Expand Up @@ -521,25 +523,25 @@ describe('scheduler', () => {
test('should allow explicitly marked jobs to trigger itself', async () => {
// normal job
let count = 0
const job = () => {
const job: SchedulerJob = () => {
if (count < 3) {
count++
queueJob(job)
}
}
job.allowRecurse = true
job.flags! |= SchedulerJobFlags.ALLOW_RECURSE
queueJob(job)
await nextTick()
expect(count).toBe(3)

// post cb
const cb = () => {
const cb: SchedulerJob = () => {
if (count < 5) {
count++
queuePostFlushCb(cb)
}
}
cb.allowRecurse = true
cb.flags! |= SchedulerJobFlags.ALLOW_RECURSE
queuePostFlushCb(cb)
await nextTick()
expect(count).toBe(5)
Expand Down Expand Up @@ -572,7 +574,7 @@ describe('scheduler', () => {
// simulate parent component that toggles child
const job1 = () => {
// @ts-expect-error
job2.active = false
job2.flags! |= SchedulerJobFlags.DISPOSED
}
// simulate child that's triggered by the same reactive change that
// triggers its toggle
Expand All @@ -589,11 +591,11 @@ describe('scheduler', () => {

it('flushPreFlushCbs inside a pre job', async () => {
const spy = vi.fn()
const job = () => {
const job: SchedulerJob = () => {
spy()
flushPreFlushCbs()
}
job.pre = true
job.flags! |= SchedulerJobFlags.PRE
queueJob(job)
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isRef,
isShallow,
} from '@vue/reactivity'
import { type SchedulerJob, queueJob } from './scheduler'
import { type SchedulerJob, SchedulerJobFlags, queueJob } from './scheduler'
import {
EMPTY_OBJ,
NOOP,
Expand Down Expand Up @@ -382,7 +382,7 @@ function doWatch(

// important: mark the job as a watcher callback so that scheduler knows
// it is allowed to self-trigger (#1727)
job.allowRecurse = !!cb
if (cb) job.flags! |= SchedulerJobFlags.ALLOW_RECURSE

const effect = new ReactiveEffect(getter)

Expand All @@ -394,7 +394,7 @@ function doWatch(
scheduler = () => queuePostRenderEffect(job, instance && instance.suspense)
} else {
// default: 'pre'
job.pre = true
job.flags! |= SchedulerJobFlags.PRE
if (instance) job.id = instance.uid
scheduler = () => queueJob(job)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/components/BaseTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ErrorCodes, callWithAsyncErrorHandling } from '../errorHandling'
import { PatchFlags, ShapeFlags, isArray } from '@vue/shared'
import { onBeforeUnmount, onMounted } from '../apiLifecycle'
import type { RendererElement } from '../renderer'
import { SchedulerJobFlags } from '../scheduler'

type Hook<T = () => void> = T | T[]

Expand Down Expand Up @@ -231,7 +232,7 @@ const BaseTransitionImpl: ComponentOptions = {
state.isLeaving = false
// #6835
// it also needs to be updated when active is undefined
if (instance.job.active !== false) {
if (!(instance.job.flags! & SchedulerJobFlags.DISPOSED)) {
instance.update()
}
}
Expand Down
18 changes: 15 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ import {
} from '@vue/shared'
import {
type SchedulerJob,
SchedulerJobFlags,
flushPostFlushCbs,
flushPreFlushCbs,
invalidateJob,
queueJob,
queuePostFlushCb,
} from './scheduler'
import { ReactiveEffect, pauseTracking, resetTracking } from '@vue/reactivity'
import {
EffectFlags,
ReactiveEffect,
pauseTracking,
resetTracking,
} from '@vue/reactivity'
import { updateProps } from './componentProps'
import { updateSlots } from './componentSlots'
import { popWarningContext, pushWarningContext, warn } from './warning'
Expand Down Expand Up @@ -2281,7 +2287,7 @@ function baseCreateRenderer(
// setup has resolved.
if (job) {
// so that scheduler will no longer invoke it
job.active = false
job.flags! |= SchedulerJobFlags.DISPOSED
unmount(subTree, instance, parentSuspense, doRemove)
}
// unmounted hook
Expand Down Expand Up @@ -2419,7 +2425,13 @@ function toggleRecurse(
{ effect, job }: ComponentInternalInstance,
allowed: boolean,
) {
effect.allowRecurse = job.allowRecurse = allowed
if (allowed) {
effect.flags |= EffectFlags.ALLOW_RECURSE
job.flags! |= SchedulerJobFlags.ALLOW_RECURSE
} else {
effect.flags &= ~EffectFlags.ALLOW_RECURSE
job.flags! &= ~SchedulerJobFlags.ALLOW_RECURSE
}
}

export function needTransition(
Expand Down
Loading