Skip to content

Commit 03566c6

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

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
}

packages/runtime-core/src/errorHandling.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pauseTracking, resetTracking } from '@vue/reactivity'
1+
import { setActiveSub } from '@vue/reactivity'
22
import type { GenericComponentInstance } from './component'
33
import { popWarningContext, pushWarningContext, warn } from './warning'
44
import { EMPTY_OBJ, isArray, isFunction, isPromise } from '@vue/shared'
@@ -139,13 +139,13 @@ export function handleError(
139139
}
140140
// app-level handling
141141
if (errorHandler) {
142-
pauseTracking()
142+
const prevSub = setActiveSub()
143143
callWithErrorHandling(errorHandler, null, ErrorCodes.APP_ERROR_HANDLER, [
144144
err,
145145
exposedInstance,
146146
errorInfo,
147147
])
148-
resetTracking()
148+
setActiveSub(prevSub)
149149
return
150150
}
151151
}

packages/runtime-core/src/renderer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ import {
5757
import {
5858
EffectFlags,
5959
ReactiveEffect,
60-
pauseTracking,
61-
resetTracking,
60+
setActiveSub,
6261
setCurrentScope,
6362
} from '@vue/reactivity'
6463
import { updateProps } from './componentProps'
@@ -1693,11 +1692,11 @@ function baseCreateRenderer(
16931692
updateProps(instance, nextVNode.props, prevProps, optimized)
16941693
updateSlots(instance, nextVNode.children, optimized)
16951694

1696-
pauseTracking()
1695+
const prevSub = setActiveSub()
16971696
// props update may have triggered pre-flush watchers.
16981697
// flush them before the render update.
16991698
flushPreFlushCbs(instance)
1700-
resetTracking()
1699+
setActiveSub(prevSub)
17011700
}
17021701

17031702
const patchChildren: PatchChildrenFn = (

packages/runtime-core/src/warning.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
formatComponentName,
55
} from './component'
66
import { isFunction, isString } from '@vue/shared'
7-
import { isRef, pauseTracking, resetTracking, toRaw } from '@vue/reactivity'
7+
import { isRef, setActiveSub, toRaw } from '@vue/reactivity'
88
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
99
import { type VNode, isVNode } from './vnode'
1010

@@ -41,7 +41,7 @@ export function warn(msg: string, ...args: any[]): void {
4141

4242
// avoid props formatting or warn handler tracking deps that might be mutated
4343
// during patch, leading to infinite recursion.
44-
pauseTracking()
44+
const prevSub = setActiveSub()
4545

4646
const entry = stack.length ? stack[stack.length - 1] : null
4747
const instance = isVNode(entry) ? entry.component : entry
@@ -79,7 +79,7 @@ export function warn(msg: string, ...args: any[]): void {
7979
console.warn(...warnArgs)
8080
}
8181

82-
resetTracking()
82+
setActiveSub(prevSub)
8383
isWarning = false
8484
}
8585

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import {
33
type ShallowRef,
44
isReactive,
55
isShallow,
6-
pauseTracking,
7-
resetTracking,
6+
setActiveSub,
87
shallowReadArray,
98
shallowRef,
109
toReactive,
@@ -95,7 +94,7 @@ export const createFor = (
9594
const oldLength = oldBlocks.length
9695
newBlocks = new Array(newLength)
9796

98-
pauseTracking()
97+
const prevSub = setActiveSub()
9998

10099
if (!isMounted) {
101100
isMounted = true
@@ -284,7 +283,7 @@ export const createFor = (
284283
frag.nodes.push(parentAnchor)
285284
}
286285

287-
resetTracking()
286+
setActiveSub(prevSub)
288287
}
289288

290289
const needKey = renderItem.length > 1

packages/runtime-vapor/src/block.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
unmountComponent,
77
} from './component'
88
import { createComment, createTextNode } from './dom/node'
9-
import { EffectScope, pauseTracking, resetTracking } from '@vue/reactivity'
9+
import { EffectScope, setActiveSub } from '@vue/reactivity'
1010
import { isHydrating } from './dom/hydration'
1111

1212
export type Block =
@@ -47,7 +47,7 @@ export class DynamicFragment extends VaporFragment {
4747
}
4848
this.current = key
4949

50-
pauseTracking()
50+
const prevSub = setActiveSub()
5151
const parent = this.anchor.parentNode
5252

5353
// teardown previous branch
@@ -73,7 +73,7 @@ export class DynamicFragment extends VaporFragment {
7373
parent && insert(this.nodes, parent, this.anchor)
7474
}
7575

76-
resetTracking()
76+
setActiveSub(prevSub)
7777
}
7878
}
7979

packages/runtime-vapor/src/component.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ import {
3030
type ShallowRef,
3131
markRaw,
3232
onScopeDispose,
33-
pauseTracking,
3433
proxyRefs,
35-
resetTracking,
34+
setActiveSub,
3635
unref,
3736
} from '@vue/reactivity'
3837
import { EMPTY_OBJ, invokeArrayFns, isFunction, isString } from '@vue/shared'
@@ -191,8 +190,8 @@ export function createComponent(
191190
instance.emitsOptions = normalizeEmitsOptions(component)
192191
}
193192

194-
const prev = setCurrentInstance(instance)
195-
pauseTracking()
193+
const prevInstance = setCurrentInstance(instance)
194+
const prevSub = setActiveSub()
196195

197196
if (__DEV__) {
198197
setupPropsValidation(instance)
@@ -258,8 +257,8 @@ export function createComponent(
258257
})
259258
}
260259

261-
resetTracking()
262-
setCurrentInstance(...prev)
260+
setActiveSub(prevSub)
261+
setCurrentInstance(...prevInstance)
263262

264263
if (__DEV__) {
265264
popWarningContext()

0 commit comments

Comments
 (0)