mirror of https://github.com/vuejs/core.git
perf: optimize scheduler queueJob performance (#138)
This commit is contained in:
parent
669fec8dad
commit
ff943f4ddf
|
@ -36,9 +36,11 @@ export enum BaseWatchErrorCodes {
|
||||||
// TODO move to a scheduler package
|
// TODO move to a scheduler package
|
||||||
export interface SchedulerJob extends Function {
|
export interface SchedulerJob extends Function {
|
||||||
id?: number
|
id?: number
|
||||||
|
// TODO refactor these boolean flags to a single bitwise flag
|
||||||
pre?: boolean
|
pre?: boolean
|
||||||
active?: boolean
|
active?: boolean
|
||||||
computed?: boolean
|
computed?: boolean
|
||||||
|
queued?: boolean
|
||||||
/**
|
/**
|
||||||
* Indicates whether the effect is allowed to recursively trigger itself
|
* Indicates whether the effect is allowed to recursively trigger itself
|
||||||
* when managed by the scheduler.
|
* when managed by the scheduler.
|
||||||
|
|
|
@ -28,32 +28,31 @@ const resolvedPromise = /*#__PURE__*/ Promise.resolve() as Promise<any>
|
||||||
let currentFlushPromise: Promise<void> | null = null
|
let currentFlushPromise: Promise<void> | null = null
|
||||||
|
|
||||||
function queueJob(job: SchedulerJob) {
|
function queueJob(job: SchedulerJob) {
|
||||||
if (
|
if (!job.queued) {
|
||||||
!queue.length ||
|
|
||||||
!queue.includes(
|
|
||||||
job,
|
|
||||||
isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
if (job.id == null) {
|
if (job.id == null) {
|
||||||
queue.push(job)
|
queue.push(job)
|
||||||
|
} else {
|
||||||
|
// fast path when the job id is larger than the tail
|
||||||
|
if (!job.pre && job.id >= (queue[queue.length - 1]?.id || 0)) {
|
||||||
|
queue.push(job)
|
||||||
} else {
|
} else {
|
||||||
queue.splice(findInsertionIndex(job.id), 0, job)
|
queue.splice(findInsertionIndex(job.id), 0, job)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (!job.allowRecurse) {
|
||||||
|
job.queued = true
|
||||||
|
}
|
||||||
queueFlush()
|
queueFlush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queuePostRenderEffect(cb: SchedulerJobs) {
|
export function queuePostRenderEffect(cb: SchedulerJobs) {
|
||||||
if (!isArray(cb)) {
|
if (!isArray(cb)) {
|
||||||
if (
|
if (!cb.queued) {
|
||||||
!activePostFlushCbs ||
|
|
||||||
!activePostFlushCbs.includes(
|
|
||||||
cb,
|
|
||||||
cb.allowRecurse ? postFlushIndex + 1 : postFlushIndex,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
pendingPostFlushCbs.push(cb)
|
pendingPostFlushCbs.push(cb)
|
||||||
|
if (!cb.allowRecurse) {
|
||||||
|
cb.queued = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// if cb is an array, it is a component lifecycle hook which can only be
|
// if cb is an array, it is a component lifecycle hook which can only be
|
||||||
|
@ -93,6 +92,7 @@ function flushPostFlushCbs() {
|
||||||
postFlushIndex++
|
postFlushIndex++
|
||||||
) {
|
) {
|
||||||
activePostFlushCbs[postFlushIndex]()
|
activePostFlushCbs[postFlushIndex]()
|
||||||
|
activePostFlushCbs[postFlushIndex].queued = false
|
||||||
}
|
}
|
||||||
activePostFlushCbs = null
|
activePostFlushCbs = null
|
||||||
postFlushIndex = 0
|
postFlushIndex = 0
|
||||||
|
@ -115,6 +115,7 @@ function flushJobs() {
|
||||||
try {
|
try {
|
||||||
for (let i = 0; i < queue!.length; i++) {
|
for (let i = 0; i < queue!.length; i++) {
|
||||||
queue![i]()
|
queue![i]()
|
||||||
|
queue![i].queued = false
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
flushIndex = 0
|
flushIndex = 0
|
||||||
|
|
Loading…
Reference in New Issue