From b613f90146cbdd030ce31bb1da8ce1b2b77f1a76 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 21 May 2019 12:04:17 +0200 Subject: [PATCH] refactor(ivy): avoid calling query instructions from static query instructions (#30587) PR Close #30587 --- packages/core/src/render3/query.ts | 49 ++++++++++++++++++------------ 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/core/src/render3/query.ts b/packages/core/src/render3/query.ts index dd918278b3..1b2daecea4 100644 --- a/packages/core/src/render3/query.ts +++ b/packages/core/src/render3/query.ts @@ -24,7 +24,7 @@ import {unusedValueExportToPlacateAjd as unused1} from './interfaces/definition' import {unusedValueExportToPlacateAjd as unused2} from './interfaces/injector'; import {TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeType, unusedValueExportToPlacateAjd as unused3} from './interfaces/node'; import {LQueries, unusedValueExportToPlacateAjd as unused4} from './interfaces/query'; -import {CONTENT_QUERIES, HEADER_OFFSET, LView, QUERIES, TVIEW} from './interfaces/view'; +import {CONTENT_QUERIES, HEADER_OFFSET, LView, QUERIES, TVIEW, TView} from './interfaces/view'; import {getCurrentQueryIndex, getIsParent, getLView, isCreationMode, setCurrentQueryIndex} from './state'; import {loadInternal} from './util/view_utils'; import {createElementRef, createTemplateRef} from './view_engine_compatibility'; @@ -370,12 +370,13 @@ type QueryList_ = QueryList& {_valuesTree: any[], _static: boolean}; */ function query( // TODO: "read" should be an AbstractType (FW-486) - lView: LView, predicate: Type| string[], descend: boolean, read: any): QueryList { + lView: LView, predicate: Type| string[], descend: boolean, read: any, + isStatic: boolean): QueryList { ngDevMode && assertPreviousIsParent(getIsParent()); const queryList = new QueryList() as QueryList_; const queries = lView[QUERIES] || (lView[QUERIES] = new LQueries_(null, null, null)); queryList._valuesTree = []; - queryList._static = false; + queryList._static = isStatic; queries.track(queryList, predicate, descend, read); storeCleanupWithContext(lView, queryList, queryList.destroy); return queryList; @@ -415,12 +416,10 @@ export function ɵɵqueryRefresh(queryList: QueryList): boolean { export function ɵɵstaticViewQuery( // TODO(FW-486): "read" should be an AbstractType predicate: Type| string[], descend: boolean, read: any): void { - const queryList = ɵɵviewQuery(predicate, descend, read) as QueryList_; - const tView = getLView()[TVIEW]; - queryList._static = true; - if (!tView.staticViewQueries) { - tView.staticViewQueries = true; - } + const lView = getLView(); + const tView = lView[TVIEW]; + viewQueryInternal(lView, tView, predicate, descend, read, true); + tView.staticViewQueries = true; } /** @@ -438,14 +437,20 @@ export function ɵɵviewQuery( predicate: Type| string[], descend: boolean, read: any): QueryList { const lView = getLView(); const tView = lView[TVIEW]; + return viewQueryInternal(lView, tView, predicate, descend, read, false); +} + +function viewQueryInternal( + lView: LView, tView: TView, predicate: Type| string[], descend: boolean, read: any, + isStatic: boolean): QueryList { if (tView.firstTemplatePass) { tView.expandoStartIndex++; } const index = getCurrentQueryIndex(); - const viewQuery: QueryList = query(lView, predicate, descend, read); - store(index - HEADER_OFFSET, viewQuery); + const queryList: QueryList = query(lView, predicate, descend, read, isStatic); + store(index - HEADER_OFFSET, queryList); setCurrentQueryIndex(index + 1); - return viewQuery; + return queryList; } /** @@ -477,7 +482,15 @@ export function ɵɵcontentQuery( read: any): QueryList { const lView = getLView(); const tView = lView[TVIEW]; - const contentQuery: QueryList = query(lView, predicate, descend, read); + return contentQueryInternal(lView, tView, directiveIndex, predicate, descend, read, false); +} + +function contentQueryInternal( + lView: LView, tView: TView, directiveIndex: number, predicate: Type| string[], + descend: boolean, + // TODO(FW-486): "read" should be an AbstractType + read: any, isStatic: boolean): QueryList { + const contentQuery: QueryList = query(lView, predicate, descend, read, isStatic); (lView[CONTENT_QUERIES] || (lView[CONTENT_QUERIES] = [])).push(contentQuery); if (tView.firstTemplatePass) { const tViewContentQueries = tView.contentQueries || (tView.contentQueries = []); @@ -506,12 +519,10 @@ export function ɵɵstaticContentQuery( directiveIndex: number, predicate: Type| string[], descend: boolean, // TODO(FW-486): "read" should be an AbstractType read: any): void { - const queryList = ɵɵcontentQuery(directiveIndex, predicate, descend, read) as QueryList_; - const tView = getLView()[TVIEW]; - queryList._static = true; - if (!tView.staticContentQueries) { - tView.staticContentQueries = true; - } + const lView = getLView(); + const tView = lView[TVIEW]; + contentQueryInternal(lView, tView, directiveIndex, predicate, descend, read, true); + tView.staticContentQueries = true; } /**