-
Notifications
You must be signed in to change notification settings - Fork 30k
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: improve performance for emit(), on(), and listeners() #13155
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,74 +86,18 @@ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { | |
}; | ||
|
||
function $getMaxListeners(that) { | ||
if (that._maxListeners === undefined) | ||
const maxListeners = that._maxListeners; | ||
if (maxListeners === undefined) | ||
return EventEmitter.defaultMaxListeners; | ||
return that._maxListeners; | ||
return maxListeners; | ||
} | ||
|
||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() { | ||
return $getMaxListeners(this); | ||
}; | ||
|
||
// These standalone emit* functions are used to optimize calling of event | ||
// handlers for fast cases because emit() itself often has a variable number of | ||
// arguments and can be deoptimized because of that. These functions always have | ||
// the same number of arguments and thus do not get deoptimized, so the code | ||
// inside them can execute faster. | ||
function emitNone(handler, isFn, self) { | ||
if (isFn) | ||
handler.call(self); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].call(self); | ||
} | ||
} | ||
function emitOne(handler, isFn, self, arg1) { | ||
if (isFn) | ||
handler.call(self, arg1); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].call(self, arg1); | ||
} | ||
} | ||
function emitTwo(handler, isFn, self, arg1, arg2) { | ||
if (isFn) | ||
handler.call(self, arg1, arg2); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].call(self, arg1, arg2); | ||
} | ||
} | ||
function emitThree(handler, isFn, self, arg1, arg2, arg3) { | ||
if (isFn) | ||
handler.call(self, arg1, arg2, arg3); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].call(self, arg1, arg2, arg3); | ||
} | ||
} | ||
|
||
function emitMany(handler, isFn, self, args) { | ||
if (isFn) | ||
handler.apply(self, args); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].apply(self, args); | ||
} | ||
} | ||
|
||
EventEmitter.prototype.emit = function emit(type) { | ||
var er, handler, len, args, i, events, domain; | ||
var er, fn, i, events, domain; | ||
var needDomainExit = false; | ||
var doError = (type === 'error'); | ||
|
||
|
@@ -189,38 +133,103 @@ EventEmitter.prototype.emit = function emit(type) { | |
return false; | ||
} | ||
|
||
handler = events[type]; | ||
fn = events[type]; | ||
|
||
if (!handler) | ||
if (!fn) | ||
return false; | ||
|
||
if (domain && this !== process) { | ||
domain.enter(); | ||
needDomainExit = true; | ||
} | ||
|
||
var isFn = typeof handler === 'function'; | ||
len = arguments.length; | ||
switch (len) { | ||
// fast cases | ||
case 1: | ||
emitNone(handler, isFn, this); | ||
break; | ||
case 2: | ||
emitOne(handler, isFn, this, arguments[1]); | ||
break; | ||
case 3: | ||
emitTwo(handler, isFn, this, arguments[1], arguments[2]); | ||
break; | ||
case 4: | ||
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]); | ||
break; | ||
// slower | ||
default: | ||
args = new Array(len - 1); | ||
for (i = 1; i < len; i++) | ||
args[i - 1] = arguments[i]; | ||
emitMany(handler, isFn, this, args); | ||
var len = arguments.length; | ||
var args; | ||
if (typeof fn === 'function') { | ||
switch (len) { | ||
case 1: fn.call(this); break; | ||
case 2: fn.call(this, arguments[1]); break; | ||
case 3: fn.call(this, arguments[1], arguments[2]); break; | ||
case 4: fn.call(this, arguments[1], arguments[2], arguments[3]); | ||
break; | ||
default: | ||
args = new Array(len - 1); | ||
for (i = 1; i < len; i++) | ||
args[i - 1] = arguments[i]; | ||
fn.apply(this, args); | ||
} | ||
} else { | ||
var fnlen = fn.length; | ||
fn = arrayClone(fn, fnlen); | ||
switch (len) { | ||
case 1: | ||
fn[0].call(this); | ||
fn[1].call(this); | ||
if (fnlen === 2) break; | ||
fn[2].call(this); | ||
if (fnlen === 3) break; | ||
fn[3].call(this); | ||
if (fnlen === 4) break; | ||
fn[4].call(this); | ||
if (fnlen === 5) break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is there any significance of unrolling the loop for |
||
for (i = 5; i < fnlen; ++i) | ||
fn[i].call(this); | ||
break; | ||
case 2: | ||
fn[0].call(this, arguments[1]); | ||
fn[1].call(this, arguments[1]); | ||
if (fnlen === 2) break; | ||
fn[2].call(this, arguments[1]); | ||
if (fnlen === 3) break; | ||
fn[3].call(this, arguments[1]); | ||
if (fnlen === 4) break; | ||
fn[4].call(this, arguments[1]); | ||
if (fnlen === 5) break; | ||
for (i = 5; i < fnlen; ++i) | ||
fn[i].call(this, arguments[1]); | ||
break; | ||
case 3: | ||
fn[0].call(this, arguments[1], arguments[2]); | ||
fn[1].call(this, arguments[1], arguments[2]); | ||
if (fnlen === 2) break; | ||
fn[2].call(this, arguments[1], arguments[2]); | ||
if (fnlen === 3) break; | ||
fn[3].call(this, arguments[1], arguments[2]); | ||
if (fnlen === 4) break; | ||
fn[4].call(this, arguments[1], arguments[2]); | ||
if (fnlen === 5) break; | ||
for (i = 5; i < fnlen; ++i) | ||
fn[i].call(this, arguments[1], arguments[2]); | ||
break; | ||
case 4: | ||
fn[0].call(this, arguments[1], arguments[2], arguments[3]); | ||
fn[1].call(this, arguments[1], arguments[2], arguments[3]); | ||
if (fnlen === 2) break; | ||
fn[2].call(this, arguments[1], arguments[2], arguments[3]); | ||
if (fnlen === 3) break; | ||
fn[3].call(this, arguments[1], arguments[2], arguments[3]); | ||
if (fnlen === 4) break; | ||
fn[4].call(this, arguments[1], arguments[2], arguments[3]); | ||
if (fnlen === 5) break; | ||
for (i = 5; i < fnlen; ++i) | ||
fn[i].call(this, arguments[1], arguments[2], arguments[3]); | ||
break; | ||
default: | ||
args = new Array(len - 1); | ||
for (i = 1; i < len; i++) | ||
args[i - 1] = arguments[i]; | ||
fn[0].apply(this, args); | ||
fn[1].apply(this, args); | ||
if (fnlen === 2) break; | ||
fn[2].apply(this, args); | ||
if (fnlen === 3) break; | ||
fn[3].apply(this, args); | ||
if (fnlen === 4) break; | ||
fn[4].apply(this, args); | ||
if (fnlen === 5) break; | ||
for (i = 5; i < fnlen; ++i) | ||
fn[i].apply(this, args); | ||
} | ||
} | ||
|
||
if (needDomainExit) | ||
|
@@ -230,7 +239,6 @@ EventEmitter.prototype.emit = function emit(type) { | |
}; | ||
|
||
function _addListener(target, type, listener, prepend) { | ||
var m; | ||
var events; | ||
var existing; | ||
|
||
|
@@ -275,7 +283,7 @@ function _addListener(target, type, listener, prepend) { | |
|
||
// Check for listener leak | ||
if (!existing.warned) { | ||
m = $getMaxListeners(target); | ||
const m = $getMaxListeners(target); | ||
if (m && m > 0 && existing.length > m) { | ||
existing.warned = true; | ||
const w = new Error('Possible EventEmitter memory leak detected. ' + | ||
|
@@ -497,16 +505,31 @@ function spliceOne(list, index) { | |
} | ||
|
||
function arrayClone(arr, n) { | ||
var copy = new Array(n); | ||
switch (n) { | ||
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]]; | ||
} | ||
const copy = new Array(n); | ||
for (var i = 0; i < n; ++i) | ||
copy[i] = arr[i]; | ||
return copy; | ||
} | ||
|
||
function unwrapListeners(arr) { | ||
const ret = new Array(arr.length); | ||
for (var i = 0; i < ret.length; ++i) { | ||
ret[i] = arr[i].listener || arr[i]; | ||
const first = arr[0].listener || arr[0]; | ||
const second = arr[1].listener || arr[1]; | ||
const n = arr.length; | ||
switch (n) { | ||
case 2: return [first, second]; | ||
case 3: return [first, second, arr[2].listener || arr[2]]; | ||
case 4: return [first, second, arr[2].listener || arr[2], | ||
arr[3].listener || arr[3]]; | ||
default: | ||
const copy = new Array(n); | ||
for (var i = 0; i < n; ++i) | ||
copy[i] = arr[i].listener || arr[i]; | ||
return copy; | ||
} | ||
return ret; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if simplified to will be faster?