From 1d27fc5942b40d804dbc1d4aa65e3073aaaca62d Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Mon, 13 May 2024 15:31:44 +0300 Subject: [PATCH] events: try to improve perf by checking if impl already exists this will break but just for the sake of it --- lib/internal/events/event_emitter.js | 13 +------------ lib/internal/events/symbols.js | 2 -- lib/internal/streams/readable.js | 11 ++++------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/internal/events/event_emitter.js b/lib/internal/events/event_emitter.js index 9709c0c5176e1d..8bf856cb0c0fa6 100644 --- a/lib/internal/events/event_emitter.js +++ b/lib/internal/events/event_emitter.js @@ -32,7 +32,6 @@ const { kMaxEventTargetListeners, kMaxEventTargetListenersWarned, kImpl, - kPreInitiated, kIsFastPath, kSwitchToSlowPath, kInitialEvents, @@ -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, @@ -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); @@ -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; } diff --git a/lib/internal/events/symbols.js b/lib/internal/events/symbols.js index 53980855996d2a..9f15f874cb5223 100644 --- a/lib/internal/events/symbols.js +++ b/lib/internal/events/symbols.js @@ -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'); @@ -32,7 +31,6 @@ module.exports = { kMaxEventTargetListenersWarned, kWatermarkData, kImpl, - kPreInitiated, kIsFastPath, kSwitchToSlowPath, kRejection, diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index bb90cc18145ae7..c8f46deb979516 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -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 { @@ -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, @@ -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); @@ -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) {