From ca84316bfb3410efe21333670a6ad5cd21857396 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 12 Apr 2024 16:02:52 +0800 Subject: [PATCH] perf(ssr): optimize setup context creation for ssr in v8 --- packages/runtime-core/src/component.ts | 62 ++++++++++----------- packages/runtime-core/src/componentProps.ts | 2 +- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 4cabdad0d44..a551529e667 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -1004,36 +1004,28 @@ export function finishComponentSetup( } } -function getAttrsProxy(instance: ComponentInternalInstance): Data { - return ( - instance.attrsProxy || - (instance.attrsProxy = new Proxy( - instance.attrs, - __DEV__ - ? { - get(target, key: string) { - markAttrsAccessed() - track(instance, TrackOpTypes.GET, '$attrs') - return target[key] - }, - set() { - warn(`setupContext.attrs is readonly.`) - return false - }, - deleteProperty() { - warn(`setupContext.attrs is readonly.`) - return false - }, - } - : { - get(target, key: string) { - track(instance, TrackOpTypes.GET, '$attrs') - return target[key] - }, - }, - )) - ) -} +const attrsProxyHandlers = __DEV__ + ? { + get(target: Data, key: string) { + markAttrsAccessed() + track(target, TrackOpTypes.GET, '') + return target[key] + }, + set() { + warn(`setupContext.attrs is readonly.`) + return false + }, + deleteProperty() { + warn(`setupContext.attrs is readonly.`) + return false + }, + } + : { + get(target: Data, key: string) { + track(target, TrackOpTypes.GET, '') + return target[key] + }, + } /** * Dev-only @@ -1080,9 +1072,13 @@ export function createSetupContext( if (__DEV__) { // We use getters in dev in case libs like test-utils overwrite instance // properties (overwrites should not be done in prod) + let attrsProxy: Data return Object.freeze({ get attrs() { - return getAttrsProxy(instance) + return ( + attrsProxy || + (attrsProxy = new Proxy(instance.attrs, attrsProxyHandlers)) + ) }, get slots() { return getSlotsProxy(instance) @@ -1094,9 +1090,7 @@ export function createSetupContext( }) } else { return { - get attrs() { - return getAttrsProxy(instance) - }, + attrs: new Proxy(instance.attrs, attrsProxyHandlers), slots: instance.slots, emit: instance.emit, expose, diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index c0cef2f0901..1c87304185c 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -365,7 +365,7 @@ export function updateProps( // trigger updates for $attrs in case it's used in component slots if (hasAttrsChanged) { - trigger(instance, TriggerOpTypes.SET, '$attrs') + trigger(instance.attrs, TriggerOpTypes.SET, '') } if (__DEV__) {