]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf: avoid deopt for props/emits normalization when global mixins are used
authorEvan You <yyx990803@gmail.com>
Wed, 2 Jun 2021 19:22:52 +0000 (15:22 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 2 Jun 2021 19:22:52 +0000 (15:22 -0400)
packages/runtime-core/src/apiCreateApp.ts
packages/runtime-core/src/component.ts
packages/runtime-core/src/componentEmits.ts
packages/runtime-core/src/componentOptions.ts
packages/runtime-core/src/componentProps.ts

index 617ddd20e384067bab09b8cc2aec701a3b07de02..4bdc21c1dac9550df852e6a0187d8c86f9b3ddfc 100644 (file)
@@ -20,6 +20,8 @@ import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
 import { isFunction, NO, isObject } from '@vue/shared'
 import { version } from '.'
 import { installAppCompatProperties } from './compat/global'
+import { NormalizedPropsOptions } from './componentProps'
+import { ObjectEmitsOptions } from './componentEmits'
 
 export interface App<HostElement = any> {
   version: string
@@ -101,13 +103,19 @@ export interface AppContext {
    * Cache for merged/normalized component options
    * Each app instance has its own cache because app-level global mixins and
    * optionMergeStrategies can affect merge behavior.
+   * @internal
    */
-  cache: WeakMap<ComponentOptions, MergedComponentOptions>
+  optionsCache: WeakMap<ComponentOptions, MergedComponentOptions>
   /**
-   * Flag for de-optimizing props normalization
+   * Cache for normalized props options
    * @internal
    */
-  deopt?: boolean
+  propsCache: WeakMap<ConcreteComponent, NormalizedPropsOptions>
+  /**
+   * Cache for normalized emits options
+   * @internal
+   */
+  emitsCache: WeakMap<ConcreteComponent, ObjectEmitsOptions | null>
   /**
    * HMR only
    * @internal
@@ -144,7 +152,9 @@ export function createAppContext(): AppContext {
     components: {},
     directives: {},
     provides: Object.create(null),
-    cache: new WeakMap()
+    optionsCache: new WeakMap(),
+    propsCache: new WeakMap(),
+    emitsCache: new WeakMap()
   }
 }
 
@@ -213,11 +223,6 @@ export function createAppAPI<HostElement>(
         if (__FEATURE_OPTIONS_API__) {
           if (!context.mixins.includes(mixin)) {
             context.mixins.push(mixin)
-            // global mixin with props/emits de-optimizes props/emits
-            // normalization caching.
-            if (mixin.props || mixin.emits) {
-              context.deopt = true
-            }
           } else if (__DEV__) {
             warn(
               'Mixin has already been applied to target app' +
index fd7b5777c93b3026007beac97a56de7adbc58a6f..78cabdd3538b8b6d643a30a7640c2c1292d6877e 100644 (file)
@@ -76,14 +76,6 @@ export interface AllowedComponentProps {
 // Note: can't mark this whole interface internal because some public interfaces
 // extend it.
 export interface ComponentInternalOptions {
-  /**
-   * @internal
-   */
-  __props?: NormalizedPropsOptions
-  /**
-   * @internal
-   */
-  __emits?: ObjectEmitsOptions | null
   /**
    * @internal
    */
index 44f9765dd9ac378c6b7ba4b2b5be4fe3924dc055..b7fbec48992f0999cdf8c42f62b3570f5cd0f578 100644 (file)
@@ -172,8 +172,10 @@ export function normalizeEmitsOptions(
   appContext: AppContext,
   asMixin = false
 ): ObjectEmitsOptions | null {
-  if (!appContext.deopt && comp.__emits !== undefined) {
-    return comp.__emits
+  const cache = appContext.emitsCache
+  const cached = cache.get(comp)
+  if (cached !== undefined) {
+    return cached
   }
 
   const raw = comp.emits
@@ -201,7 +203,8 @@ export function normalizeEmitsOptions(
   }
 
   if (!raw && !hasExtends) {
-    return (comp.__emits = null)
+    cache.set(comp, null)
+    return null
   }
 
   if (isArray(raw)) {
@@ -209,7 +212,9 @@ export function normalizeEmitsOptions(
   } else {
     extend(normalized, raw)
   }
-  return (comp.__emits = normalized)
+
+  cache.set(comp, normalized)
+  return normalized
 }
 
 // Check if an incoming prop key is a declared emit event listener.
index 3983e8d94e71ed7be5cb3d8b7c1cc8827fedacef..11c42fcd8e07182ec368cdf359d9def11749b4b0 100644 (file)
@@ -894,7 +894,7 @@ export function resolveMergedOptions(
   const { mixins, extends: extendsOptions } = base
   const {
     mixins: globalMixins,
-    cache,
+    optionsCache: cache,
     config: { optionMergeStrategies }
   } = instance.appContext
   const cached = cache.get(base)
index 3c9afc7c5c1582ee74d428f705e7e8dc0ee73abd..b83f0abbe24c85f3c9e48b1fe932b4e70cb60b4f 100644 (file)
@@ -436,8 +436,10 @@ export function normalizePropsOptions(
   appContext: AppContext,
   asMixin = false
 ): NormalizedPropsOptions {
-  if (!appContext.deopt && comp.__props) {
-    return comp.__props
+  const cache = appContext.propsCache
+  const cached = cache.get(comp)
+  if (cached) {
+    return cached
   }
 
   const raw = comp.props
@@ -468,7 +470,8 @@ export function normalizePropsOptions(
   }
 
   if (!raw && !hasExtends) {
-    return (comp.__props = EMPTY_ARR as any)
+    cache.set(comp, EMPTY_ARR as any)
+    return EMPTY_ARR as any
   }
 
   if (isArray(raw)) {
@@ -506,7 +509,9 @@ export function normalizePropsOptions(
     }
   }
 
-  return (comp.__props = [normalized, needCastKeys])
+  const res: NormalizedPropsOptions = [normalized, needCastKeys]
+  cache.set(comp, res)
+  return res
 }
 
 function validatePropName(key: string) {