Skip to content

Commit

Permalink
[BUGFIX release] Simplify mixin application
Browse files Browse the repository at this point in the history
Simplifies mixin application in a number of ways:

- Ensures that we only access `meta` once for a given object when
  applying mixins
- Ensures that we only `peekDescriptors` once for a given descriptor
- Minimizes the number of if/else branches and defaulting in general
- Breaks apart `defineProperty` so that we can do less work per
  definition when mixing in mixins, since we know more about what
  possible operations will occur.
- Removes extra brand checks from `defineProperty` (Array.isArray) so
  we don't penalize every defineProperty for doing that.
- Only revalidate observers once per mixin application.
- Only brand check `didDefineProperty` once per mixin application.
- Replace `for..in` loops with `Object.keys` since we only care about
  own properties.
- Only loop once in `extractAccessors`.
- Combine observer and listener meta into a single object so we only
  need to do one lookup. In most cases, this is undefined in modern
  applications, so no extra memory cost.
- Simplify CP descriptor property lookups (remove duplicate functions).
  • Loading branch information
Chris Garrett committed Aug 7, 2020
1 parent 52d12c8 commit 3990a01
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 540 deletions.
446 changes: 216 additions & 230 deletions packages/@ember/-internals/metal/lib/computed.ts

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions packages/@ember/-internals/metal/lib/descriptor_map.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Meta, peekMeta } from '@ember/-internals/meta';
import { assert } from '@ember/debug';

const DECORATOR_DESCRIPTOR_MAP: WeakMap<
import('./decorator').Decorator,
import('./decorator').ComputedDescriptor | boolean
> = new WeakMap();
const DECORATOR_DESCRIPTOR_MAP: WeakMap<Function, unknown | boolean> = new WeakMap();

/**
Returns the CP descriptor associated with `obj` and `keyName`, if any.
Expand All @@ -30,8 +27,8 @@ export function descriptorForProperty(obj: object, keyName: string, _meta?: Meta
}
}

export function descriptorForDecorator(dec: import('./decorator').Decorator) {
return DECORATOR_DESCRIPTOR_MAP.get(dec);
export function descriptorForDecorator<T>(dec: Function): T | true | undefined {
return DECORATOR_DESCRIPTOR_MAP.get(dec) as T;
}

/**
Expand All @@ -43,7 +40,7 @@ export function descriptorForDecorator(dec: import('./decorator').Decorator) {
@private
*/
export function isClassicDecorator(dec: any) {
return dec !== null && dec !== undefined && DECORATOR_DESCRIPTOR_MAP.has(dec);
return typeof dec === 'function' && DECORATOR_DESCRIPTOR_MAP.has(dec);
}

/**
Expand All @@ -53,6 +50,6 @@ export function isClassicDecorator(dec: any) {
@param {function} decorator the value to mark as a decorator
@private
*/
export function setClassicDecorator(dec: import('./decorator').Decorator, value: any = true) {
export function setClassicDecorator(dec: Function, value: any = true) {
DECORATOR_DESCRIPTOR_MAP.set(dec, value);
}
Loading

0 comments on commit 3990a01

Please sign in to comment.