Skip to content

Commit

Permalink
fixup! perf(ivy): move local references into consts array
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto committed Oct 21, 2019
1 parent b18cf83 commit 2d2556c
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 46 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/render3/definition.ts
Expand Up @@ -18,7 +18,7 @@ import {stringify} from '../util/stringify';
import {EMPTY_ARRAY, EMPTY_OBJ} from './empty';
import {NG_BASE_DEF, NG_COMP_DEF, NG_DIR_DEF, NG_FACTORY_DEF, NG_LOC_ID_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature, DirectiveType, DirectiveTypesOrFactory, FactoryFn, HostBindingsFunction, PipeDef, PipeType, PipeTypesOrFactory, ViewQueriesFunction, ɵɵBaseDef} from './interfaces/definition';
import {TAttributes} from './interfaces/node';
import {TConstants} from './interfaces/node';
// while SelectorFlags is unused here, it's required so that types don't get resolved lazily
// see: https://github.com/Microsoft/web-build-tools/issues/1050
import {CssSelectorList, SelectorFlags} from './interfaces/projection';
Expand Down Expand Up @@ -172,8 +172,11 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
*/
template: ComponentTemplate<T>;

/** Constants for the nodes in the component's view. */
consts?: (TAttributes | string)[];
/**
* Constants for the nodes in the component's view.
* Includes attribute arrays, local definition arrays etc.
*/
consts?: TConstants;

/**
* An array of `ngContent[selector]` values that were found in the template.
Expand Down
15 changes: 6 additions & 9 deletions packages/core/src/render3/instructions/container.ts
Expand Up @@ -17,7 +17,7 @@ import {BINDING_INDEX, FLAGS, HEADER_OFFSET, InitPhaseState, LView, LViewFlags,
import {assertNodeType} from '../node_assert';
import {appendChild, removeView} from '../node_manipulation';
import {getCheckNoChangesMode, getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {getNativeByTNode, load} from '../util/view_utils';
import {getConstant, getNativeByTNode, load} from '../util/view_utils';

import {addToViewTree, createDirectivesInstances, createLContainer, createTNode, createTView, getOrCreateTNode, resolveDirectives, saveResolvedLocalsInData} from './shared';

Expand Down Expand Up @@ -56,28 +56,25 @@ export function ɵɵcontainer(index: number): void {
* @param decls The number of nodes, local refs, and pipes for this template
* @param vars The number of bindings for this template
* @param tagName The name of the container element, if applicable
* @param constsIndex Index of template in the `consts` array.
* @param localRefs Index of the local refenreces in the `consts` array.
* @param attrsIndex Index of template attributes in the `consts` array.
* @param localRefs Index of the local references in the `consts` array.
* @param localRefExtractor A function which extracts local-refs values from the template.
* Defaults to the current element associated with the local-ref.
*
* @codeGenApi
*/
export function ɵɵtemplate(
index: number, templateFn: ComponentTemplate<any>| null, decls: number, vars: number,
tagName?: string | null, constsIndex?: number | null, localRefsIndex?: number | null,
tagName?: string | null, attrsIndex?: number | null, localRefsIndex?: number | null,
localRefExtractor?: LocalRefExtractor) {
const lView = getLView();
const tView = lView[TVIEW];
const tViewConsts = tView.consts;

// TODO: consider a separate node type for templates
const tContainerNode = containerInternal(
lView, index, tagName || null,
tViewConsts === null || constsIndex == null ? null : tViewConsts[constsIndex] as TAttributes);
const localRefs = tViewConsts === null || localRefsIndex == null ?
null :
tViewConsts[localRefsIndex] as string[];
lView, index, tagName || null, getConstant(tViewConsts, attrsIndex) as TAttributes);
const localRefs = getConstant(tViewConsts, localRefsIndex) as string[];
if (tView.firstTemplatePass) {
ngDevMode && ngDevMode.firstTemplatePass++;
resolveDirectives(tView, lView, tContainerNode, localRefs);
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/render3/instructions/element.ts
Expand Up @@ -20,7 +20,7 @@ import {appendChild} from '../node_manipulation';
import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getNamespace, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {setUpAttributes} from '../util/attrs_utils';
import {getInitialStylingValue, hasClassInput, hasStyleInput, selectClassBasedInputName} from '../util/styling_utils';
import {getNativeByTNode, getTNode} from '../util/view_utils';
import {getConstant, getNativeByTNode, getTNode} from '../util/view_utils';

import {createDirectivesInstances, elementCreate, executeContentQueries, getOrCreateTNode, renderInitialStyling, resolveDirectives, saveResolvedLocalsInData, setInputsForProperty} from './shared';
import {registerInitialStylingOnTNode} from './styling';
Expand All @@ -46,11 +46,8 @@ export function ɵɵelementStart(
const lView = getLView();
const tView = lView[TVIEW];
const tViewConsts = tView.consts;
const attrs =
tViewConsts === null || attrsIndex == null ? null : tViewConsts[attrsIndex] as TAttributes;
const localRefs = tViewConsts === null || localRefsIndex === undefined ?
null :
tViewConsts[localRefsIndex] as string[];
const attrs = getConstant(tViewConsts, attrsIndex) as TAttributes;
const localRefs = getConstant(tViewConsts, localRefsIndex) as string[];
if (ngDevMode) {
assertEqual(
lView[BINDING_INDEX], tView.bindingStartIndex,
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/render3/instructions/element_container.ts
Expand Up @@ -15,6 +15,7 @@ import {BINDING_INDEX, HEADER_OFFSET, RENDERER, TVIEW, T_HOST} from '../interfac
import {assertNodeType} from '../node_assert';
import {appendChild} from '../node_manipulation';
import {getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
import {getConstant} from '../util/view_utils';

import {createDirectivesInstances, executeContentQueries, getOrCreateTNode, resolveDirectives, saveResolvedLocalsInData} from './shared';
import {registerInitialStylingOnTNode} from './styling';
Expand Down Expand Up @@ -42,11 +43,8 @@ export function ɵɵelementContainerStart(
const renderer = lView[RENDERER];
const tagName = 'ng-container';
const tViewConsts = tView.consts;
const attrs =
tViewConsts === null || attrsIndex == null ? null : tViewConsts[attrsIndex] as TAttributes;
const localRefs = tViewConsts === null || localRefsIndex === undefined ?
null :
tViewConsts[localRefsIndex] as string[];
const attrs = getConstant(tViewConsts, attrsIndex) as TAttributes;
const localRefs = getConstant(tViewConsts, localRefsIndex) as string[];
ngDevMode && assertEqual(
lView[BINDING_INDEX], tView.bindingStartIndex,
'element containers should be created before any bindings');
Expand Down Expand Up @@ -118,13 +116,13 @@ export function ɵɵelementContainerEnd(): void {
* and {@link elementContainerEnd}
*
* @param index Index of the element in the LView array
* @param constsIndex Index of the container attributes in the `consts` array.
* @param attrsIndex Index of the container attributes in the `consts` array.
* @param localRefsIndex Index of the container's local references in the `consts` array.
*
* @codeGenApi
*/
export function ɵɵelementContainer(
index: number, constsIndex?: number | null, localRefsIndex?: number): void {
ɵɵelementContainerStart(index, constsIndex, localRefsIndex);
index: number, attrsIndex?: number | null, localRefsIndex?: number): void {
ɵɵelementContainerStart(index, attrsIndex, localRefsIndex);
ɵɵelementContainerEnd();
}
4 changes: 2 additions & 2 deletions packages/core/src/render3/instructions/lview_debug.ts
Expand Up @@ -14,7 +14,7 @@ import {initNgDevMode} from '../../util/ng_dev_mode';
import {ACTIVE_INDEX, CONTAINER_HEADER_OFFSET, LContainer, MOVED_VIEWS, NATIVE} from '../interfaces/container';
import {DirectiveDefList, PipeDefList, ViewQueriesFunction} from '../interfaces/definition';
import {COMMENT_MARKER, ELEMENT_MARKER, I18nMutateOpCode, I18nMutateOpCodes, I18nUpdateOpCode, I18nUpdateOpCodes, TIcu} from '../interfaces/i18n';
import {PropertyAliases, TAttributes, TContainerNode, TElementNode, TNode as ITNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TViewNode} from '../interfaces/node';
import {PropertyAliases, TConstants, TContainerNode, TElementNode, TNode as ITNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TViewNode} from '../interfaces/node';
import {SelectorFlags} from '../interfaces/projection';
import {TQueries} from '../interfaces/query';
import {RComment, RElement, RNode} from '../interfaces/renderer';
Expand Down Expand Up @@ -102,7 +102,7 @@ export const TViewConstructor = class TView implements ITView {
public pipeRegistry: PipeDefList|null, //
public firstChild: TNode|null, //
public schemas: SchemaMetadata[]|null, //
public consts: (TAttributes|string)[]|null, //
public consts: TConstants|null, //
) {}

get template_(): string {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/render3/instructions/shared.ts
Expand Up @@ -23,7 +23,7 @@ import {executeCheckHooks, executeInitAndCheckHooks, incrementInitPhaseFlags, re
import {ACTIVE_INDEX, CONTAINER_HEADER_OFFSET, LContainer} from '../interfaces/container';
import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefListOrFactory, PipeDefListOrFactory, RenderFlags, ViewQueriesFunction} from '../interfaces/definition';
import {INJECTOR_BLOOM_PARENT_SIZE, NodeInjectorFactory} from '../interfaces/injector';
import {AttributeMarker, InitialInputData, InitialInputs, LocalRefExtractor, PropertyAliasValue, PropertyAliases, TAttributes, TContainerNode, TElementContainerNode, TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TProjectionNode, TViewNode} from '../interfaces/node';
import {AttributeMarker, InitialInputData, InitialInputs, LocalRefExtractor, PropertyAliasValue, PropertyAliases, TAttributes, TConstants, TContainerNode, TElementContainerNode, TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TProjectionNode, TViewNode} from '../interfaces/node';
import {RComment, RElement, RText, Renderer3, RendererFactory3, isProceduralRenderer} from '../interfaces/renderer';
import {SanitizerFn} from '../interfaces/sanitization';
import {isComponentDef, isComponentHost, isContentQueryHost, isLContainer, isRootView} from '../interfaces/type_checks';
Expand Down Expand Up @@ -591,7 +591,7 @@ export function createTView(
viewIndex: number, templateFn: ComponentTemplate<any>| null, decls: number, vars: number,
directives: DirectiveDefListOrFactory | null, pipes: PipeDefListOrFactory | null,
viewQuery: ViewQueriesFunction<any>| null, schemas: SchemaMetadata[] | null,
consts: (TAttributes | string)[] | null): TView {
consts: TConstants | null): TView {
ngDevMode && ngDevMode.tView++;
const bindingStartIndex = HEADER_OFFSET + decls;
// This length does not yet contain host bindings from child directives because at this point,
Expand Down Expand Up @@ -630,7 +630,7 @@ export function createTView(
typeof pipes === 'function' ? pipes() : pipes, // pipeRegistry: PipeDefList|null,
null, // firstChild: TNode|null,
schemas, // schemas: SchemaMetadata[]|null,
consts) : // consts: (TAttributes|string)[]|null
consts) : // consts: TConstants|null
{
id: viewIndex,
blueprint: blueprint,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/render3/interfaces/definition.ts
Expand Up @@ -10,7 +10,7 @@ import {SchemaMetadata, ViewEncapsulation} from '../../core';
import {ProcessProvidersFunction} from '../../di/interface/provider';
import {Type} from '../../interface/type';

import {TAttributes} from './node';
import {TConstants} from './node';
import {CssSelectorList} from './projection';
import {TView} from './view';

Expand Down Expand Up @@ -240,7 +240,7 @@ export interface ComponentDef<T> extends DirectiveDef<T> {
readonly template: ComponentTemplate<T>;

/** Constants associated with the component's view. */
readonly consts: (TAttributes|string)[]|null;
readonly consts: TConstants|null;

/**
* An array of `ngContent[selector]` values that were found in the template.
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/render3/interfaces/node.ts
Expand Up @@ -224,6 +224,13 @@ export const enum AttributeMarker {
*/
export type TAttributes = (string | AttributeMarker | CssSelector)[];

/**
* Constants that are associated with a view. Includes:
* - Attribute arrays.
* - Local definition arrays.
*/
export type TConstants = (TAttributes | string)[];

/**
* Binding data (flyweight) for a particular node that is shared between all templates
* of a specific type.
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/render3/interfaces/view.ts
Expand Up @@ -15,7 +15,7 @@ import {Sanitizer} from '../../sanitization/sanitizer';
import {LContainer} from './container';
import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefList, HostBindingsFunction, PipeDef, PipeDefList, ViewQueriesFunction} from './definition';
import {I18nUpdateOpCodes, TI18n} from './i18n';
import {TAttributes, TElementNode, TNode, TViewNode} from './node';
import {TAttributes, TConstants, TElementNode, TNode, TViewNode} from './node';
import {PlayerHandler} from './player';
import {LQueries, TQueries} from './query';
import {RElement, Renderer3, RendererFactory3} from './renderer';
Expand Down Expand Up @@ -563,10 +563,10 @@ export interface TView {
schemas: SchemaMetadata[]|null;

/**
* Array of attributes for all of the elements in the view. Used
* for directive matching and attribute bindings.
* Array of constants for the view. Includes attribute arrays, local definition arrays etc.
* Used for directive matching, attribute bindings, local definitions and more.
*/
consts: (TAttributes|string)[]|null;
consts: TConstants|null;
}

export const enum RootContextFlags {Empty = 0b00, DetectChanges = 0b01, FlushPlayers = 0b10}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/render3/util/view_utils.ts
Expand Up @@ -10,7 +10,7 @@ import {assertDataInRange, assertDefined, assertDomNode, assertGreaterThan, asse
import {assertTNodeForLView} from '../assert';
import {LContainer, TYPE} from '../interfaces/container';
import {LContext, MONKEY_PATCH_KEY_NAME} from '../interfaces/context';
import {TNode} from '../interfaces/node';
import {TConstants, TNode} from '../interfaces/node';
import {RNode, isProceduralRenderer} from '../interfaces/renderer';
import {isLContainer, isLView} from '../interfaces/type_checks';
import {FLAGS, HEADER_OFFSET, HOST, LView, LViewFlags, PARENT, PREORDER_HOOK_FLAGS, RENDERER, TData, TVIEW} from '../interfaces/view';
Expand Down Expand Up @@ -175,6 +175,11 @@ export function viewAttachedToContainer(view: LView): boolean {
return isLContainer(view[PARENT]);
}

/** Returns a constant from `TConstants` instance. */
export function getConstant(consts: TConstants | null, index: number | null | undefined) {
return consts === null || index == null ? null : consts[index];
}

/**
* Resets the pre-order hook flags of the view.
* @param lView the LView on which the flags are reset
Expand Down
Expand Up @@ -284,6 +284,9 @@
{
"name": "getComponentViewByIndex"
},
{
"name": "getConstant"
},
{
"name": "getContainerRenderParent"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Expand Up @@ -623,6 +623,9 @@
{
"name": "getConfig"
},
{
"name": "getConstant"
},
{
"name": "getContainerRenderParent"
},
Expand Down
9 changes: 4 additions & 5 deletions packages/core/test/render3/render_util.ts
Expand Up @@ -13,7 +13,7 @@ import {TemplateRef} from '@angular/core/src/linker/template_ref';
import {ViewContainerRef} from '@angular/core/src/linker/view_container_ref';
import {Renderer2} from '@angular/core/src/render/api';
import {createLView, createTView, getOrCreateTNode, getOrCreateTView, renderComponentOrTemplate} from '@angular/core/src/render3/instructions/shared';
import {TAttributes, TNodeType} from '@angular/core/src/render3/interfaces/node';
import {TAttributes, TConstants, TNodeType} from '@angular/core/src/render3/interfaces/node';
import {getLView, resetComponentState, selectView} from '@angular/core/src/render3/state';
import {stringifyElement} from '@angular/platform-browser/testing/src/browser_util';

Expand Down Expand Up @@ -103,7 +103,7 @@ export class TemplateFixture extends BaseFixture {
private createBlock: () => void, private updateBlock: () => void = noop, decls: number = 0,
private vars: number = 0, directives?: DirectiveTypesOrFactory|null,
pipes?: PipeTypesOrFactory|null, sanitizer?: Sanitizer|null,
rendererFactory?: RendererFactory3, private _consts?: TAttributes[]) {
rendererFactory?: RendererFactory3, private _consts?: TConstants) {
super();
this._directiveDefs = toDefs(directives, extractDirectiveDef);
this._pipeDefs = toDefs(pipes, extractPipeDef);
Expand Down Expand Up @@ -249,7 +249,7 @@ export function renderTemplate<T>(
hostNode: RElement, templateFn: ComponentTemplate<T>, decls: number, vars: number, context: T,
providedRendererFactory: RendererFactory3, componentView: LView | null,
directives?: DirectiveDefListOrFactory | null, pipes?: PipeDefListOrFactory | null,
sanitizer?: Sanitizer | null, consts?: TAttributes[]): LView {
sanitizer?: Sanitizer | null, consts?: TConstants): LView {
if (componentView === null) {
resetComponentState();
const renderer = providedRendererFactory.createRenderer(null, null);
Expand Down Expand Up @@ -290,8 +290,7 @@ export function renderTemplate<T>(
export function renderToHtml(
template: ComponentTemplate<any>, ctx: any, decls: number = 0, vars: number = 0,
directives?: DirectiveTypesOrFactory | null, pipes?: PipeTypesOrFactory | null,
providedRendererFactory?: RendererFactory3 | null, keepNgReflect = false,
consts?: TAttributes[]) {
providedRendererFactory?: RendererFactory3 | null, keepNgReflect = false, consts?: TConstants) {
hostView = renderTemplate(
containerEl, template, decls, vars, ctx, providedRendererFactory || testRendererFactory,
hostView, toDefs(directives, extractDirectiveDef), toDefs(pipes, extractPipeDef), null,
Expand Down
6 changes: 3 additions & 3 deletions tools/public_api_guard/core/core.d.ts
Expand Up @@ -785,7 +785,7 @@ export declare function ɵɵdefineComponent<T>(componentDefinition: {
contentQueries?: ContentQueriesFunction<T>;
exportAs?: string[];
template: ComponentTemplate<T>;
consts?: (TAttributes | string)[];
consts?: TConstants;
ngContentSelectors?: string[];
viewQuery?: ViewQueriesFunction<T> | null;
features?: ComponentDefFeature[];
Expand Down Expand Up @@ -853,7 +853,7 @@ export declare function ɵɵdisableBindings(): void;

export declare function ɵɵelement(index: number, name: string, attrsIndex?: number | null, localRefsIndex?: number): void;

export declare function ɵɵelementContainer(index: number, constsIndex?: number | null, localRefsIndex?: number): void;
export declare function ɵɵelementContainer(index: number, attrsIndex?: number | null, localRefsIndex?: number): void;

export declare function ɵɵelementContainerEnd(): void;

Expand Down Expand Up @@ -1077,7 +1077,7 @@ export declare function ɵɵstylePropInterpolateV(prop: string, values: any[], v

export declare function ɵɵstyleSanitizer(sanitizer: StyleSanitizeFn | null): void;

export declare function ɵɵtemplate(index: number, templateFn: ComponentTemplate<any> | null, decls: number, vars: number, tagName?: string | null, constsIndex?: number | null, localRefsIndex?: number | null, localRefExtractor?: LocalRefExtractor): void;
export declare function ɵɵtemplate(index: number, templateFn: ComponentTemplate<any> | null, decls: number, vars: number, tagName?: string | null, attrsIndex?: number | null, localRefsIndex?: number | null, localRefExtractor?: LocalRefExtractor): void;

export declare function ɵɵtemplateRefExtractor(tNode: TNode, currentView: LView): ViewEngine_TemplateRef<unknown> | null;

Expand Down

0 comments on commit 2d2556c

Please sign in to comment.