perf(core): use native addEventListener for faster rendering. (#18107)

Angular can make many assumptions about its event handlers. As a result
the bookkeeping for native addEventListener is significantly cheaper
than Zone's addEventLister which can't make such assumptions.

This change bypasses the Zone's addEventListener if present and always
uses the native addEventHandler. As a result registering event listeners
is about 3 times faster.

PR Close #18107
This commit is contained in:
Miško Hevery 2017-07-13 15:36:11 -07:00 committed by Miško Hevery
parent 8bcb268140
commit 6279e50d78
11 changed files with 122 additions and 42 deletions

View File

@ -189,14 +189,7 @@ export function listenToElementOutputs(view: ViewData, compView: ViewData, def:
} }
function renderEventHandlerClosure(view: ViewData, index: number, eventName: string) { function renderEventHandlerClosure(view: ViewData, index: number, eventName: string) {
return (event: any) => { return (event: any) => dispatchEvent(view, index, eventName, event);
try {
return dispatchEvent(view, index, eventName, event);
} catch (e) {
// Attention: Don't rethrow, to keep in sync with directive events.
view.root.errorHandler.handleError(e);
}
}
} }

View File

@ -138,14 +138,7 @@ export function createDirectiveInstance(view: ViewData, def: NodeDef): any {
} }
function eventHandlerClosure(view: ViewData, index: number, eventName: string) { function eventHandlerClosure(view: ViewData, index: number, eventName: string) {
return (event: any) => { return (event: any) => dispatchEvent(view, index, eventName, event);
try {
return dispatchEvent(view, index, eventName, event);
} catch (e) {
// Attention: Don't rethrow, as it would cancel Observable subscriptions!
view.root.errorHandler.handleError(e);
}
}
} }
export function checkAndUpdateDirectiveInline( export function checkAndUpdateDirectiveInline(

View File

@ -126,12 +126,18 @@ export function markParentViewsForCheckProjectedViews(view: ViewData, endView: V
} }
export function dispatchEvent( export function dispatchEvent(
view: ViewData, nodeIndex: number, eventName: string, event: any): boolean { view: ViewData, nodeIndex: number, eventName: string, event: any): boolean|undefined {
const nodeDef = view.def.nodes[nodeIndex]; try {
const startView = const nodeDef = view.def.nodes[nodeIndex];
nodeDef.flags & NodeFlags.ComponentView ? asElementData(view, nodeIndex).componentView : view; const startView = nodeDef.flags & NodeFlags.ComponentView ?
markParentViewsForCheck(startView); asElementData(view, nodeIndex).componentView :
return Services.handleEvent(view, nodeIndex, eventName, event); view;
markParentViewsForCheck(startView);
return Services.handleEvent(view, nodeIndex, eventName, event);
} catch (e) {
// Attention: Don't rethrow, as it would cancel Observable subscriptions!
view.root.errorHandler.handleError(e);
}
} }
export function declaredViewContainer(view: ViewData): ElementData|null { export function declaredViewContainer(view: ViewData): ElementData|null {

View File

@ -164,13 +164,39 @@ export class NgZone {
* *
* If a synchronous error happens it will be rethrown and not reported via `onError`. * If a synchronous error happens it will be rethrown and not reported via `onError`.
*/ */
run(fn: () => any): any { return (this as any as NgZonePrivate)._inner.run(fn); } run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
return (this as any as NgZonePrivate)._inner.run(fn, applyThis, applyArgs) as T;
}
/**
* Executes the `fn` function synchronously within the Angular zone as a task and returns value
* returned by the function.
*
* Running functions via `run` allows you to reenter Angular zone from a task that was executed
* outside of the Angular zone (typically started via {@link #runOutsideAngular}).
*
* Any future tasks or microtasks scheduled from within this function will continue executing from
* within the Angular zone.
*
* If a synchronous error happens it will be rethrown and not reported via `onError`.
*/
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T {
const zone = (this as any as NgZonePrivate)._inner;
const task = zone.scheduleEventTask('NgZoneEvent: ' + name, fn, EMPTY_PAYLOAD, noop, noop);
try {
return zone.runTask(task, applyThis, applyArgs) as T;
} finally {
zone.cancelTask(task);
}
}
/** /**
* Same as `run`, except that synchronous errors are caught and forwarded via `onError` and not * Same as `run`, except that synchronous errors are caught and forwarded via `onError` and not
* rethrown. * rethrown.
*/ */
runGuarded(fn: () => any): any { return (this as any as NgZonePrivate)._inner.runGuarded(fn); } runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
return (this as any as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs) as T;
}
/** /**
* Executes the `fn` function synchronously in Angular's parent zone and returns value returned by * Executes the `fn` function synchronously in Angular's parent zone and returns value returned by
@ -185,9 +211,15 @@ export class NgZone {
* *
* Use {@link #run} to reenter the Angular zone and do work that updates the application model. * Use {@link #run} to reenter the Angular zone and do work that updates the application model.
*/ */
runOutsideAngular(fn: () => any): any { return (this as any as NgZonePrivate)._outer.run(fn); } runOutsideAngular<T>(fn: (...args: any[]) => T): T {
return (this as any as NgZonePrivate)._outer.run(fn) as T;
}
} }
function noop(){};
const EMPTY_PAYLOAD = {};
interface NgZonePrivate extends NgZone { interface NgZonePrivate extends NgZone {
_outer: Zone; _outer: Zone;
_inner: Zone; _inner: Zone;

View File

@ -12,6 +12,12 @@ import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {createRootView, isBrowser, recordNodeToRemove} from './helper'; import {createRootView, isBrowser, recordNodeToRemove} from './helper';
/**
* We map addEventListener to the Zones internal name. This is because we want to be fast
* and bypass the zone bookkeeping. We know that we can do the bookkeeping faster.
*/
const addEventListener = '__zone_symbol__addEventListener';
export function main() { export function main() {
describe(`Component Views`, () => { describe(`Component Views`, () => {
function compViewDef( function compViewDef(
@ -180,7 +186,7 @@ export function main() {
const update = jasmine.createSpy('updater'); const update = jasmine.createSpy('updater');
const addListenerSpy = spyOn(HTMLElement.prototype, 'addEventListener').and.callThrough(); const addListenerSpy = spyOn(HTMLElement.prototype, addEventListener).and.callThrough();
const {view} = createAndGetRootNodes(compViewDef( const {view} = createAndGetRootNodes(compViewDef(
[ [
@ -301,4 +307,4 @@ export function main() {
}); });
}); });
} }

View File

@ -14,6 +14,13 @@ import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {ARG_TYPE_VALUES, checkNodeInlineOrDynamic, createRootView, isBrowser, recordNodeToRemove} from './helper'; import {ARG_TYPE_VALUES, checkNodeInlineOrDynamic, createRootView, isBrowser, recordNodeToRemove} from './helper';
/**
* We map addEventListener to the Zones internal name. This is because we want to be fast
* and bypass the zone bookkeeping. We know that we can do the bookkeeping faster.
*/
const addEventListener = '__zone_symbol__addEventListener';
const removeEventListener = '__zone_symbol__removeEventListener';
export function main() { export function main() {
describe(`View Elements`, () => { describe(`View Elements`, () => {
function compViewDef( function compViewDef(
@ -190,7 +197,7 @@ export function main() {
it('should listen to DOM events', () => { it('should listen to DOM events', () => {
const handleEventSpy = jasmine.createSpy('handleEvent'); const handleEventSpy = jasmine.createSpy('handleEvent');
const removeListenerSpy = const removeListenerSpy =
spyOn(HTMLElement.prototype, 'removeEventListener').and.callThrough(); spyOn(HTMLElement.prototype, removeEventListener).and.callThrough();
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']], NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']],
handleEventSpy)])); handleEventSpy)]));
@ -210,8 +217,8 @@ export function main() {
it('should listen to window events', () => { it('should listen to window events', () => {
const handleEventSpy = jasmine.createSpy('handleEvent'); const handleEventSpy = jasmine.createSpy('handleEvent');
const addListenerSpy = spyOn(window, 'addEventListener'); const addListenerSpy = spyOn(window, addEventListener);
const removeListenerSpy = spyOn(window, 'removeEventListener'); const removeListenerSpy = spyOn(window, removeEventListener);
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
NodeFlags.None, null !, null !, 0, 'button', null !, null !, NodeFlags.None, null !, null !, 0, 'button', null !, null !,
[['window', 'windowClick']], handleEventSpy)])); [['window', 'windowClick']], handleEventSpy)]));
@ -233,8 +240,8 @@ export function main() {
it('should listen to document events', () => { it('should listen to document events', () => {
const handleEventSpy = jasmine.createSpy('handleEvent'); const handleEventSpy = jasmine.createSpy('handleEvent');
const addListenerSpy = spyOn(document, 'addEventListener'); const addListenerSpy = spyOn(document, addEventListener);
const removeListenerSpy = spyOn(document, 'removeEventListener'); const removeListenerSpy = spyOn(document, removeEventListener);
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
NodeFlags.None, null !, null !, 0, 'button', null !, null !, NodeFlags.None, null !, null !, 0, 'button', null !, null !,
[['document', 'documentClick']], handleEventSpy)])); [['document', 'documentClick']], handleEventSpy)]));
@ -284,7 +291,7 @@ export function main() {
it('should report debug info on event errors', () => { it('should report debug info on event errors', () => {
const handleErrorSpy = spyOn(TestBed.get(ErrorHandler), 'handleError'); const handleErrorSpy = spyOn(TestBed.get(ErrorHandler), 'handleError');
const addListenerSpy = spyOn(HTMLElement.prototype, 'addEventListener').and.callThrough(); const addListenerSpy = spyOn(HTMLElement.prototype, addEventListener).and.callThrough();
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']], NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']],
() => { throw new Error('Test'); })])); () => { throw new Error('Test'); })]));

View File

@ -220,6 +220,13 @@ function commonTests() {
macroTask(() => { async.done(); }); macroTask(() => { async.done(); });
}), testTimeout); }), testTimeout);
it('should return the body return value from runTask',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
macroTask(() => { expect(_zone.runTask(() => 6)).toEqual(6); });
macroTask(() => { async.done(); });
}), testTimeout);
it('should call onUnstable and onMicrotaskEmpty', it('should call onUnstable and onMicrotaskEmpty',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
runNgZoneNoLog(() => macroTask(_log.fn('run'))); runNgZoneNoLog(() => macroTask(_log.fn('run')));

View File

@ -6,22 +6,54 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Inject, Injectable} from '@angular/core'; import {Inject, Injectable, NgZone, ɵglobal as global} from '@angular/core';
import {DOCUMENT} from '../dom_tokens'; import {DOCUMENT} from '../dom_tokens';
import {EventManagerPlugin} from './event_manager'; import {EventManagerPlugin} from './event_manager';
/**
* Detect if Zone is present. If it is then bypass 'addEventListener' since Angular can do much more
* efficient bookkeeping than Zone can, because we have additional information. This speeds up
* addEventListener by 3x.
*/
const Zone = global['Zone'];
const __symbol__ = Zone && Zone['__symbol__'] || function<T>(v: T): T {
return v;
};
const ADD_EVENT_LISTENER: 'addEventListener' = __symbol__('addEventListener');
const REMOVE_EVENT_LISTENER: 'removeEventListener' = __symbol__('removeEventListener');
@Injectable() @Injectable()
export class DomEventsPlugin extends EventManagerPlugin { export class DomEventsPlugin extends EventManagerPlugin {
constructor(@Inject(DOCUMENT) doc: any) { super(doc); } constructor(@Inject(DOCUMENT) doc: any, private ngZone: NgZone) { super(doc); }
// This plugin should come last in the list of plugins, because it accepts all // This plugin should come last in the list of plugins, because it accepts all
// events. // events.
supports(eventName: string): boolean { return true; } supports(eventName: string): boolean { return true; }
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
element.addEventListener(eventName, handler as any, false); /**
return () => element.removeEventListener(eventName, handler as any, false); * This code is about to add a listener to the DOM. If Zone.js is present, than
* `addEventListener` has been patched. The patched code adds overhead in both
* memory and speed (3x slower) than native. For this reason if we detect that
* Zone.js is present we bypass zone and use native addEventListener instead.
* The result is faster registration but the zone will not be restored. We do
* manual zone restoration in element.ts renderEventHandlerClosure method.
*
* NOTE: it is possible that the element is from different iframe, and so we
* have to check before we execute the method.
*/
const self = this;
let byPassZoneJS = element[ADD_EVENT_LISTENER];
let callback: EventListener = handler as EventListener;
if (byPassZoneJS) {
callback = function() {
return self.ngZone.runTask(handler as any, null, arguments as any, eventName);
};
}
element[byPassZoneJS ? ADD_EVENT_LISTENER : 'addEventListener'](eventName, callback, false);
return () => element[byPassZoneJS ? REMOVE_EVENT_LISTENER : 'removeEventListener'](
eventName, callback as any, false);
} }
} }

View File

@ -16,12 +16,14 @@ import {el} from '../../../testing/src/browser_util';
export function main() { export function main() {
let domEventPlugin: DomEventsPlugin; let domEventPlugin: DomEventsPlugin;
let doc: any; let doc: any;
let zone: NgZone;
describe('EventManager', () => { describe('EventManager', () => {
beforeEach(() => { beforeEach(() => {
doc = getDOM().supportsDOMEvents() ? document : getDOM().createHtmlDocument(); doc = getDOM().supportsDOMEvents() ? document : getDOM().createHtmlDocument();
domEventPlugin = new DomEventsPlugin(doc); zone = new NgZone({});
domEventPlugin = new DomEventsPlugin(doc, zone);
}); });
it('should delegate event bindings to plugins that are passed in from the most generic one to the most specific one', it('should delegate event bindings to plugins that are passed in from the most generic one to the most specific one',

View File

@ -172,7 +172,8 @@ class DefaultServerRenderer2 implements Renderer2 {
const el = const el =
typeof target === 'string' ? getDOM().getGlobalEventTarget(this.document, target) : target; typeof target === 'string' ? getDOM().getGlobalEventTarget(this.document, target) : target;
const outsideHandler = (event: any) => this.ngZone.runGuarded(() => callback(event)); const outsideHandler = (event: any) => this.ngZone.runGuarded(() => callback(event));
return this.ngZone.runOutsideAngular(() => getDOM().onAndCancel(el, eventName, outsideHandler)); return this.ngZone.runOutsideAngular(
() => getDOM().onAndCancel(el, eventName, outsideHandler) as any);
} }
} }

View File

@ -650,9 +650,10 @@ export declare class NgZone {
constructor({enableLongStackTrace}: { constructor({enableLongStackTrace}: {
enableLongStackTrace?: boolean; enableLongStackTrace?: boolean;
}); });
run(fn: () => any): any; run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T;
runGuarded(fn: () => any): any; runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T;
runOutsideAngular(fn: () => any): any; runOutsideAngular<T>(fn: (...args: any[]) => T): T;
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T;
static assertInAngularZone(): void; static assertInAngularZone(): void;
static assertNotInAngularZone(): void; static assertNotInAngularZone(): void;
static isInAngularZone(): boolean; static isInAngularZone(): boolean;