-
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
stream: pre-allocate _events #50428
stream: pre-allocate _events #50428
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ const { | |
|
||
const kCapture = Symbol('kCapture'); | ||
const kErrorMonitor = Symbol('events.errorMonitor'); | ||
const kShapeMode = Symbol('shapeMode'); | ||
const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners'); | ||
const kMaxEventTargetListenersWarned = | ||
Symbol('events.maxEventTargetListenersWarned'); | ||
|
@@ -344,6 +345,9 @@ EventEmitter.init = function(opts) { | |
this._events === ObjectGetPrototypeOf(this)._events) { | ||
this._events = { __proto__: null }; | ||
this._eventsCount = 0; | ||
this[kShapeMode] = false; | ||
benjamingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
this[kShapeMode] = true; | ||
} | ||
|
||
this._maxListeners = this._maxListeners || undefined; | ||
|
@@ -686,9 +690,13 @@ EventEmitter.prototype.removeListener = | |
return this; | ||
|
||
if (list === listener || list.listener === listener) { | ||
if (--this._eventsCount === 0) | ||
this._eventsCount -= 1; | ||
|
||
if (this[kShapeMode]) { | ||
events[type] = undefined; | ||
} else if (this._eventsCount === 0) { | ||
this._events = { __proto__: null }; | ||
else { | ||
} else { | ||
delete events[type]; | ||
if (events.removeListener) | ||
this.emit('removeListener', type, list.listener || listener); | ||
|
@@ -750,6 +758,7 @@ EventEmitter.prototype.removeAllListeners = | |
else | ||
delete events[type]; | ||
} | ||
this[kShapeMode] = false; | ||
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. Why? What if we just have an object template (template in the abstract way not v8 one) and use that when resetting? (Same for the one below) (This is not really important comment as it's not happening on a regular basis) 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. I don't understand 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. Maybe reset 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. That would require us to keep the template in memory and then create copies. Maybe having some kind of |
||
return this; | ||
} | ||
|
||
|
@@ -762,6 +771,7 @@ EventEmitter.prototype.removeAllListeners = | |
this.removeAllListeners('removeListener'); | ||
this._events = { __proto__: null }; | ||
this._eventsCount = 0; | ||
this[kShapeMode] = false; | ||
return this; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,24 @@ function Duplex(options) { | |
if (!(this instanceof Duplex)) | ||
return new Duplex(options); | ||
|
||
this._events ??= { | ||
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. Non-blocking nit: a comment explaining the purpose of defining this like this would be helpful for future generations coming into this code :-)
rluvaton marked this conversation as resolved.
Show resolved
Hide resolved
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. I don't really think we should modify the private property 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. Follow up PR welcome? |
||
close: undefined, | ||
error: undefined, | ||
prefinish: undefined, | ||
finish: undefined, | ||
drain: undefined, | ||
data: undefined, | ||
end: undefined, | ||
readable: undefined, | ||
// Skip uncommon events... | ||
// pause: undefined, | ||
// resume: undefined, | ||
// pipe: undefined, | ||
// unpipe: undefined, | ||
// [destroyImpl.kConstruct]: undefined, | ||
// [destroyImpl.kDestroy]: undefined, | ||
}; | ||
|
||
this._readableState = new Readable.ReadableState(options, this, true); | ||
this._writableState = new Writable.WritableState(options, this, true); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ class FakeInput extends EventEmitter { | |
function isWarned(emitter) { | ||
for (const name in emitter) { | ||
const listeners = emitter[name]; | ||
if (listeners.warned) return true; | ||
if (listeners && listeners.warned) return true; | ||
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. Non-blocking nit: Wouldn't |
||
} | ||
return false; | ||
} | ||
|
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.
Ideally this wouldn't be an extra property and share a bit field with captureRejections and maybe _maxListeners, but that doesn't need to happen in this PR