Skip to content

Commit

Permalink
perf(ivy): run registerPostOrderHooks in the first template pass only (
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource authored and sabeersulaiman committed Sep 6, 2019
1 parent cba616a commit cac6f44
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
53 changes: 25 additions & 28 deletions packages/core/src/render3/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {assertEqual, assertNotEqual} from '../util/assert';

import {assertFirstTemplatePass} from './assert';
import {DirectiveDef} from './interfaces/definition';
import {TNode} from './interfaces/node';
import {FLAGS, HookData, InitPhaseState, LView, LViewFlags, PREORDER_HOOK_FLAGS, PreOrderHookFlags, TView} from './interfaces/view';
Expand Down Expand Up @@ -35,9 +36,7 @@ import {getCheckNoChangesMode} from './state';
export function registerPreOrderHooks(
directiveIndex: number, directiveDef: DirectiveDef<any>, tView: TView, nodeIndex: number,
initialPreOrderHooksLength: number, initialPreOrderCheckHooksLength: number): void {
ngDevMode &&
assertEqual(tView.firstTemplatePass, true, 'Should only be called on first template pass');

ngDevMode && assertFirstTemplatePass(tView);
const {onChanges, onInit, doCheck} = directiveDef;
if (initialPreOrderHooksLength >= 0 &&
(!tView.preOrderHooks || initialPreOrderHooksLength === tView.preOrderHooks.length) &&
Expand Down Expand Up @@ -86,35 +85,33 @@ export function registerPreOrderHooks(
* @param tNode The TNode whose directives are to be searched for hooks to queue
*/
export function registerPostOrderHooks(tView: TView, tNode: TNode): void {
if (tView.firstTemplatePass) {
// It's necessary to loop through the directives at elementEnd() (rather than processing in
// directiveCreate) so we can preserve the current hook order. Content, view, and destroy
// hooks for projected components and directives must be called *before* their hosts.
for (let i = tNode.directiveStart, end = tNode.directiveEnd; i < end; i++) {
const directiveDef = tView.data[i] as DirectiveDef<any>;
if (directiveDef.afterContentInit) {
(tView.contentHooks || (tView.contentHooks = [])).push(-i, directiveDef.afterContentInit);
}
ngDevMode && assertFirstTemplatePass(tView);
// It's necessary to loop through the directives at elementEnd() (rather than processing in
// directiveCreate) so we can preserve the current hook order. Content, view, and destroy
// hooks for projected components and directives must be called *before* their hosts.
for (let i = tNode.directiveStart, end = tNode.directiveEnd; i < end; i++) {
const directiveDef = tView.data[i] as DirectiveDef<any>;
if (directiveDef.afterContentInit) {
(tView.contentHooks || (tView.contentHooks = [])).push(-i, directiveDef.afterContentInit);
}

if (directiveDef.afterContentChecked) {
(tView.contentHooks || (tView.contentHooks = [])).push(i, directiveDef.afterContentChecked);
(tView.contentCheckHooks || (tView.contentCheckHooks = [
])).push(i, directiveDef.afterContentChecked);
}
if (directiveDef.afterContentChecked) {
(tView.contentHooks || (tView.contentHooks = [])).push(i, directiveDef.afterContentChecked);
(tView.contentCheckHooks || (tView.contentCheckHooks = [
])).push(i, directiveDef.afterContentChecked);
}

if (directiveDef.afterViewInit) {
(tView.viewHooks || (tView.viewHooks = [])).push(-i, directiveDef.afterViewInit);
}
if (directiveDef.afterViewInit) {
(tView.viewHooks || (tView.viewHooks = [])).push(-i, directiveDef.afterViewInit);
}

if (directiveDef.afterViewChecked) {
(tView.viewHooks || (tView.viewHooks = [])).push(i, directiveDef.afterViewChecked);
(tView.viewCheckHooks || (tView.viewCheckHooks = [
])).push(i, directiveDef.afterViewChecked);
}
if (directiveDef.afterViewChecked) {
(tView.viewHooks || (tView.viewHooks = [])).push(i, directiveDef.afterViewChecked);
(tView.viewCheckHooks || (tView.viewCheckHooks = [])).push(i, directiveDef.afterViewChecked);
}

if (directiveDef.onDestroy != null) {
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, directiveDef.onDestroy);
}
if (directiveDef.onDestroy != null) {
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, directiveDef.onDestroy);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render3/instructions/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function ɵɵtemplate(
if (tView.firstTemplatePass) {
ngDevMode && ngDevMode.firstTemplatePass++;
resolveDirectives(tView, lView, tContainerNode, localRefs || null);
registerPostOrderHooks(tView, tContainerNode);

const embeddedTView = tContainerNode.tViews = createTView(
-1, templateFn, consts, vars, tView.directiveRegistry, tView.pipeRegistry, null,
Expand All @@ -90,7 +91,6 @@ export function ɵɵtemplate(

createDirectivesAndLocals(tView, lView, tContainerNode, localRefExtractor);
attachPatchData(getNativeByTNode(tContainerNode, lView), lView);
registerPostOrderHooks(tView, tContainerNode);
setIsNotParent();
}

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/render3/instructions/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ export function ɵɵelementEnd(): void {
const lView = getLView();
const tView = lView[TVIEW];

registerPostOrderHooks(tView, previousOrParentTNode);
decreaseElementDepthCount();

if (tView.firstTemplatePass && tView.queries !== null &&
isContentQueryHost(previousOrParentTNode)) {
tView.queries !.elementEnd(previousOrParentTNode);
if (tView.firstTemplatePass) {
registerPostOrderHooks(tView, previousOrParentTNode);
if (isContentQueryHost(previousOrParentTNode)) {
tView.queries !.elementEnd(previousOrParentTNode);
}
}

if (hasClassInput(tNode) && tNode.classes) {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/render3/instructions/element_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export function ɵɵelementContainerEnd(): void {

ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.ElementContainer);

registerPostOrderHooks(tView, previousOrParentTNode);

if (tView.firstTemplatePass && tView.queries !== null &&
isContentQueryHost(previousOrParentTNode)) {
tView.queries.elementEnd(previousOrParentTNode);
if (tView.firstTemplatePass) {
registerPostOrderHooks(tView, previousOrParentTNode);
if (isContentQueryHost(previousOrParentTNode)) {
tView.queries !.elementEnd(previousOrParentTNode);
}
}
}

Expand Down

0 comments on commit cac6f44

Please sign in to comment.