From bfef095dc4a790d8d0d30092cea36356ecd11774 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 16 Oct 2019 22:13:27 +0200 Subject: [PATCH] fixup! perf(ivy): move local references into consts array --- packages/core/src/render3/definition.ts | 9 ++++++--- .../core/src/render3/instructions/container.ts | 15 ++++++--------- packages/core/src/render3/instructions/element.ts | 9 +++------ .../src/render3/instructions/element_container.ts | 14 ++++++-------- .../core/src/render3/instructions/lview_debug.ts | 4 ++-- packages/core/src/render3/instructions/shared.ts | 6 +++--- .../core/src/render3/interfaces/definition.ts | 4 ++-- packages/core/src/render3/interfaces/node.ts | 7 +++++++ packages/core/src/render3/interfaces/view.ts | 8 ++++---- packages/core/src/render3/util/view_utils.ts | 7 ++++++- .../cyclic_import/bundle.golden_symbols.json | 3 +++ .../test/bundling/todo/bundle.golden_symbols.json | 3 +++ packages/core/test/render3/render_util.ts | 9 ++++----- tools/public_api_guard/core/core.d.ts | 6 +++--- 14 files changed, 58 insertions(+), 46 deletions(-) diff --git a/packages/core/src/render3/definition.ts b/packages/core/src/render3/definition.ts index 64d6b1ba77f1e..c0cd7d68f4569 100644 --- a/packages/core/src/render3/definition.ts +++ b/packages/core/src/render3/definition.ts @@ -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_LOCALE_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'; @@ -172,8 +172,11 @@ export function ɵɵdefineComponent(componentDefinition: { */ template: ComponentTemplate; - /** 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. diff --git a/packages/core/src/render3/instructions/container.ts b/packages/core/src/render3/instructions/container.ts index 4841d7307c3ab..831f213d76ed4 100644 --- a/packages/core/src/render3/instructions/container.ts +++ b/packages/core/src/render3/instructions/container.ts @@ -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'; @@ -56,8 +56,8 @@ 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. * @@ -65,7 +65,7 @@ export function ɵɵcontainer(index: number): void { */ export function ɵɵtemplate( index: number, templateFn: ComponentTemplate| 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]; @@ -73,11 +73,8 @@ export function ɵɵtemplate( // 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); diff --git a/packages/core/src/render3/instructions/element.ts b/packages/core/src/render3/instructions/element.ts index 3d600dafa7ea9..5d01d72235e1e 100644 --- a/packages/core/src/render3/instructions/element.ts +++ b/packages/core/src/render3/instructions/element.ts @@ -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} 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'; @@ -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, diff --git a/packages/core/src/render3/instructions/element_container.ts b/packages/core/src/render3/instructions/element_container.ts index 85ee585127991..c4aeb0c554567 100644 --- a/packages/core/src/render3/instructions/element_container.ts +++ b/packages/core/src/render3/instructions/element_container.ts @@ -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'; @@ -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'); @@ -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(); } diff --git a/packages/core/src/render3/instructions/lview_debug.ts b/packages/core/src/render3/instructions/lview_debug.ts index 754b8b97dce32..97fedf04b81a5 100644 --- a/packages/core/src/render3/instructions/lview_debug.ts +++ b/packages/core/src/render3/instructions/lview_debug.ts @@ -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'; @@ -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 { diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index 04490cae27afe..4095c561c7e97 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -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'; @@ -591,7 +591,7 @@ export function createTView( viewIndex: number, templateFn: ComponentTemplate| null, decls: number, vars: number, directives: DirectiveDefListOrFactory | null, pipes: PipeDefListOrFactory | null, viewQuery: ViewQueriesFunction| 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, @@ -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, diff --git a/packages/core/src/render3/interfaces/definition.ts b/packages/core/src/render3/interfaces/definition.ts index 245172fcf703a..b5270ecd54293 100644 --- a/packages/core/src/render3/interfaces/definition.ts +++ b/packages/core/src/render3/interfaces/definition.ts @@ -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'; @@ -244,7 +244,7 @@ export interface ComponentDef extends DirectiveDef { readonly template: ComponentTemplate; /** 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. diff --git a/packages/core/src/render3/interfaces/node.ts b/packages/core/src/render3/interfaces/node.ts index a598252390fbf..90f23b78da364 100644 --- a/packages/core/src/render3/interfaces/node.ts +++ b/packages/core/src/render3/interfaces/node.ts @@ -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. diff --git a/packages/core/src/render3/interfaces/view.ts b/packages/core/src/render3/interfaces/view.ts index fe15a7ee12f1e..9b15ae4b73234 100644 --- a/packages/core/src/render3/interfaces/view.ts +++ b/packages/core/src/render3/interfaces/view.ts @@ -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'; @@ -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} diff --git a/packages/core/src/render3/util/view_utils.ts b/packages/core/src/render3/util/view_utils.ts index 8de1909721536..a5c8f60d90281 100644 --- a/packages/core/src/render3/util/view_utils.ts +++ b/packages/core/src/render3/util/view_utils.ts @@ -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'; @@ -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 diff --git a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json index 1edbb3f222386..99a899aed2bf8 100644 --- a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json +++ b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json @@ -296,6 +296,9 @@ { "name": "getComponentViewByIndex" }, + { + "name": "getConstant" + }, { "name": "getContainerRenderParent" }, diff --git a/packages/core/test/bundling/todo/bundle.golden_symbols.json b/packages/core/test/bundling/todo/bundle.golden_symbols.json index 2466540dddbac..9885249f25306 100644 --- a/packages/core/test/bundling/todo/bundle.golden_symbols.json +++ b/packages/core/test/bundling/todo/bundle.golden_symbols.json @@ -638,6 +638,9 @@ { "name": "getConfig" }, + { + "name": "getConstant" + }, { "name": "getContainerRenderParent" }, diff --git a/packages/core/test/render3/render_util.ts b/packages/core/test/render3/render_util.ts index 108931f8b0175..48efa0efa9159 100644 --- a/packages/core/test/render3/render_util.ts +++ b/packages/core/test/render3/render_util.ts @@ -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'; @@ -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); @@ -249,7 +249,7 @@ export function renderTemplate( hostNode: RElement, templateFn: ComponentTemplate, 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); @@ -290,8 +290,7 @@ export function renderTemplate( export function renderToHtml( template: ComponentTemplate, 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, diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index 2ab1d563bc9a5..65da3df17e364 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -788,7 +788,7 @@ export declare function ɵɵdefineComponent(componentDefinition: { contentQueries?: ContentQueriesFunction; exportAs?: string[]; template: ComponentTemplate; - consts?: (TAttributes | string)[]; + consts?: TConstants; ngContentSelectors?: string[]; viewQuery?: ViewQueriesFunction | null; features?: ComponentDefFeature[]; @@ -856,7 +856,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; @@ -1080,7 +1080,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 | 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 | 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 | null;