Skip to content

Commit 0da6e3f

Browse files
committed
perf(reactivity): add fast path for notify a single effect
1 parent 532ed51 commit 0da6e3f

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

packages/reactivity/src/computed.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
checkDirty,
1818
endTracking,
1919
link,
20-
processComputedUpdate,
20+
shallowPropagate,
2121
startTracking,
2222
} from './system'
2323
import { warn } from './warning'
@@ -138,8 +138,18 @@ export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
138138

139139
get value(): T {
140140
const flags = this.flags
141-
if (flags & (SubscriberFlags.Dirty | SubscriberFlags.Pending)) {
142-
processComputedUpdate(this, flags)
141+
if (
142+
flags & SubscriberFlags.Dirty ||
143+
(flags & SubscriberFlags.Pending && checkDirty(this.deps!))
144+
) {
145+
if (this.update()) {
146+
const subs = this.subs
147+
if (subs !== undefined) {
148+
shallowPropagate(subs)
149+
}
150+
}
151+
} else if (flags & SubscriberFlags.Pending) {
152+
this.flags = flags & ~SubscriberFlags.Pending
143153
}
144154
if (activeSub !== undefined) {
145155
if (__DEV__) {

packages/reactivity/src/system.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function startBatch(): void {
4747
}
4848

4949
export function endBatch(): void {
50-
if (!--batchDepth) {
50+
if (!--batchDepth && notifyBufferLength) {
5151
processEffectNotifications()
5252
}
5353
}
@@ -180,7 +180,11 @@ export function propagate(current: Link): void {
180180

181181
if (shouldNotify) {
182182
if ('notify' in sub) {
183-
notifyBuffer[notifyBufferLength++] = sub
183+
if (!batchDepth && !notifyBufferLength && next === undefined) {
184+
sub.notify()
185+
} else {
186+
notifyBuffer[notifyBufferLength++] = sub
187+
}
184188
} else {
185189
const subSubs = (sub as Dependency).subs
186190
if (subSubs !== undefined) {
@@ -225,7 +229,7 @@ export function propagate(current: Link): void {
225229
break
226230
} while (true)
227231

228-
if (!batchDepth) {
232+
if (!batchDepth && notifyBufferLength) {
229233
processEffectNotifications()
230234
}
231235
}
@@ -251,22 +255,6 @@ export function endTracking(sub: Subscriber): void {
251255
sub.flags &= ~SubscriberFlags.Tracking
252256
}
253257

254-
export function processComputedUpdate(
255-
computed: Computed,
256-
flags: SubscriberFlags,
257-
): void {
258-
if (flags & SubscriberFlags.Dirty || checkDirty(computed.deps!)) {
259-
if (computed.update()) {
260-
const subs = computed.subs
261-
if (subs !== undefined) {
262-
shallowPropagate(subs)
263-
}
264-
}
265-
} else {
266-
computed.flags = flags & ~SubscriberFlags.Pending
267-
}
268-
}
269-
270258
export function processEffectNotifications(): void {
271259
while (notifyIndex < notifyBufferLength) {
272260
const effect = notifyBuffer[notifyIndex]!
@@ -348,7 +336,7 @@ export function checkDirty(current: Link): boolean {
348336
} while (true)
349337
}
350338

351-
function shallowPropagate(link: Link): void {
339+
export function shallowPropagate(link: Link): void {
352340
do {
353341
const sub = link.sub
354342
const subFlags = sub.flags

0 commit comments

Comments
 (0)