stream: keep webstreams nil requests in fast mode

The shared "no pending request" records in the writable stream were
`__proto__: null` literals, which V8 creates in dictionary mode. They
sit in inFlightWriteRequest, closeRequest and pendingAbortRequest
whenever nothing is pending, and their promise field is checked several
times per write, so those loads did a hash lookup on every write and
every pipe. They are now built as plain literals and get their null
prototype afterwards, which keeps them in fast mode.

The readable controllers also initialized their state slot with an
empty object that setup replaced immediately. That throwaway allocation
is gone, matching the writable and transform controllers.

Add a writable-write benchmark: nothing in benchmark/webstreams drove
WritableStreamDefaultWriter.write() directly.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/66230
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-09-29 00:25:12 +02:00
1 parent b7f6a66a9f
commit e241cb80da
4 files changed
+42 -9

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
'use strict';
const common = require('../common.js');
const assert = require('node:assert');
const { WritableStream } = require('node:stream/web');
const bench = common.createBenchmark(main, {
n: [1e5],
type: ['await', 'queued'],
});
async function main({ n, type }) {
let count = 0;
const ws = new WritableStream({
write() {
count++;
},
}, { highWaterMark: type === 'queued' ? n : 1 });
const writer = ws.getWriter();
bench.start();
if (type === 'await') {
for (let i = 0; i < n; i++)
await writer.write('a');
} else {
for (let i = 1; i < n; i++)
writer.write('a');
await writer.write('a');
}
bench.end(n);
await writer.close();
assert.strictEqual(count, n);
}
+2 -2
View File
@@ -1140,7 +1140,7 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, {
class ReadableStreamDefaultController {
[kType] = 'ReadableStreamDefaultController';
[kState] = {};
[kState];
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
@@ -1203,7 +1203,7 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, {
class ReadableByteStreamController {
[kType] = 'ReadableByteStreamController';
[kState] = {};
[kState];
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
@@ -303,6 +303,7 @@ TransferredTransformStream.prototype[kDeserialize] = () => {};
class TransformStreamDefaultController {
[kType] = 'TransformStreamDefaultController';
[kState];
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
+8 -7
View File
@@ -105,19 +105,19 @@ const kSkipThrow = Symbol('kSkipThrow');
// Shared sentinels for the "no pending request" state records. These
// records are only ever replaced wholesale and never mutated in place,
// so single shared instances are safe and avoid an allocation on every
// state reset (one per write on the hot path).
const kNilRequest = {
__proto__: null,
// state reset (one per write on the hot path). The prototype is nulled
// after creation: a `__proto__: null` literal is created in dictionary
// mode, and these fields are loaded several times per write.
const kNilRequest = ObjectSetPrototypeOf({
promise: undefined,
resolve: undefined,
reject: undefined,
};
const kNilPendingAbortRequest = {
__proto__: null,
}, null);
const kNilPendingAbortRequest = ObjectSetPrototypeOf({
abort: kNilRequest,
reason: undefined,
wasAlreadyErroring: false,
};
}, null);
let releasedError;
@@ -502,6 +502,7 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, {
class WritableStreamDefaultController {
[kType] = 'WritableStreamDefaultController';
[kState];
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {