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:

committed by
Alex Rickabaugh

parent
9147092a15
commit
2deac0a412
30
packages/core/src/render3/styling/state.ts
Normal file
30
packages/core/src/render3/styling/state.ts
Normal 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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user