From 7097f9169eafc14ce2c105303ee022970222cb12 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Fri, 29 Nov 2024 18:18:23 +0800 Subject: [PATCH 1/2] perf: `setup()` returning object is only needed in __DEV__ (hmr) --- packages/runtime-vapor/src/apiRender.ts | 56 ++++++++++++++----------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/runtime-vapor/src/apiRender.ts b/packages/runtime-vapor/src/apiRender.ts index fe06beb65..318d9af20 100644 --- a/packages/runtime-vapor/src/apiRender.ts +++ b/packages/runtime-vapor/src/apiRender.ts @@ -55,32 +55,38 @@ export function setupComponent(instance: ComponentInternalInstance): void { let block: Block | undefined - if ( - stateOrNode && - (stateOrNode instanceof Node || - isArray(stateOrNode) || - fragmentKey in stateOrNode || - componentKey in stateOrNode) - ) { + // Skip the type check for production since this is only for Dev HMR + if (__DEV__) { + if ( + stateOrNode && + (stateOrNode instanceof Node || + isArray(stateOrNode) || + fragmentKey in stateOrNode || + componentKey in stateOrNode) + ) { + block = stateOrNode + } else if (isObject(stateOrNode)) { + instance.setupState = proxyRefs(stateOrNode) + } + + if (!block && component.render) { + pauseTracking() + block = callWithErrorHandling( + component.render, + instance, + VaporErrorCodes.RENDER_FUNCTION, + [ + instance.setupState, // _ctx + __DEV__ ? shallowReadonly(props) : props, // $props + instance.emit, // $emit + __DEV__ ? getAttrsProxy(instance) : instance.attrs, // $attrs + __DEV__ ? getSlotsProxy(instance) : instance.slots, // $slots + ], + ) + resetTracking() + } + } else { block = stateOrNode - } else if (isObject(stateOrNode)) { - instance.setupState = proxyRefs(stateOrNode) - } - if (!block && component.render) { - pauseTracking() - block = callWithErrorHandling( - component.render, - instance, - VaporErrorCodes.RENDER_FUNCTION, - [ - instance.setupState, // _ctx - __DEV__ ? shallowReadonly(props) : props, // $props - instance.emit, // $emit - __DEV__ ? getAttrsProxy(instance) : instance.attrs, // $attrs - __DEV__ ? getSlotsProxy(instance) : instance.slots, // $slots - ], - ) - resetTracking() } if (!block) { From dc39fb4470e0910d0e53ff29ad46518f00c74ac8 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Sat, 30 Nov 2024 17:25:44 +0800 Subject: [PATCH 2/2] refactor(runtime-vapor): simplify props and context handling in setupComponent --- packages/runtime-vapor/src/apiRender.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/runtime-vapor/src/apiRender.ts b/packages/runtime-vapor/src/apiRender.ts index 318d9af20..408a7928d 100644 --- a/packages/runtime-vapor/src/apiRender.ts +++ b/packages/runtime-vapor/src/apiRender.ts @@ -77,10 +77,10 @@ export function setupComponent(instance: ComponentInternalInstance): void { VaporErrorCodes.RENDER_FUNCTION, [ instance.setupState, // _ctx - __DEV__ ? shallowReadonly(props) : props, // $props + shallowReadonly(props), // $props instance.emit, // $emit - __DEV__ ? getAttrsProxy(instance) : instance.attrs, // $attrs - __DEV__ ? getSlotsProxy(instance) : instance.slots, // $slots + getAttrsProxy(instance), // $attrs + getSlotsProxy(instance), // $slots ], ) resetTracking()