perf(ivy): cache multiple reads to an element's stylingContext (#29818)

Because the styling context may be stored in a different location
and be apart of a sub array, reading the styling context each time
a host binding is evaluated is costly. This patch ensures that the
active styling context is cached so that multiple interactions with
styling bindings can easily retrieve the styling context efficiently.

PR Close #29818
This commit is contained in:
Matias Niemelä
2019-04-10 11:54:59 -07:00
committed by Alex Rickabaugh
parent 9147092a15
commit 2deac0a412
10 changed files with 102 additions and 29 deletions

View File

@ -0,0 +1,30 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {StylingContext} from '../interfaces/styling';
let stylingContext: StylingContext|null = null;
/**
* Gets the most recent styling context value.
*
* Note that only one styling context is stored at a given time.
*/
export function getCachedStylingContext() {
return stylingContext;
}
/**
* Sets the most recent styling context value.
*
* Note that only one styling context is stored at a given time.
*
* @param context The styling context value that will be stored
*/
export function setCachedStylingContext(context: StylingContext | null) {
stylingContext = context;
}

View File

@ -124,7 +124,7 @@ export function allocStylingContext(
* @param index Index of the style allocation. See: `elementStyling`.
* @param viewData The view to search for the styling context
*/
export function getStylingContext(index: number, viewData: LView): StylingContext {
export function getStylingContextFromLView(index: number, viewData: LView): StylingContext {
let storageIndex = index;
let slotValue: LContainer|LView|StylingContext|RElement = viewData[storageIndex];
let wrapper: LContainer|LView|StylingContext = viewData;
@ -252,7 +252,7 @@ export function getOrCreatePlayerContext(target: {}, context?: LContext | null):
}
const {lView, nodeIndex} = context;
const stylingContext = getStylingContext(nodeIndex, lView);
const stylingContext = getStylingContextFromLView(nodeIndex, lView);
return getPlayerContext(stylingContext) || allocPlayerContext(stylingContext);
}