Skip to content

Commit

Permalink
events: try to improve perf by checking if impl already exists
Browse files Browse the repository at this point in the history
this will break but just for the sake of it
  • Loading branch information
rluvaton committed May 13, 2024
1 parent 190cb0d commit 1d27fc5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
13 changes: 1 addition & 12 deletions lib/internal/events/event_emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const {
kMaxEventTargetListeners,
kMaxEventTargetListenersWarned,
kImpl,
kPreInitiated,
kIsFastPath,
kSwitchToSlowPath,
kInitialEvents,
Expand Down Expand Up @@ -223,13 +222,6 @@ ObjectDefineProperties(EventEmitter.prototype, {
configurable: false,
writable: true,
},
[kPreInitiated]: {
__proto__: null,
value: false,
enumerable: false,
configurable: false,
writable: true,
},
[kIsFastPath]: {
__proto__: null,
value: undefined,
Expand Down Expand Up @@ -360,7 +352,6 @@ EventEmitter.init = function(opts) {

this._maxListeners = this._maxListeners || undefined;


if (opts?.captureRejections) {
validateBoolean(opts.captureRejections, 'options.captureRejections');
this[kCapture] = Boolean(opts.captureRejections);
Expand All @@ -369,9 +360,7 @@ EventEmitter.init = function(opts) {
// prototype lookup in a very sensitive hot path.
this[kCapture] = EventEmitter.prototype[kCapture];
}

if(this[kPreInitiated] === true) {
this[kPreInitiated] = false;
if(this[kImpl] !== undefined) {
return;
}

Expand Down
2 changes: 0 additions & 2 deletions lib/internal/events/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const kMaxEventTargetListenersWarned =
const kWatermarkData = SymbolFor('nodejs.watermarkData');

const kImpl = Symbol('kImpl');
const kPreInitiated = Symbol('kPreInitiated');
const kIsFastPath = Symbol('kIsFastPath');
const kSwitchToSlowPath = Symbol('kSwitchToSlowPath');
const kRejection = SymbolFor('nodejs.rejection');
Expand All @@ -32,7 +31,6 @@ module.exports = {
kMaxEventTargetListenersWarned,
kWatermarkData,
kImpl,
kPreInitiated,
kIsFastPath,
kSwitchToSlowPath,
kRejection,
Expand Down
11 changes: 4 additions & 7 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Readable.ReadableState = ReadableState;

const EE = require('events');
const { Stream, prependListener } = require('internal/streams/legacy');
const { kInitialEvents, kImpl, kShapeMode, kIsFastPath, kPreInitiated} = require('internal/events/symbols');
const { kInitialEvents, kImpl, kShapeMode, kIsFastPath } = require('internal/events/symbols');
const { Buffer } = require('buffer');

const {
Expand Down Expand Up @@ -321,9 +321,9 @@ function Readable(options) {
if (!(this instanceof Readable))
return new Readable(options);

if (this[kImpl] === undefined && this._events === undefined) {
if (this[kImpl] === undefined) {
this[kIsFastPath] = true;
this[kImpl] = new FastEventEmitter(this, this[kInitialEvents] !== undefined ? this[kInitialEvents] :{
this[kImpl] = new FastEventEmitter(this, this[kInitialEvents] === undefined ? {
close: undefined,
error: undefined,
data: undefined,
Expand All @@ -336,10 +336,8 @@ function Readable(options) {
// unpipe: undefined,
// [destroyImpl.kConstruct]: undefined,
// [destroyImpl.kDestroy]: undefined,
});
} : this[kInitialEvents]);
this[kImpl][kShapeMode] = true;
} else {
this[kPreInitiated] = false;
}

this._readableState = new ReadableState(options, this, false);
Expand Down Expand Up @@ -367,7 +365,6 @@ function Readable(options) {
}
}

Readable.prototype[kPreInitiated] = true;
Readable.prototype.destroy = destroyImpl.destroy;
Readable.prototype._undestroy = destroyImpl.undestroy;
Readable.prototype._destroy = function(err, cb) {
Expand Down

0 comments on commit 1d27fc5

Please sign in to comment.