From a295255e5828c2296d4b24360b8023274f6be780 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 2 Dec 2019 15:10:22 +0100 Subject: [PATCH] perf(ivy): avoid duplicate state lookup and default function parameters (#34183) Includes a few minor performance improvements: * In the `nextContext` instruction we assign a new LView to the `LFrame.contextLView` and then we immediately look it up to get its context. We don't need to do that since we know the view that was assigned already. * Removes the default value for the `level` parameter of `nextContextImpl` because it generates more code in es5 and is internal-only. * Removes the default parameter from `setActiveHostElement` since it generates extra code and it's an internal function. * Makes a check in `setElementExitFn` more strict since we're guaranteed for the value to match the type. PR Close #34183 --- packages/core/src/render3/state.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/core/src/render3/state.ts b/packages/core/src/render3/state.ts index ae3e3b5e46..3746c90c6c 100644 --- a/packages/core/src/render3/state.ts +++ b/packages/core/src/render3/state.ts @@ -302,7 +302,7 @@ function setActiveElementFlag(flag: ActiveElementFlags) { * @param elementIndex the element index value for the host element where * the directive/component instance lives */ -export function setActiveHostElement(elementIndex: number | null = null) { +export function setActiveHostElement(elementIndex: number | null) { if (hasActiveElementFlag(ActiveElementFlags.RunExitFn)) { executeElementExitFn(); } @@ -330,7 +330,7 @@ export function executeElementExitFn() { */ export function setElementExitFn(fn: () => void): void { setActiveElementFlag(ActiveElementFlags.RunExitFn); - if (instructionState.elementExitFn == null) { + if (instructionState.elementExitFn === null) { instructionState.elementExitFn = fn; } ngDevMode && @@ -592,9 +592,10 @@ export function leaveView() { instructionState.lFrame = instructionState.lFrame.parent; } -export function nextContextImpl(level: number = 1): T { - instructionState.lFrame.contextLView = walkUpViews(level, instructionState.lFrame.contextLView !); - return instructionState.lFrame.contextLView[CONTEXT] as T; +export function nextContextImpl(level: number): T { + const contextLView = instructionState.lFrame.contextLView = + walkUpViews(level, instructionState.lFrame.contextLView !); + return contextLView[CONTEXT] as T; } function walkUpViews(nestingLevel: number, currentView: LView): LView {