Commit d5af3894004 for nodejs
commit d5af3894004d676b85fda9547ddf73d50481fea3
Author: Matteo Collina <hello@matteocollina.com>
Date: Mon Nov 16 21:37:16 2026 +0100
stream: use distinct webstreams brand checks
The brand checks were closures returned by one isBrandCheck() factory.
Closures of the same function literal share their feedback vector, so
the kState and kType loads in every check saw all the stream classes
and went megamorphic. Declare each check as its own function.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/67433
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js
index 3181355959b..f35d192f22d 100644
--- a/lib/internal/webstreams/readablestream.js
+++ b/lib/internal/webstreams/readablestream.js
@@ -112,7 +112,6 @@ const {
extractHighWaterMark,
extractSizeAlgorithm,
getNonWritablePropertyDescriptor,
- isBrandCheck,
kDeferredStart,
kEmptyQueue,
kParkedAlgorithmResult,
@@ -1385,18 +1384,44 @@ function createReadableByteStream(start, pull, cancel) {
return stream;
}
-const isReadableStream =
- isBrandCheck('ReadableStream');
-const isReadableByteStreamController =
- isBrandCheck('ReadableByteStreamController');
-const isReadableStreamDefaultController =
- isBrandCheck('ReadableStreamDefaultController');
-const isReadableStreamBYOBRequest =
- isBrandCheck('ReadableStreamBYOBRequest');
-const isReadableStreamDefaultReader =
- isBrandCheck('ReadableStreamDefaultReader');
-const isReadableStreamBYOBReader =
- isBrandCheck('ReadableStreamBYOBReader');
+// Separate functions rather than closures from one factory: closures of
+// the same function literal share their feedback, which made these loads
+// megamorphic across all the stream classes.
+function isReadableStream(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableStream';
+}
+
+function isReadableByteStreamController(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableByteStreamController';
+}
+
+function isReadableStreamDefaultController(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableStreamDefaultController';
+}
+
+function isReadableStreamBYOBRequest(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableStreamBYOBRequest';
+}
+
+function isReadableStreamDefaultReader(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableStreamDefaultReader';
+}
+
+function isReadableStreamBYOBReader(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'ReadableStreamBYOBReader';
+}
// ---- ReadableStream Implementation
diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js
index 60c7bd65bb9..e9cf0d9978e 100644
--- a/lib/internal/webstreams/transformstream.js
+++ b/lib/internal/webstreams/transformstream.js
@@ -55,7 +55,6 @@ const {
extractHighWaterMark,
extractSizeAlgorithm,
getNonWritablePropertyDescriptor,
- isBrandCheck,
kParkedAlgorithmResult,
kResolvedPromise,
kState,
@@ -372,10 +371,20 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, {
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name),
});
-const isTransformStream =
- isBrandCheck('TransformStream');
-const isTransformStreamDefaultController =
- isBrandCheck('TransformStreamDefaultController');
+// Separate functions rather than closures from one factory: closures of
+// the same function literal share their feedback, which made these loads
+// megamorphic across all the stream classes.
+function isTransformStream(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'TransformStream';
+}
+
+function isTransformStreamDefaultController(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'TransformStreamDefaultController';
+}
// Raw callback (see createRawCallback*): invoked inside the try/catch of
// transformStreamDefaultControllerPerformTransform.
diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js
index 2d9ba0ae403..79a512b05dd 100644
--- a/lib/internal/webstreams/util.js
+++ b/lib/internal/webstreams/util.js
@@ -138,14 +138,6 @@ function canCopyArrayBuffer(toBuffer, toIndex, fromBuffer, fromIndex, count) {
fromIndex + count <= ArrayBufferPrototypeGetByteLength(fromBuffer);
}
-function isBrandCheck(brand) {
- return (value) => {
- return value != null &&
- value[kState] !== undefined &&
- value[kType] === brand;
- };
-}
-
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
// instead of a plain array. Entries are pushed at the tail and consumed
// at the head, so a plain array either moves every element or forces the
@@ -497,7 +489,6 @@ module.exports = {
extractHighWaterMark,
extractSizeAlgorithm,
getNonWritablePropertyDescriptor,
- isBrandCheck,
isPromisePending,
kDeferredStart,
kEmptyQueue,
diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js
index f332fd19fd0..1cf10984d3d 100644
--- a/lib/internal/webstreams/writablestream.js
+++ b/lib/internal/webstreams/writablestream.js
@@ -63,7 +63,6 @@ const {
extractHighWaterMark,
extractSizeAlgorithm,
getNonWritablePropertyDescriptor,
- isBrandCheck,
isPromisePending,
kDeferredStart,
kEmptyQueue,
@@ -573,12 +572,26 @@ function createWritableStream(start, write, close, abort, highWaterMark = 1, siz
return stream;
}
-const isWritableStream =
- isBrandCheck('WritableStream');
-const isWritableStreamDefaultWriter =
- isBrandCheck('WritableStreamDefaultWriter');
-const isWritableStreamDefaultController =
- isBrandCheck('WritableStreamDefaultController');
+// Separate functions rather than closures from one factory: closures of
+// the same function literal share their feedback, which made these loads
+// megamorphic across all the stream classes.
+function isWritableStream(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'WritableStream';
+}
+
+function isWritableStreamDefaultWriter(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'WritableStreamDefaultWriter';
+}
+
+function isWritableStreamDefaultController(value) {
+ return value != null &&
+ value[kState] !== undefined &&
+ value[kType] === 'WritableStreamDefaultController';
+}
// The state record classes null out their prototype chain to keep
// Object.prototype out of property lookups; every field ever assigned