Skip to content

Commit 03566c6

Browse files
committed
perf: use setActiveSub instead of pauseTracking to avoid array usage
1 parent 0af4b64 commit 03566c6

File tree

16 files changed

+52
-67
lines changed

16 files changed

+52
-67
lines changed

packages/reactivity/src/arrayInstrumentations.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { isArray } from '@vue/shared'
22
import { TrackOpTypes } from './constants'
33
import { ARRAY_ITERATE_KEY, track } from './dep'
4-
import { pauseTracking, resetTracking } from './effect'
54
import { isProxy, isShallow, toRaw, toReactive } from './reactive'
6-
import { endBatch, startBatch } from './system'
5+
import { endBatch, setActiveSub, startBatch } from './system'
76

87
/**
98
* Track array iteration and return:
@@ -320,10 +319,10 @@ function noTracking(
320319
method: keyof Array<any>,
321320
args: unknown[] = [],
322321
) {
323-
pauseTracking()
324322
startBatch()
323+
const prevSub = setActiveSub()
325324
const res = (toRaw(self) as any)[method].apply(self, args)
325+
setActiveSub(prevSub)
326326
endBatch()
327-
resetTracking()
328327
return res
329328
}

packages/reactivity/src/effect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ const resetTrackingStack: (ReactiveNode | undefined)[] = []
226226
*/
227227
export function pauseTracking(): void {
228228
resetTrackingStack.push(activeSub)
229-
setActiveSub(undefined)
229+
setActiveSub()
230230
}
231231

232232
/**
@@ -264,7 +264,7 @@ export function resetTracking(): void {
264264
if (resetTrackingStack.length) {
265265
setActiveSub(resetTrackingStack.pop()!)
266266
} else {
267-
setActiveSub(undefined)
267+
setActiveSub()
268268
}
269269
}
270270

@@ -305,7 +305,7 @@ export function onEffectCleanup(fn: () => void, failSilently = false): void {
305305

306306
function cleanupEffect(fn: () => void) {
307307
// run cleanup without active effect
308-
const prevSub = setActiveSub(undefined)
308+
const prevSub = setActiveSub()
309309
try {
310310
fn()
311311
} finally {

packages/reactivity/src/effectScope.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ export function getCurrentScope(): EffectScope | undefined {
118118
return activeEffectScope
119119
}
120120

121-
export function setCurrentScope(
122-
scope: EffectScope | undefined,
123-
): EffectScope | undefined {
121+
export function setCurrentScope(scope?: EffectScope): EffectScope | undefined {
124122
try {
125123
return activeEffectScope
126124
} finally {

packages/reactivity/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ export {
102102
type WatchCallback,
103103
type OnCleanup,
104104
} from './watch'
105+
/**
106+
* @internal
107+
*/
108+
export { setActiveSub } from './system'

packages/reactivity/src/system.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ export let activeSub: ReactiveNode | undefined = undefined
4545
let notifyIndex = 0
4646
let notifyBufferLength = 0
4747

48-
export function setActiveSub(
49-
sub: ReactiveNode | undefined,
50-
): ReactiveNode | undefined {
48+
export function setActiveSub(sub?: ReactiveNode): ReactiveNode | undefined {
5149
try {
5250
return activeSub
5351
} finally {

packages/reactivity/src/watch.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ import {
1111
} from '@vue/shared'
1212
import type { ComputedRef } from './computed'
1313
import { ReactiveFlags } from './constants'
14-
import {
15-
type DebuggerOptions,
16-
ReactiveEffect,
17-
cleanup,
18-
pauseTracking,
19-
resetTracking,
20-
} from './effect'
14+
import { type DebuggerOptions, ReactiveEffect, cleanup } from './effect'
2115
import { isReactive, isShallow } from './reactive'
2216
import { type Ref, isRef } from './ref'
17+
import { setActiveSub } from './system'
2318
import { warn } from './warning'
2419

2520
// These errors were transferred from `packages/runtime-core/src/errorHandling.ts`
@@ -159,11 +154,11 @@ export class WatcherEffect extends ReactiveEffect {
159154
// no cb -> simple effect
160155
getter = () => {
161156
if (this.cleanupsLength) {
162-
pauseTracking()
157+
const prevSub = setActiveSub()
163158
try {
164159
cleanup(this)
165160
} finally {
166-
resetTracking()
161+
setActiveSub(prevSub)
167162
}
168163
}
169164
const currentEffect = activeWatcher

packages/runtime-core/src/apiLifecycle.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import type { ComponentPublicInstance } from './componentPublicInstance'
88
import { ErrorTypeStrings, callWithAsyncErrorHandling } from './errorHandling'
99
import { warn } from './warning'
1010
import { toHandlerKey } from '@vue/shared'
11-
import {
12-
type DebuggerEvent,
13-
pauseTracking,
14-
resetTracking,
15-
} from '@vue/reactivity'
11+
import { type DebuggerEvent, setActiveSub } from '@vue/reactivity'
1612
import { LifecycleHooks } from './enums'
1713

1814
export { onActivated, onDeactivated } from './components/KeepAlive'
@@ -33,7 +29,7 @@ export function injectHook(
3329
(hook.__weh = (...args: unknown[]) => {
3430
// disable tracking inside all lifecycle hooks
3531
// since they can potentially be called inside effects.
36-
pauseTracking()
32+
const prevSub = setActiveSub()
3733
// Set currentInstance during hook invocation.
3834
// This assumes the hook does not synchronously trigger other hooks, which
3935
// can only be false when the user does something really funky.
@@ -42,7 +38,7 @@ export function injectHook(
4238
return callWithAsyncErrorHandling(hook, target, type, args)
4339
} finally {
4440
setCurrentInstance(...prev)
45-
resetTracking()
41+
setActiveSub(prevSub)
4642
}
4743
})
4844
if (prepend) {

packages/runtime-core/src/component.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
TrackOpTypes,
66
isRef,
77
markRaw,
8-
pauseTracking,
98
proxyRefs,
10-
resetTracking,
9+
setActiveSub,
1110
shallowReadonly,
1211
track,
1312
} from '@vue/reactivity'
@@ -887,7 +886,7 @@ function setupStatefulComponent(
887886
// 2. call setup()
888887
const { setup } = Component
889888
if (setup) {
890-
pauseTracking()
889+
const prevSub = setActiveSub()
891890
const setupContext = (instance.setupContext =
892891
setup.length > 1 ? createSetupContext(instance) : null)
893892
const prev = setCurrentInstance(instance)
@@ -901,7 +900,7 @@ function setupStatefulComponent(
901900
],
902901
)
903902
const isAsyncSetup = isPromise(setupResult)
904-
resetTracking()
903+
setActiveSub(prevSub)
905904
setCurrentInstance(...prev)
906905

907906
if ((isAsyncSetup || instance.sp) && !isAsyncWrapper(instance)) {
@@ -1085,13 +1084,13 @@ export function finishComponentSetup(
10851084

10861085
// support for 2.x options
10871086
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
1088-
const prev = setCurrentInstance(instance)
1089-
pauseTracking()
1087+
const prevInstance = setCurrentInstance(instance)
1088+
const prevSub = setActiveSub()
10901089
try {
10911090
applyOptions(instance)
10921091
} finally {
1093-
resetTracking()
1094-
setCurrentInstance(...prev)
1092+
setActiveSub(prevSub)
1093+
setCurrentInstance(...prevInstance)
10951094
}
10961095
}
10971096

packages/runtime-core/src/customFormatter.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {
44
isReadonly,
55
isRef,
66
isShallow,
7-
pauseTracking,
8-
resetTracking,
7+
setActiveSub,
98
toRaw,
109
} from '@vue/reactivity'
1110
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
@@ -37,9 +36,9 @@ export function initCustomFormatter(): void {
3736
return ['div', vueStyle, `VueInstance`]
3837
} else if (isRef(obj)) {
3938
// avoid tracking during debugger accessing
40-
pauseTracking()
39+
const prevSub = setActiveSub()
4140
const value = obj.value
42-
resetTracking()
41+
setActiveSub(prevSub)
4342
return [
4443
'div',
4544
{},

packages/runtime-core/src/directives.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { currentRenderingInstance } from './componentRenderContext'
2323
import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
2424
import type { ComponentPublicInstance } from './componentPublicInstance'
2525
import { mapCompatDirectiveHook } from './compat/customDirective'
26-
import { pauseTracking, resetTracking, traverse } from '@vue/reactivity'
26+
import { setActiveSub, traverse } from '@vue/reactivity'
2727

2828
export interface DirectiveBinding<
2929
Value = any,
@@ -187,14 +187,14 @@ export function invokeDirectiveHook(
187187
if (hook) {
188188
// disable tracking inside all lifecycle hooks
189189
// since they can potentially be called inside effects.
190-
pauseTracking()
190+
const prevSub = setActiveSub()
191191
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
192192
vnode.el,
193193
binding,
194194
vnode,
195195
prevVNode,
196196
])
197-
resetTracking()
197+
setActiveSub(prevSub)
198198
}
199199
}
200200
}

0 commit comments

Comments
 (0)