Skip to content

lib: faster event listening #41309

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

Closed
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
87 changes: 29 additions & 58 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayFrom,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
ArrayPrototypeShift,
Expand Down Expand Up @@ -53,8 +54,7 @@ const {
} = primordials;
const kRejection = SymbolFor('nodejs.rejection');
const { inspect } = require('internal/util/inspect');

let spliceOne;
const getMappedLinkedList = require('internal/mapped_linkedlist');

const {
AbortError,
Expand Down Expand Up @@ -526,8 +526,8 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
addCatch(this, result, type, args);
}
} else {
const len = handler.length;
const listeners = arrayClone(handler);
const listeners = ArrayFrom(handler);
const len = listeners.length;
for (let i = 0; i < len; ++i) {
const result = listeners[i].apply(this, args);

Expand All @@ -545,6 +545,10 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
return true;
};

function getRealListener(listener) {
return listener.listener || listener;
}

function _addListener(target, type, listener, prepend) {
let m;
let events;
Expand Down Expand Up @@ -575,12 +579,10 @@ function _addListener(target, type, listener, prepend) {
events[type] = listener;
++target._eventsCount;
} else {
if (typeof existing === 'function') {
// Adding the second element, need to change to array.
if (typeof existing === 'function')
existing = events[type] =
prepend ? [listener, existing] : [existing, listener];
// If we've already got an array, just append.
} else if (prepend) {
getMappedLinkedList(getRealListener).push(existing);
if (prepend) {
existing.unshift(listener);
} else {
existing.push(listener);
Expand Down Expand Up @@ -703,32 +705,16 @@ EventEmitter.prototype.removeListener =
if (events.removeListener)
this.emit('removeListener', type, list.listener || listener);
}
} else if (typeof list !== 'function') {
let position = -1;

for (let i = list.length - 1; i >= 0; i--) {
if (list[i] === listener || list[i].listener === listener) {
position = i;
break;
}
}

if (position < 0)
return this;

if (position === 0)
list.shift();
else {
if (spliceOne === undefined)
spliceOne = require('internal/util').spliceOne;
spliceOne(list, position);
} else if (typeof list !== 'function' && list?.remove(listener)) {
if (list.length === 0) {
delete events[type];
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
} else if (list.length === 1) {
events[type] = list.first;
}

if (list.length === 1)
events[type] = list[0];

if (events.removeListener !== undefined)
this.emit('removeListener', type, listener);
this.emit('removeListener', type, getRealListener(listener));
}

return this;
Expand Down Expand Up @@ -781,8 +767,8 @@ EventEmitter.prototype.removeAllListeners =
this.removeListener(type, listeners);
} else if (listeners !== undefined) {
// LIFO order
for (let i = listeners.length - 1; i >= 0; i--) {
this.removeListener(type, listeners[i]);
for (const listener of listeners.reverseIterator()) {
this.removeListener(type, listener);
}
}

Expand All @@ -800,10 +786,12 @@ function _listeners(target, type, unwrap) {
return [];

if (typeof evlistener === 'function')
return unwrap ? [evlistener.listener || evlistener] : [evlistener];
return unwrap ? [getRealListener(evlistener)] : [evlistener];

return unwrap ?
unwrapListeners(evlistener) : arrayClone(evlistener);
return ArrayFrom(unwrap ?
unwrapListeners(evlistener) :
evlistener,
);
}

/**
Expand Down Expand Up @@ -874,27 +862,10 @@ EventEmitter.prototype.eventNames = function eventNames() {
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
};

function arrayClone(arr) {
// At least since V8 8.3, this implementation is faster than the previous
// which always used a simple for-loop
switch (arr.length) {
case 2: return [arr[0], arr[1]];
case 3: return [arr[0], arr[1], arr[2]];
case 4: return [arr[0], arr[1], arr[2], arr[3]];
case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]];
case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
}
return ArrayPrototypeSlice(arr);
}

function unwrapListeners(arr) {
const ret = arrayClone(arr);
for (let i = 0; i < ret.length; ++i) {
const orig = ret[i].listener;
if (typeof orig === 'function')
ret[i] = orig;
function *unwrapListeners(listeners) {
for (const item of listeners) {
yield typeof item.listener === 'function' ? item.listener : item;
}
return ret;
}

/**
Expand Down
136 changes: 136 additions & 0 deletions lib/internal/mapped_linkedlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict';

const {
Map,
SymbolIterator,
} = primordials;

function push(root, value) {
const node = { value };
if (root.last) {
node.previous = root.last;
root.last.next = node;
} else {
root.first = node;
}
root.last = node;
root.length++;

return node;
}

function unshift(root, value) {
const node = { value };
if (root.first) {
node.next = root.first;
root.first.previous = node;
} else {
root.last = node;
}
root.first = node;
root.length++;

return node;
}

function pop(root) {
if (root.last) {
const { value } = root.last;
root.last = root.last.previous;
root.length--;
if (!root.last) {
root.first = root.last;
}
return value;
}
}

function getIterator(root, initialProperty, nextProperty) {
let node = root[initialProperty];

return {
next() {
if (!node) {
return { done: true };
}
const result = {
done: false,
value: node.value,
};
node = node[nextProperty];
return result;
},
[SymbolIterator]() {
return this;
}
};
}


function getMappedLinkedList(getKey = (value) => value) {
const map = new Map();
function addToMap(value, node, operation) {
const key = getKey(value);
let refs = map.get(key);
if (!refs) {
map.set(key, refs = { length: 0 });
}
operation(refs, node);
}
const root = { length: 0 };

return {
root,
get length() {
return root.length;
},
get first() {
return root.first?.value;
},
push(value) {
const node = push(root, value);
addToMap(value, node, push);

return this;
},
unshift(value) {
const node = unshift(root, value);
addToMap(value, node, unshift);

return this;
},
remove(value) {
const key = getKey(value);
const refs = map.get(key);
if (refs) {
const result = pop(refs);
if (result.previous) {
result.previous.next = result.next;
}
if (result === root.last) {
root.last = result.previous || result.next;
}
if (result.next) {
result.next.previous = result.previous;
}
if (result === root.first) {
root.first = result.next || result.previous;
}
if (refs.length === 0) {
map.delete(key);
}
root.length--;
return 1;
}
return 0;
},
[SymbolIterator]() {
return getIterator(root, 'first', 'next');
},
reverseIterator() {
return getIterator(root, 'last', 'previous');
},
};
}

module.exports = getMappedLinkedList;
10 changes: 6 additions & 4 deletions lib/internal/streams/legacy.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const {
ArrayIsArray,
ObjectSetPrototypeOf,
} = primordials;

const EE = require('events');
const getMappedLinkedList = require('internal/mapped_linkedlist');

function Stream(opts) {
EE.call(this, opts);
Expand Down Expand Up @@ -105,10 +105,12 @@ function prependListener(emitter, event, fn) {
// the prependListener() method. The goal is to eventually remove this hack.
if (!emitter._events || !emitter._events[event])
emitter.on(event, fn);
else if (ArrayIsArray(emitter._events[event]))
else if (typeof emitter._events[event] === 'function') {
const previous = emitter._events[event];
emitter._events[event] = getMappedLinkedList((l) => l.listener || l)
.push(fn).push(previous);
} else
emitter._events[event].unshift(fn);
else
emitter._events[event] = [fn, emitter._events[event]];
}

module.exports = { Stream, prependListener };
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const expectedModules = new Set([
'NativeModule internal/histogram',
'NativeModule internal/idna',
'NativeModule internal/linkedlist',
'NativeModule internal/mapped_linkedlist',
'NativeModule internal/modules/run_main',
'NativeModule internal/modules/package_json_reader',
'NativeModule internal/modules/cjs/helpers',
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-event-emitter-listeners-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ e.listeners('bar');
e.on('foo', assert.ok);
fl = e.listeners('foo');

assert(Array.isArray(e._events.foo));
assert.strictEqual(e._events.foo.length, 2);
assert.strictEqual(e._events.foo[0], assert.fail);
assert.strictEqual(e._events.foo[1], assert.ok);
const arr = Array.from(e._events.foo);
assert.strictEqual(arr.length, 2);
assert.strictEqual(arr[0], assert.fail);
assert.strictEqual(arr[1], assert.ok);

assert(Array.isArray(fl));
assert.strictEqual(fl.length, 2);
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http-req-close-robust-from-tampering.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const { connect } = require('net');
// cause an error.

const server = createServer(common.mustCall((req, res) => {
req.client._events.close.forEach((fn) => { fn.bind(req)(); });
for (const fn of req.client._events.close) {
fn.apply(req);
}
}));

server.unref();
Expand Down