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>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-11-19 15:42:19 +01:00
1 parent a4d6596b44
commit 52c421872a
4 files changed
+72 -34

No files matched your search

+38 -13
View File
@@ -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
+14 -5
View File
@@ -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.
-9
View File
@@ -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,
+20 -7
View File
@@ -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