Skip to content

Commit

Permalink
perf: avoid deopt for props/emits normalization when global mixins ar…
Browse files Browse the repository at this point in the history
…e used
  • Loading branch information
yyx990803 committed Jun 2, 2021
1 parent e2ca67b commit 51d2be2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
23 changes: 14 additions & 9 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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' +
Expand Down
8 changes: 0 additions & 8 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
13 changes: 9 additions & 4 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,15 +203,18 @@ export function normalizeEmitsOptions(
}

if (!raw && !hasExtends) {
return (comp.__emits = null)
cache.set(comp, null)
return null
}

if (isArray(raw)) {
raw.forEach(key => (normalized[key] = null))
} 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.
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 51d2be2

Please sign in to comment.