From c249549ea58bddc9eb12539425acf77d2d49031e Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 28 Oct 2023 11:18:35 +0200 Subject: [PATCH] stream: remove LazyTransform No longer necessary given recent stream and event optimziations. Refs: https://github.com/nodejs/node/pull/50428 Refs: https://github.com/nodejs/node/issues/50439 PR-URL: https://github.com/nodejs/node/pull/50440 --- benchmark/crypto/hash-stream-creation2.js | 21 +++++++++ lib/internal/crypto/cipher.js | 21 +++++---- lib/internal/crypto/hash.js | 15 +++--- lib/internal/streams/lazy_transform.js | 57 ----------------------- src/node_builtins.cc | 1 - 5 files changed, 40 insertions(+), 75 deletions(-) create mode 100644 benchmark/crypto/hash-stream-creation2.js delete mode 100644 lib/internal/streams/lazy_transform.js diff --git a/benchmark/crypto/hash-stream-creation2.js b/benchmark/crypto/hash-stream-creation2.js new file mode 100644 index 00000000000000..f39e970c5301f3 --- /dev/null +++ b/benchmark/crypto/hash-stream-creation2.js @@ -0,0 +1,21 @@ +// Creation benchmark +// creates a single hasher, then pushes a minimal data through it +'use strict'; +const common = require('../common.js'); +const crypto = require('crypto'); + +const bench = common.createBenchmark(main, { + n: [10e3], + algo: ['md5' ], +}); + +const buf = Buffer.alloc(4); + +function main({ algo, n }) { + bench.start(); + for (let i = 0; i < n; ++i) { + const h = crypto.createHash(algo); + h.end(buf); + } + bench.end(n); +} diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index a2b560d1382418..0b1d961a9a75c6 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -56,7 +56,7 @@ const { const assert = require('internal/assert'); -const LazyTransform = require('internal/streams/lazy_transform'); +const Transform = require('internal/streams/transform'); const { normalizeEncoding } = require('internal/util'); @@ -122,7 +122,8 @@ function createCipherBase(cipher, credential, options, decipher, iv) { } this._decoder = null; - ReflectApply(LazyTransform, this, [options]); + ReflectApply(Transform, this, [options]); + this._writableState.decodeStrings = false; } function createCipher(cipher, password, options, decipher) { @@ -152,8 +153,8 @@ function Cipher(cipher, password, options) { ReflectApply(createCipher, this, [cipher, password, options, true]); } -ObjectSetPrototypeOf(Cipher.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Cipher, LazyTransform); +ObjectSetPrototypeOf(Cipher.prototype, Transform.prototype); +ObjectSetPrototypeOf(Cipher, Transform); Cipher.prototype._transform = function _transform(chunk, encoding, callback) { this.push(this[kHandle].update(chunk, encoding)); @@ -257,8 +258,8 @@ function addCipherPrototypeFunctions(constructor) { constructor.prototype.setAAD = Cipher.prototype.setAAD; } -ObjectSetPrototypeOf(Cipheriv.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Cipheriv, LazyTransform); +ObjectSetPrototypeOf(Cipheriv.prototype, Transform.prototype); +ObjectSetPrototypeOf(Cipheriv, Transform); addCipherPrototypeFunctions(Cipheriv); // The Decipher class is part of the legacy Node.js crypto API. It exposes @@ -273,8 +274,8 @@ function Decipher(cipher, password, options) { ReflectApply(createCipher, this, [cipher, password, options, false]); } -ObjectSetPrototypeOf(Decipher.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Decipher, LazyTransform); +ObjectSetPrototypeOf(Decipher.prototype, Transform.prototype); +ObjectSetPrototypeOf(Decipher, Transform); addCipherPrototypeFunctions(Decipher); // The Decipheriv class is part of the legacy Node.js crypto API. It exposes @@ -289,8 +290,8 @@ function Decipheriv(cipher, key, iv, options) { ReflectApply(createCipherWithIV, this, [cipher, key, options, false, iv]); } -ObjectSetPrototypeOf(Decipheriv.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Decipheriv, LazyTransform); +ObjectSetPrototypeOf(Decipheriv.prototype, Transform.prototype); +ObjectSetPrototypeOf(Decipheriv, Transform); addCipherPrototypeFunctions(Decipheriv); function getCipherInfo(nameOrNid, options) { diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 57fcb63518d52d..adb52b75593c89 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -51,7 +51,7 @@ const { isArrayBufferView, } = require('internal/util/types'); -const LazyTransform = require('internal/streams/lazy_transform'); +const Transform = require('internal/streams/transform'); const kState = Symbol('kState'); const kFinalized = Symbol('kFinalized'); @@ -69,11 +69,12 @@ function Hash(algorithm, options) { this[kState] = { [kFinalized]: false, }; - ReflectApply(LazyTransform, this, [options]); + ReflectApply(Transform, this, [options]); + this._writableState.decodeStrings = false; } -ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Hash, LazyTransform); +ObjectSetPrototypeOf(Hash.prototype, Transform.prototype); +ObjectSetPrototypeOf(Hash, Transform); Hash.prototype.copy = function copy(options) { const state = this[kState]; @@ -133,11 +134,11 @@ function Hmac(hmac, key, options) { this[kState] = { [kFinalized]: false, }; - ReflectApply(LazyTransform, this, [options]); + ReflectApply(Transform, this, [options]); } -ObjectSetPrototypeOf(Hmac.prototype, LazyTransform.prototype); -ObjectSetPrototypeOf(Hmac, LazyTransform); +ObjectSetPrototypeOf(Hmac.prototype, Transform.prototype); +ObjectSetPrototypeOf(Hmac, Transform); Hmac.prototype.update = Hash.prototype.update; diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js deleted file mode 100644 index ab5746ce6a4cf8..00000000000000 --- a/lib/internal/streams/lazy_transform.js +++ /dev/null @@ -1,57 +0,0 @@ -// LazyTransform is a special type of Transform stream that is lazily loaded. -// This is used for performance with bi-API-ship: when two APIs are available -// for the stream, one conventional and one non-conventional. -'use strict'; - -const { - ObjectDefineProperties, - ObjectDefineProperty, - ObjectSetPrototypeOf, -} = primordials; - -const stream = require('stream'); - -module.exports = LazyTransform; - -function LazyTransform(options) { - this._options = options; -} -ObjectSetPrototypeOf(LazyTransform.prototype, stream.Transform.prototype); -ObjectSetPrototypeOf(LazyTransform, stream.Transform); - -function makeGetter(name) { - return function() { - stream.Transform.call(this, this._options); - this._writableState.decodeStrings = false; - return this[name]; - }; -} - -function makeSetter(name) { - return function(val) { - ObjectDefineProperty(this, name, { - __proto__: null, - value: val, - enumerable: true, - configurable: true, - writable: true, - }); - }; -} - -ObjectDefineProperties(LazyTransform.prototype, { - _readableState: { - __proto__: null, - get: makeGetter('_readableState'), - set: makeSetter('_readableState'), - configurable: true, - enumerable: true, - }, - _writableState: { - __proto__: null, - get: makeGetter('_writableState'), - set: makeSetter('_writableState'), - configurable: true, - enumerable: true, - }, -}); diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 84815969b6d1fa..47edd6ae4f9a0a 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -125,7 +125,6 @@ BuiltinLoader::BuiltinCategories BuiltinLoader::GetBuiltinCategories() const { "internal/tls/parse-cert-string", "internal/tls/secure-context", "internal/http2/core", "internal/http2/compat", "internal/policy/manifest", "internal/process/policy", - "internal/streams/lazy_transform", #endif // !HAVE_OPENSSL "sys", // Deprecated. "wasi", // Experimental.