From efdea709452e26c0f1b9b13873850e027450a434 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 | 22 +++++++++ lib/internal/crypto/cipher.js | 2 - lib/internal/crypto/hash.js | 14 +++--- lib/internal/streams/lazy_transform.js | 57 ----------------------- src/node_builtins.cc | 1 - 5 files changed, 29 insertions(+), 67 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..ba4972a54ab9f5 --- /dev/null +++ b/benchmark/crypto/hash-stream-creation2.js @@ -0,0 +1,22 @@ +// Throughput benchmark +// creates a single hasher, then pushes a bunch of 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..e6a90e9b774ba6 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -56,8 +56,6 @@ const { const assert = require('internal/assert'); -const LazyTransform = require('internal/streams/lazy_transform'); - const { normalizeEncoding } = require('internal/util'); const { StringDecoder } = require('string_decoder'); diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 57fcb63518d52d..906a83bf7a69e7 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,11 @@ function Hash(algorithm, options) { this[kState] = { [kFinalized]: false, }; - ReflectApply(LazyTransform, this, [options]); + ReflectApply(Transform, this, [options]); } -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 +133,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.