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

performance,events: use Map to store events in EventEmitter #21856

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions benchmark/events/ee-add-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
const common = require('../common.js');
const events = require('events');

const bench = common.createBenchmark(main, { n: [1e6] });
const bench = common.createBenchmark(main, {
n: [5e6],
Copy link
Member

Choose a reason for hiding this comment

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

Do we really have to push up the iterations here?

staleEventsCount: [0, 5],
listenerCount: [1, 5],
});

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

var k;
for (k = 0; k < 10; k += 1)
for (k = 0; k < listenerCount; k += 1)
listeners.push(function() {});

for (k = 0; k < staleEventsCount; k++)
ee.on(`dummyunused${k}`, () => {});

bench.start();
for (var i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
Expand Down
28 changes: 28 additions & 0 deletions benchmark/events/ee-emit-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
const common = require('../common.js');
const EventEmitter = require('events').EventEmitter;

const bench = common.createBenchmark(main, {
n: [2e7],
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a pretty high iteration count.

listenerCount: [1, 5, 10],
});

function main({ n, listenerCount }) {
const ee = new EventEmitter();

for (var k = 0; k < listenerCount; k += 1) {
ee.on('dummy', function() {});
ee.on(`dummy${k}`, function() {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
if (i % 3 === 0)
ee.emit('dummy', true, 5);
else if (i % 2 === 0)
ee.emit('dummy', true, 5, 10, false);
else
ee.emit('dummy');
}
bench.end(n);
}
6 changes: 3 additions & 3 deletions benchmark/events/ee-emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const EventEmitter = require('events').EventEmitter;
const bench = common.createBenchmark(main, {
n: [2e6],
argc: [0, 2, 4, 10],
listeners: [1, 5, 10],
listenerCount: [1, 5, 10],
});

function main({ n, argc, listeners }) {
function main({ n, argc, listenerCount }) {
const ee = new EventEmitter();

for (var k = 0; k < listeners; k += 1)
for (var k = 0; k < listenerCount; k += 1)
ee.on('dummy', function() {});

var i;
Expand Down
28 changes: 28 additions & 0 deletions benchmark/events/ee-event-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
const common = require('../common.js');
const EventEmitter = require('events').EventEmitter;

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

function main({ n }) {
const ee = new EventEmitter();

for (var k = 0; k < 9; k += 1) {
ee.on(`dummy0${k}`, function() {});
ee.on(`dummy1${k}`, function() {});
ee.on(`dummy2${k}`, function() {});
}

ee.removeAllListeners('dummy01');
ee.removeAllListeners('dummy11');
ee.removeAllListeners('dummy21');
ee.removeAllListeners('dummy06');
ee.removeAllListeners('dummy16');
ee.removeAllListeners('dummy26');

bench.start();
for (var i = 0; i < n; i += 1) {
ee.eventNames();
}
bench.end(n);
}
2 changes: 1 addition & 1 deletion benchmark/events/ee-listeners-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');
const EventEmitter = require('events').EventEmitter;

const bench = common.createBenchmark(main, { n: [5e6] });
const bench = common.createBenchmark(main, { n: [1e7] });
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to increase the iterations here?


function main({ n }) {
const ee = new EventEmitter();
Expand Down
13 changes: 9 additions & 4 deletions benchmark/events/ee-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
const common = require('../common.js');
const EventEmitter = require('events').EventEmitter;

const bench = common.createBenchmark(main, { n: [2e7] });
const bench = common.createBenchmark(main, {
n: [5e6],
listenerCount: [1, 5, 10],
});

function main({ n }) {

function main({ n, listenerCount }) {
const ee = new EventEmitter();

function listener() {}

bench.start();
for (var i = 0; i < n; i += 1) {
for (var i = 0; i < n; ++i) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.once(dummy, listener);
for (var j = 0; j < listenerCount; ++j)
ee.once(dummy, listener);
ee.emit(dummy);
}
bench.end(n);
Expand Down
15 changes: 9 additions & 6 deletions deps/v8/src/builtins/builtins-collections-gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1474,14 +1474,17 @@ TF_BUILTIN(MapPrototypeDelete, CollectionsBuiltinsAssembler) {
LoadFixedArrayElement(table, OrderedHashMap::kNumberOfBucketsIndex);

// If there fewer elements than #buckets / 2, shrink the table.
Label shrink(this);
GotoIf(SmiLessThan(SmiAdd(number_of_elements, number_of_elements),
number_of_buckets),
&shrink);
Return(TrueConstant());
Label dont_shrink(this);
GotoIf(SmiGreaterThanOrEqual(SmiAdd(number_of_elements, number_of_elements),
number_of_buckets),
&dont_shrink);
// If #buckets is less than 8 then don't shrink the table
GotoIf(SmiLessThan(number_of_buckets, SmiConstant(8)),
&dont_shrink);

BIND(&shrink);
CallRuntime(Runtime::kMapShrink, context, receiver);

BIND(&dont_shrink);
Return(TrueConstant());
}

Expand Down
Loading