mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
stream: pre-allocate _events
PR-URL: https://github.com/nodejs/node/pull/50428 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
@@ -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;
|
||||
} 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;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -762,6 +771,7 @@ EventEmitter.prototype.removeAllListeners =
|
||||
this.removeAllListeners('removeListener');
|
||||
this._events = { __proto__: null };
|
||||
this._eventsCount = 0;
|
||||
this[kShapeMode] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,24 @@ function Duplex(options) {
|
||||
if (!(this instanceof Duplex))
|
||||
return new Duplex(options);
|
||||
|
||||
this._events ??= {
|
||||
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);
|
||||
|
||||
|
||||
@@ -319,6 +319,21 @@ function Readable(options) {
|
||||
if (!(this instanceof Readable))
|
||||
return new Readable(options);
|
||||
|
||||
this._events ??= {
|
||||
close: undefined,
|
||||
error: 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 ReadableState(options, this, false);
|
||||
|
||||
if (options) {
|
||||
|
||||
@@ -385,6 +385,17 @@ function Writable(options) {
|
||||
if (!(this instanceof Writable))
|
||||
return new Writable(options);
|
||||
|
||||
this._events ??= {
|
||||
close: undefined,
|
||||
error: undefined,
|
||||
prefinish: undefined,
|
||||
finish: undefined,
|
||||
drain: undefined,
|
||||
// Skip uncommon events...
|
||||
// [destroyImpl.kConstruct]: undefined,
|
||||
// [destroyImpl.kDestroy]: undefined,
|
||||
};
|
||||
|
||||
this._writableState = new WritableState(options, this, false);
|
||||
|
||||
if (options) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,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;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user