Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

Projects
None yet
6 participants
@mhevery
Copy link
Member

mhevery commented Jul 13, 2017

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.

@mhevery mhevery requested a review from IgorMinar Jul 13, 2017

@googlebot googlebot added the cla: yes label Jul 13, 2017

@IgorMinar
Copy link
Member

IgorMinar left a comment

I'm not familiar with all the internals that might be affected by this change, but on the surface this looks right. We should however not merge this until after master is open for 5.0 changes due to the slight public api change.

element.addEventListener(eventName, handler as any, false);
return () => element.removeEventListener(eventName, handler as any, false);
element[ADD_EVENT_LISTENER](eventName, handler as any, false);
return () => element[REMOVE_EVENT_LISTENER](eventName, handler as any, false);

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Jul 14, 2017

Member

does this call into the native addEventListener? can you document that? it's not clear from this code what's going on here.

This comment has been minimized.

Copy link
@mhevery

mhevery Jul 14, 2017

Author Member

added docs

@@ -185,7 +189,9 @@ export class NgZone {
*
* 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): any {

This comment has been minimized.

Copy link
@gkalpak

gkalpak Jul 14, 2017

Member

Shouldn't the return type for runOutsideAngular be T (instead of any)?

This comment has been minimized.

Copy link
@mhevery

mhevery Jul 14, 2017

Author Member

good catch, fixed


import {DOCUMENT} from '../dom_tokens';

import {EventManagerPlugin} from './event_manager';

/**
* Detect if Zone is present. If it is than bypass 'addEventListener' since Angular can do much more

This comment has been minimized.

Copy link
@gkalpak

gkalpak Jul 14, 2017

Member

than --> then

This comment has been minimized.

Copy link
@mhevery

mhevery Jul 14, 2017

Author Member

fixed

@mhevery mhevery force-pushed the mhevery:direct_addEventListener branch 3 times, most recently from fa2ef62 to c64a586 Jul 14, 2017

@mhevery mhevery force-pushed the mhevery:direct_addEventListener branch 2 times, most recently from 771dca7 to fcc7096 Jul 17, 2017

@mhevery mhevery force-pushed the angular:master branch from 08b36cb to 5d4b36f Jul 20, 2017

@mhevery mhevery force-pushed the mhevery:direct_addEventListener branch from fcc7096 to 3421dca Jul 21, 2017

@pkozlowski-opensource

This comment has been minimized.

Copy link
Member

pkozlowski-opensource commented Jul 24, 2017

@mhevery I took your PR for a test drive and here are the results I'm seeing on a sample app I'm using for Angular perf testing

Before

    forcedGcAmount |       forcedGcTime |           gcAmount |             gcTime |        majorGcTime |     pureScriptTime |         renderTime |         scriptTime
------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------
           2676.10 |               8.41 |             872.74 |              11.67 |               0.02 |              71.60 |              91.39 |              71.60
           3705.25 |               8.55 |               0.00 |               0.00 |               0.00 |              56.81 |              35.62 |              56.81
           3708.75 |               9.11 |               0.00 |               0.00 |               0.00 |              55.84 |              36.73 |              55.84
           3708.82 |               8.78 |               0.00 |               0.00 |               0.00 |              51.68 |              37.19 |              51.68
           3684.94 |               8.76 |               0.00 |               0.00 |               0.00 |              49.14 |              37.37 |              49.14
           3673.35 |               8.53 |               0.00 |               0.00 |               0.00 |              46.35 |              39.38 |              46.35
           3679.52 |               9.02 |               0.00 |               0.00 |               0.00 |              41.30 |              35.53 |              41.30
           3679.25 |               9.30 |               0.00 |               0.00 |               0.00 |              45.88 |              39.32 |              45.88
           3670.77 |               8.85 |               0.00 |               0.00 |               0.00 |              43.36 |              39.14 |              43.36
           3676.83 |               9.41 |               0.00 |               0.00 |               0.00 |              48.45 |              37.52 |              48.45
           3689.84 |               8.69 |               0.00 |               0.00 |               0.00 |              41.51 |              35.74 |              41.51
           3671.09 |               9.96 |               0.00 |               0.00 |               0.00 |              42.25 |              40.35 |              42.25
           3672.90 |               8.95 |               0.00 |               0.00 |               0.00 |              39.64 |              36.83 |              39.64
           3671.46 |               9.22 |               0.00 |               0.00 |               0.00 |              40.46 |              37.68 |              40.46
           3671.88 |               9.86 |               0.00 |               0.00 |               0.00 |              47.25 |              35.60 |              47.25
           3755.75 |               9.31 |               0.00 |               0.00 |               0.00 |              42.86 |              37.03 |              42.86
           3683.50 |               8.75 |               0.00 |               0.00 |               0.00 |              42.47 |              36.47 |              42.47
           3679.86 |               8.78 |               0.00 |               0.00 |               0.00 |              43.96 |              35.75 |              43.96
           3674.36 |               9.70 |               0.00 |               0.00 |               0.00 |              43.65 |              37.52 |              43.65
           3704.49 |               8.99 |               0.00 |               0.00 |               0.00 |              48.58 |              37.95 |              48.58
           3698.16 |               9.06 |               0.00 |               0.00 |               0.00 |              37.38 |              38.54 |              37.38
           3698.74 |               8.95 |               0.00 |               0.00 |               0.00 |              36.66 |              36.14 |              36.66
           3670.64 |               8.85 |               0.00 |               0.00 |               0.00 |              38.47 |              36.94 |              38.47
           3668.05 |               9.74 |               0.00 |               0.00 |               0.00 |              40.27 |              36.95 |              40.27
           3671.99 |               8.97 |               0.00 |               0.00 |               0.00 |              42.87 |              37.48 |              42.87
           3699.13 |               9.03 |               0.00 |               0.00 |               0.00 |              37.77 |              38.46 |              37.77
           3676.77 |               9.46 |               0.00 |               0.00 |               0.00 |              41.97 |              38.68 |              41.97
           3667.91 |               8.71 |               0.00 |               0.00 |               0.00 |              39.03 |              37.99 |              39.03
           3670.27 |               9.44 |               0.00 |               0.00 |               0.00 |              38.46 |              35.69 |              38.46
           3683.33 |               9.19 |               0.00 |               0.00 |               0.00 |              45.79 |              39.78 |              45.79
           3670.22 |               8.94 |               0.00 |               0.00 |               0.00 |              41.23 |              36.73 |              41.23
           3671.99 |               9.39 |               0.00 |               0.00 |               0.00 |              37.10 |              34.74 |              37.10
           3689.15 |               9.08 |               0.00 |               0.00 |               0.00 |              41.51 |              36.92 |              41.51
           3671.80 |              10.12 |               0.00 |               0.00 |               0.00 |              40.93 |              37.88 |              40.93
           3691.26 |               9.07 |               0.00 |               0.00 |               0.00 |              45.13 |              37.45 |              45.13
           3671.79 |               9.32 |               0.00 |               0.00 |               0.00 |              38.22 |              37.63 |              38.22
           3682.98 |              10.37 |               0.00 |               0.00 |               0.00 |              39.54 |              38.35 |              39.54
           3671.85 |               9.56 |               0.00 |               0.00 |               0.00 |              37.81 |              36.16 |              37.81
           3670.47 |               8.75 |               0.00 |               0.00 |               0.00 |              36.87 |              37.66 |              36.87
           3672.69 |               9.03 |               0.00 |               0.00 |               0.00 |              40.56 |              37.57 |              40.56
================== | ================== | ================== | ================== | ================== | ================== | ================== | ==================
       3678.46+-0% |           9.25+-4% |               0.00 |               0.00 |               0.00 |          39.88+-6% |          37.39+-3% |          39.88+-6%

After

    forcedGcAmount |       forcedGcTime |           gcAmount |             gcTime |        majorGcTime |     pureScriptTime |         renderTime |         scriptTime
------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------
           2812.88 |               8.83 |             578.60 |               7.21 |               0.01 |              65.32 |              90.59 |              65.32
           3402.45 |               7.76 |               0.00 |               0.00 |               0.00 |              45.42 |              45.07 |              45.42
           3395.38 |               7.88 |               0.00 |               0.00 |               0.00 |              47.95 |              40.34 |              47.95
           3394.66 |               8.05 |               0.00 |               0.00 |               0.00 |              43.83 |              39.74 |              43.83
           3383.54 |               7.42 |               0.00 |               0.00 |               0.00 |              43.48 |              41.66 |              43.48
           3412.25 |               8.04 |               0.00 |               0.00 |               0.00 |              35.66 |              46.02 |              35.66
           3407.68 |               7.65 |               0.00 |               0.00 |               0.00 |              35.58 |              40.26 |              35.58
           3350.00 |               7.79 |               0.00 |               0.00 |               0.00 |              35.76 |              40.27 |              35.76
           3379.45 |               8.00 |               0.00 |               0.00 |               0.00 |              36.46 |              41.55 |              36.46
           3381.25 |               7.49 |               0.00 |               0.00 |               0.00 |              37.37 |              42.98 |              37.37
           3429.42 |               8.22 |               0.00 |               0.00 |               0.00 |              37.92 |              45.74 |              37.92
           3412.35 |               7.76 |               0.00 |               0.00 |               0.00 |              37.41 |              43.23 |              37.41
           3379.58 |               8.22 |               0.00 |               0.00 |               0.00 |              36.45 |              40.01 |              36.45
           3381.26 |               7.47 |               0.00 |               0.00 |               0.00 |              36.24 |              43.47 |              36.24
           3447.26 |               7.58 |               0.00 |               0.00 |               0.00 |              38.01 |              40.25 |              38.01
           3408.81 |               8.03 |               0.00 |               0.00 |               0.00 |              39.08 |              42.54 |              39.08
           3379.55 |               8.10 |               0.00 |               0.00 |               0.00 |              33.11 |              39.35 |              33.11
           3445.27 |               8.13 |               0.00 |               0.00 |               0.00 |              36.65 |              42.82 |              36.65
           3378.26 |               7.58 |               0.00 |               0.00 |               0.00 |              34.50 |              45.67 |              34.50
           3413.91 |               8.13 |               0.00 |               0.00 |               0.00 |              36.28 |              39.87 |              36.28
           3407.18 |               7.51 |               0.00 |               0.00 |               0.00 |              35.92 |              42.88 |              35.92
           3458.65 |              10.16 |               0.00 |               0.00 |               0.00 |              35.43 |              42.58 |              35.43
           3406.42 |               8.06 |               0.00 |               0.00 |               0.00 |              32.37 |              45.27 |              32.37
           3411.88 |               8.25 |               0.00 |               0.00 |               0.00 |              32.19 |              40.96 |              32.19
           3444.86 |               8.04 |               0.00 |               0.00 |               0.00 |              41.09 |              41.79 |              41.09
           3409.96 |               8.10 |               0.00 |               0.00 |               0.00 |              35.62 |              42.80 |              35.62
           3407.70 |               8.41 |               0.00 |               0.00 |               0.00 |              36.81 |              39.93 |              36.81
           3379.26 |               7.76 |               0.00 |               0.00 |               0.00 |              35.67 |              39.57 |              35.67
           3407.00 |               7.73 |               0.00 |               0.00 |               0.00 |              32.36 |              41.32 |              32.36
           3408.05 |               7.60 |               0.00 |               0.00 |               0.00 |              31.63 |              40.82 |              31.63
           3444.38 |               7.68 |               0.00 |               0.00 |               0.00 |              33.38 |              41.57 |              33.38
           3380.33 |               8.17 |               0.00 |               0.00 |               0.00 |              32.47 |              40.37 |              32.47
           3457.75 |               7.78 |               0.00 |               0.00 |               0.00 |              32.74 |              40.91 |              32.74
           3380.37 |               7.79 |               0.00 |               0.00 |               0.00 |              32.69 |              41.71 |              32.69
           3407.41 |               8.28 |               0.00 |               0.00 |               0.00 |              34.94 |              46.78 |              34.94
           3412.78 |               8.23 |               0.00 |               0.00 |               0.00 |              31.67 |              41.03 |              31.67
           3378.26 |               8.50 |               0.00 |               0.00 |               0.00 |              31.17 |              43.39 |              31.17
           3379.11 |               7.53 |               0.00 |               0.00 |               0.00 |              33.66 |              41.54 |              33.66
           3378.88 |               7.81 |               0.00 |               0.00 |               0.00 |              31.42 |              40.04 |              31.42
           3379.90 |               8.50 |               0.00 |               0.00 |               0.00 |              31.97 |              41.34 |              31.97
           3378.83 |               7.62 |               0.00 |               0.00 |               0.00 |              32.84 |              43.45 |              32.84
           3373.49 |               7.92 |               0.00 |               0.00 |               0.00 |              33.40 |              40.13 |              33.40
           3438.07 |               7.58 |               0.00 |               0.00 |               0.00 |              33.63 |              44.41 |              33.63
           3406.49 |               7.75 |               0.00 |               0.00 |               0.00 |              30.98 |              41.31 |              30.98
           3424.59 |               7.66 |               0.00 |               0.00 |               0.00 |              35.31 |              41.00 |              35.31
           3444.38 |               8.08 |               0.00 |               0.00 |               0.00 |              34.83 |              42.87 |              34.83
           3372.10 |               7.65 |               0.00 |               0.00 |               0.00 |              33.93 |              42.65 |              33.93
================== | ================== | ================== | ================== | ================== | ================== | ================== | ==================
       3401.57+-0% |           7.88+-3% |               0.00 |               0.00 |               0.00 |          33.03+-4% |          41.81+-3% |          33.03+-4%

Summary

  • pureScript time goes noticeably down, both before browser optimizes and after TurbFan did its magic (optimised version speedup is even more spectacular)
  • for reasons I'm having hard to explain the render time goes up

As always there is a bit of fluctuation from one test to another so to draw definitive conclusions we should run benchmarks in the "laboratory" conditions.

@mhevery mhevery force-pushed the mhevery:direct_addEventListener branch from 3421dca to 5e47f6e Jul 24, 2017

perf(core): use native addEventListener for faster rendering.
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.

@mhevery mhevery force-pushed the mhevery:direct_addEventListener branch from 5e47f6e to d14d401 Jul 25, 2017

@mhevery mhevery closed this in 6279e50 Jul 25, 2017

asnowwolf added a commit to asnowwolf/angular that referenced this pull request Aug 11, 2017

perf(core): use native addEventListener for faster rendering. (angula…
…r#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 angular#18107

juleskremer added a commit to juleskremer/angular that referenced this pull request Aug 26, 2017

perf(core): use native addEventListener for faster rendering. (angula…
…r#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 angular#18107

juleskremer added a commit to juleskremer/angular that referenced this pull request Aug 28, 2017

perf(core): use native addEventListener for faster rendering. (angula…
…r#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 angular#18107

@mhevery mhevery deleted the mhevery:direct_addEventListener branch Feb 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.