perf(ivy): remove megamorphic read from renderStringify (#30082)

The `renderStringify` function is used in a lot of performance-sensitive places, however it contains a megamorphic read which is used primarily for error messages. These changes introduce a new function that can be used to stringify output for errors and removes the megamorphic read from `renderStringify`.

This PR resolves FW-1286.

PR Close #30082
This commit is contained in:
Kristiyan Kostadinov
2019-04-24 14:50:01 +02:00
committed by Andrew Kushnir
parent 24c61cb63e
commit 2e21997016
11 changed files with 57 additions and 39 deletions

View File

@ -15,7 +15,7 @@ import {LContext} from '../interfaces/context';
import {DirectiveDef} from '../interfaces/definition';
import {TElementNode, TNode, TNodeProviderIndexes} from '../interfaces/node';
import {CLEANUP, CONTEXT, FLAGS, HOST, LView, LViewFlags, TVIEW} from '../interfaces/view';
import {renderStringify} from './misc_utils';
import {stringifyForError} from './misc_utils';
import {getLViewParent, getRootContext} from './view_traversal_utils';
import {unwrapRNode} from './view_utils';
@ -191,7 +191,7 @@ export function loadLContext(target: {}, throwOnNotFound: boolean = true): LCont
const context = getLContext(target);
if (!context && throwOnNotFound) {
throw new Error(
ngDevMode ? `Unable to find context associated with ${renderStringify(target)}` :
ngDevMode ? `Unable to find context associated with ${stringifyForError(target)}` :
'Invalid ng target');
}
return context;

View File

@ -6,11 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {assertDefined} from '../../util/assert';
import {global} from '../../util/global';
import {RElement} from '../interfaces/renderer';
import {CONTEXT, LView, RootContext} from '../interfaces/view';
import {getRootView} from './view_traversal_utils';
/**
* Returns whether the values are different from a change detection stand point.
@ -25,17 +22,31 @@ export function isDifferent(a: any, b: any): boolean {
/**
* Used for stringify render output in Ivy.
* Important! This function is very performance-sensitive and we should
* be extra careful not to introduce megamorphic reads in it.
*/
export function renderStringify(value: any): string {
if (typeof value == 'function') return value.name || value;
if (typeof value == 'string') return value;
if (typeof value === 'function') return value.name || value;
if (typeof value === 'string') return value;
if (value == null) return '';
if (typeof value == 'object' && typeof value.type == 'function')
return value.type.name || value.type;
return '' + value;
}
/**
* Used to stringify a value so that it can be displayed in an error message.
* Important! This function contains a megamorphic read and should only be
* used for error messages.
*/
export function stringifyForError(value: any) {
if (typeof value === 'object' && value != null && typeof value.type === 'function') {
return value.type.name || value.type;
}
return renderStringify(value);
}
export const defaultScheduler =
(typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame || // browser only
setTimeout // everything else