Skip to content

Commit 7fbf496

Browse files
refactor(scheduler): remove redundant sorting (#11646)
1 parent b332f80 commit 7fbf496

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

packages/runtime-core/src/scheduler.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,17 @@ export function nextTick<T = void, R = void>(
6464
return fn ? p.then(this ? fn.bind(this) : fn) : p
6565
}
6666

67-
// #2768
68-
// Use binary-search to find a suitable position in the queue,
69-
// so that the queue maintains the increasing order of job's id,
70-
// which can prevent the job from being skipped and also can avoid repeated patching.
67+
// Use binary-search to find a suitable position in the queue. The queue needs
68+
// to be sorted in increasing order of the job ids. This ensures that:
69+
// 1. Components are updated from parent to child. As the parent is always
70+
// created before the child it will always have a smaller id.
71+
// 2. If a component is unmounted during a parent component's update, its update
72+
// can be skipped.
73+
// A pre watcher will have the same id as its component's update job. The
74+
// watcher should be inserted immediately before the update job. This allows
75+
// watchers to be skipped if the component is unmounted by the parent update.
7176
function findInsertionIndex(id: number) {
72-
// the start index should be `flushIndex + 1`
73-
let start = flushIndex + 1
77+
let start = isFlushing ? flushIndex + 1 : 0
7478
let end = queue.length
7579

7680
while (start < end) {
@@ -208,33 +212,13 @@ export function flushPostFlushCbs(seen?: CountMap): void {
208212
const getId = (job: SchedulerJob): number =>
209213
job.id == null ? (job.flags! & SchedulerJobFlags.PRE ? -1 : Infinity) : job.id
210214

211-
const comparator = (a: SchedulerJob, b: SchedulerJob): number => {
212-
const diff = getId(a) - getId(b)
213-
if (diff === 0) {
214-
const isAPre = a.flags! & SchedulerJobFlags.PRE
215-
const isBPre = b.flags! & SchedulerJobFlags.PRE
216-
if (isAPre && !isBPre) return -1
217-
if (isBPre && !isAPre) return 1
218-
}
219-
return diff
220-
}
221-
222215
function flushJobs(seen?: CountMap) {
223216
isFlushPending = false
224217
isFlushing = true
225218
if (__DEV__) {
226219
seen = seen || new Map()
227220
}
228221

229-
// Sort queue before flush.
230-
// This ensures that:
231-
// 1. Components are updated from parent to child. (because parent is always
232-
// created before the child so its render effect will have smaller
233-
// priority number)
234-
// 2. If a component is unmounted during a parent component's update,
235-
// its update can be skipped.
236-
queue.sort(comparator)
237-
238222
// conditional usage of checkRecursiveUpdate must be determined out of
239223
// try ... catch block since Rollup by default de-optimizes treeshaking
240224
// inside try-catch. This can leave all warning code unshaked. Although

0 commit comments

Comments
 (0)