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

feat: add a flag in bootstrap to enable coalesce event to improve performance #30533

Closed

Conversation

JiaLiPassion
Copy link
Contributor

@JiaLiPassion JiaLiPassion commented May 17, 2019

add a new flag in BootstrapOptions to enable coalesce event to improve performance.

/**
 * Provides additional options to the bootstraping process.
 *
 *
 */
export interface BootstrapOptions {
  /**
   * Optionally specify which `NgZone` should be used.
   *
   * - Provide your own `NgZone` instance.
   * - `zone.js` - Use default `NgZone` which requires `Zone.js`.
   * - `noop` - Use `NoopNgZone` which does nothing.
   */
  ngZone?: NgZone|'zone.js'|'noop';
  
  // START<<<<<<<<<
  /**
   * Optionally specify if `NgZone` should be configure to coalesce
   * events.
   */
  ngZoneEventCoalescing?: true|false;
  // END<<<<<<<<<<<
}

The use case is something like this,

<div (click)="doSomethingElse()">
  <button (click)="doSomething()">click me</button>
</div>

It turns out that as the click event bubbles from the towards the root document it triggers change detection on each listener. This example shows the phenomena in interactive form. Notice that clicking on the will run the change detection twice. Once for the and once for the

.

So with this PR, we have this new option to let ngZone only run Change Detection once in this case.
By default, this new ngZoneEventCoalescing will be false, so nothing changed. If this option is being set to true, then only one change detection will be triggered async.

@JiaLiPassion JiaLiPassion requested a review from a team as a code owner May 17, 2019 11:51
@jasonaden jasonaden added the area: core Issues related to the framework runtime label May 17, 2019
@ngbot ngbot bot added this to the needsTriage milestone May 17, 2019
Copy link
Contributor

@mhevery mhevery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments.

@@ -249,7 +251,8 @@ export class PlatformRef {
// So we create a mini parent injector that just contains the new NgZone and
// pass that as parent to the NgModuleFactory.
const ngZoneOption = options ? options.ngZone : undefined;
const ngZone = getNgZone(ngZoneOption);
const ngZoneEventCoalescing = options ? options.ngZoneEventCoalescing : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is easier to read.

Suggested change
const ngZoneEventCoalescing = options ? options.ngZoneEventCoalescing : false;
const ngZoneEventCoalescing = (options && options.ngZoneEventCoalescing) || false;

@@ -340,14 +343,17 @@ export class PlatformRef {
get destroyed() { return this._destroyed; }
}

function getNgZone(ngZoneOption?: NgZone | 'zone.js' | 'noop'): NgZone {
function getNgZone(
ngZoneOption?: NgZone | 'zone.js' | 'noop', ngZoneEventCoalescing?: boolean): NgZone {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ngZoneOption?: NgZone | 'zone.js' | 'noop', ngZoneEventCoalescing?: boolean): NgZone {
ngZoneOption: NgZone | 'zone.js' | 'noop' | undefiend, ngZoneEventCoalescing: boolean): NgZone {

packages/core/src/zone/ng_zone.ts Outdated Show resolved Hide resolved
packages/core/src/zone/ng_zone.ts Outdated Show resolved Hide resolved
packages/core/src/zone/ng_zone.ts Outdated Show resolved Hide resolved
function forkInnerZoneWithAngularBehavior(zone: NgZonePrivate) {
declare const global: any;

const _global: any = typeof window !== 'undefined' ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import global from core/src/util

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks!

packages/core/src/zone/ng_zone.ts Outdated Show resolved Hide resolved
// so we need to cancel that and schedule a new one, or maybe we can just
// skip.
// option1 : cancel and reschedule
// option2: just return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return, I don't think there is a value in canceling it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

@@ -199,6 +199,8 @@ export interface BootstrapOptions {
* - `noop` - Use `NoopNgZone` which does nothing.
*/
ngZone?: NgZone|'zone.js'|'noop';

ngZoneEventCoalescing?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs public API docs here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add docs explaining what this does?

@mhevery
Copy link
Contributor

mhevery commented May 31, 2019

Need to run bazel run //tools/public_api_guard:core_api.accept

@JiaLiPassion JiaLiPassion requested a review from a team as a code owner May 31, 2019 15:30
@JiaLiPassion
Copy link
Contributor Author

@mhevery, I fixed several issues, now all tests passed, (expect the case of integration file-size-limit), I will continue to add test cases, could you run this version in g3? Thanks!

@mhevery
Copy link
Contributor

mhevery commented Jun 4, 2019

@JiaLiPassion JiaLiPassion requested a review from a team as a code owner June 6, 2019 16:43
@JiaLiPassion JiaLiPassion changed the title WIP: test coalesce event feat: test coalesce event Jun 6, 2019
@JiaLiPassion JiaLiPassion changed the title feat: test coalesce event feat: add a flag in bootstrap to enable coalesce event to improve performance Jun 6, 2019
@JiaLiPassion
Copy link
Contributor Author

Need wait angular/zone.js#1239 to be merged. So the integration test can pass.

@JiaLiPassion JiaLiPassion force-pushed the test-coakescing-event branch 2 times, most recently from e45f1fc to b0ec7b4 Compare July 25, 2019 04:54
@JiaLiPassion JiaLiPassion requested a review from a team as a code owner July 25, 2019 04:54
@JiaLiPassion JiaLiPassion requested a review from a team as a code owner July 26, 2019 20:33
@JiaLiPassion JiaLiPassion force-pushed the test-coakescing-event branch 2 times, most recently from 665c1ae to 0e681c1 Compare July 26, 2019 21:32
@JiaLiPassion JiaLiPassion force-pushed the test-coakescing-event branch 5 times, most recently from df3dd8c to 5911811 Compare August 30, 2019 06:23
@JiaLiPassion JiaLiPassion removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Oct 19, 2019
@JiaLiPassion
Copy link
Contributor Author

@matsko, I have updated the PR and now the API will not have any changes, and g3 should be ok, please review, thank you!

* change detection only once.
*
* By default, this option will be false. So the events will not be
* colesced and the change detection will be triggered multiple times.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* colesced and the change detection will be triggered multiple times.
* coalesced and the change detection will be triggered multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review, updated.

};
}

var nativeRequestAnimationFrame = getNativeRequestAnimationFrame().nativeRequestAnimationFrame;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stumbled over this PR. Out of curiosity: is it intentional that we introduce a new side-effect global call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, I should not add a side-effect here, I just updated the PR to move the call inside ngZone initialize call.

@JiaLiPassion JiaLiPassion force-pushed the test-coakescing-event branch 2 times, most recently from 0743288 to be89686 Compare October 19, 2019 13:50
@matsko matsko added the action: merge The PR is ready for merge by the caretaker label Oct 21, 2019
@kara kara added the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 22, 2019
@kara kara requested a review from IgorMinar October 22, 2019 19:17
AndrusGerman pushed a commit to AndrusGerman/angular that referenced this pull request Oct 22, 2019
AndrusGerman pushed a commit to AndrusGerman/angular that referenced this pull request Oct 22, 2019
@mhevery
Copy link
Contributor

mhevery commented Oct 28, 2019

presubmit

@mhevery mhevery removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 29, 2019
@mhevery mhevery added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Nov 5, 2019
@mhevery
Copy link
Contributor

mhevery commented Nov 5, 2019

MERGE_ASSISTANCE: Please merge by itself as this is high risk.

@AndrewKushnir
Copy link
Contributor

Since this change is a high risk (as Misko mentioned), I've started a Global VE presubmit as well (and a Blueprint one).

@atscott atscott closed this in 44623a1 Nov 5, 2019
@atscott
Copy link
Contributor

atscott commented Nov 5, 2019

This was merged to master only. @mhevery @JiaLiPassion is that good or did you also need this in the patch branch?

@naveedahmed1
Copy link
Contributor

Should this provide any benefit on server, I mean in case of SSR/Universal?

@trotyl
Copy link
Contributor

trotyl commented Nov 7, 2019

Should this provide any benefit on server, I mean in case of SSR/Universal?

There is little chance for DOM events to be triggered on server, unless you manually dispatch them.

@JiaLiPassion
Copy link
Contributor Author

@atscott, I am not sure about whether this need to be merged to patch branch, @mhevery , please confirm, thanks!

mohaxspb pushed a commit to mohaxspb/angular that referenced this pull request Nov 7, 2019
mohaxspb pushed a commit to mohaxspb/angular that referenced this pull request Nov 7, 2019
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note risk: high target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet