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

events: use nullish coalencing operator #38328

Closed
wants to merge 7 commits into from

Conversation

VoltrexKeyva
Copy link
Member

Could use this here instead of checking for all falsy values, a little bit more readable as well.

Could use this here instead of checking for all falsy values, a little bit more readable as well.
@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to the events subsystem / EventEmitter. needs-ci PRs that need a full CI run. labels Apr 21, 2021
@mscdex
Copy link
Contributor

mscdex commented Apr 21, 2021

I think a benchmark that utilizes 'newListener' events should be added to this PR so we can ensure that this change doesn't result in any performance regressions like we've recently seen with other new javascript operators (e.g. a0261d2).

@VoltrexKeyva
Copy link
Member Author

I think a benchmark that utilizes 'newListener' events should be added to this PR so we can ensure that this change doesn't result in any performance regressions like we've recently seen with other new javascript operators (e.g. a0261d2).

How may I add that? Sorry I've never worked with Node.js benchmarks before.

@mscdex
Copy link
Contributor

mscdex commented Apr 21, 2021

Just create a new file in benchmarks/events. You can use one of the existing benchmark files in there as a guide/starting point.

Created the benchmark for new listeners.
@VoltrexKeyva
Copy link
Member Author

Just create a new file in benchmarks/events. You can use one of the existing benchmark files in there as a guide/starting point.

Done, I'm not sure if that's how I'm supposed to do it though.

Used the increment assignment operator (`+=`) for consistency on all benchmarks.
@benjamingr
Copy link
Member

@VoltrexKeyva
Copy link
Member Author

@VoltrexMaster https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md :)

What I wrote seems to be working perfectly, no idea what else am I supposed to do.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

The basic change LGTM if there's not a significant perf regression. I did not look at the benchmark code.

Added a single event listener instead of 10 and now only exercising the new changes, and fixed random listener property.
@mscdex
Copy link
Contributor

mscdex commented Apr 21, 2021

@VoltrexMaster There is no need to emit 'newListener' manually in the benchmark as node core is already doing that and that's what we're wanting to benchmark. At this point, I think it's probably easiest to just modify the "ee-add-remove" benchmark to include a (new) boolean parameter (0 or 1) that indicates whether a 'newListener' handler will be added to the emitter before the timed loop. That should be all we need to be able to benchmark this code path.

@VoltrexKeyva
Copy link
Member Author

VoltrexKeyva commented Apr 21, 2021

@VoltrexMaster There is no need to emit 'newListener' manually in the benchmark as node core is already doing that and that's what we're wanting to benchmark. At this point, I think it's probably easiest to just modify the "ee-add-remove" benchmark to include a (new) boolean parameter (0 or 1) that indicates whether a 'newListener' will be added to the emitter before the timed loop. That should be all we need to be able to benchmark this code path.

By "before the timed loop" do you mean before where the benchmark actually starts? (bench.start()).

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

Thank you for this! Only putting 'request changes' so this doesn't land without benchmarks showing no performance regression. Code looks good otherwise 🙏

@mscdex
Copy link
Contributor

mscdex commented Apr 21, 2021

By "before the timed loop" do you mean before where the benchmark actually starts? (bench.start()).

Yes.

@VoltrexKeyva
Copy link
Member Author

@mscdex the "(new) boolean parameter (0 or 1) that indicates whether a 'newListener' handler will be added to the emitter" kinda confused me, parameter where exactly or what kind of handler? Can you please elaborate on that?

@mscdex
Copy link
Contributor

mscdex commented Apr 22, 2021

@VoltrexMaster ee-add-remove would look like this, using the current copy from the master branch (I've also added a parameter for testing 'removeListener' handlers):

'use strict';
const common = require('../common.js');
const events = require('events');

const bench = common.createBenchmark(main, {
  newlistener: [0, 1],
  removelistener: [0, 1],
  n: [1e6],
});

function main({ newlistener, removelistener, n }) {
  const ee = new events.EventEmitter();
  const listeners = [];

  for (let k = 0; k < 10; k += 1)
    listeners.push(() => {});

  if (newlistener === 1)
    ee.on('newListener', (event, listener) => {});

  if (removelistener === 1)
    ee.on('removeListener', (event, listener) => {});

  bench.start();
  for (let i = 0; i < n; i += 1) {
    const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
    for (let k = listeners.length; --k >= 0; /* empty */) {
      ee.on(dummy, listeners[k]);
    }
    for (let k = listeners.length; --k >= 0; /* empty */) {
      ee.removeListener(dummy, listeners[k]);
    }
  }
  bench.end(n);
}

@VoltrexKeyva
Copy link
Member Author

@VoltrexMaster ee-add-remove would look like this, using the current copy from the master branch (I've also added a parameter for testing 'removeListener' handlers):

'use strict';
const common = require('../common.js');
const events = require('events');

const bench = common.createBenchmark(main, {
  newlistener: [0, 1],
  removelistener: [0, 1],
  n: [1e6],
});

function main({ newlistener, removelistener, n }) {
  const ee = new events.EventEmitter();
  const listeners = [];

  for (let k = 0; k < 10; k += 1)
    listeners.push(() => {});

  if (newlistener === 1)
    ee.on('newListener', (event, listener) => {});

  if (removelistener === 1)
    ee.on('removeListener', (event, listener) => {});

  bench.start();
  for (let i = 0; i < n; i += 1) {
    const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
    for (let k = listeners.length; --k >= 0; /* empty */) {
      ee.on(dummy, listeners[k]);
    }
    for (let k = listeners.length; --k >= 0; /* empty */) {
      ee.removeListener(dummy, listeners[k]);
    }
  }
  bench.end(n);
}

Oh thanks, I didn't know what those properties on the createBenchmark() method does but now I do, adding that rn.

Fixed the `ee-newlistener` benchmark.
CI check failed for timeout, pushing to fix.
@mscdex
Copy link
Contributor

mscdex commented Apr 22, 2021

Sorry, I meant you can just update the ee-add-remove.js benchmark file instead of creating a new one.

@VoltrexKeyva
Copy link
Member Author

Sorry, I meant you can just update the ee-add-remove.js benchmark file instead of creating a new one.

Oh, how you said it made me think you meant to create a new one with copying and modifying it, Imma delete this one and edit the existing one then 😁

Deleted the ee-newlistener benchmark and modified the ee-add-remove
benchmark with the changes.
@mscdex
Copy link
Contributor

mscdex commented Apr 23, 2021

Benchmark CI results seem to look ok, although I'm not sure why the newListener=0 and removeListener=0 case for the ee-add-remove benchmark shows an improvement.

@VoltrexKeyva
Copy link
Member Author

VoltrexKeyva commented Apr 23, 2021

Benchmark CI results seem to look ok, although I'm not sure why the newListener=0 and removeListener=0 case for the ee-add-remove benchmark shows an improvement.

¯\_(ツ)_/¯

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented May 15, 2021

@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. benchmark Issues and PRs related to the benchmark subsystem. labels May 15, 2021
@jasnell
Copy link
Member

jasnell commented May 17, 2021

Landed in 6e6663b

@jasnell jasnell closed this May 17, 2021
jasnell pushed a commit that referenced this pull request May 17, 2021
PR-URL: #38328
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
@VoltrexKeyva VoltrexKeyva deleted the patch-4 branch May 17, 2021 21:56
targos pushed a commit that referenced this pull request May 18, 2021
PR-URL: #38328
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. benchmark Issues and PRs related to the benchmark subsystem. events Issues and PRs related to the events subsystem / EventEmitter. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants