stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

stream: keep webstream stream states in fast-mode objects - #65625

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16
Open

stream: keep webstream stream states in fast-mode objects#65625
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:webstream-perf-round16

Conversation

@mcollina

Copy link
Copy Markdown
Member

While profiling short-lived streams (the per-request create β†’ few chunks β†’ close pattern), createReadableStreamState showed up at 20% self time. The cause: the per-stream state records are object literals with __proto__: null, which V8 creates as dictionary-mode objects β€” roughly 50x slower to allocate (~500-1500ns vs ~30ns), and every subsequent property load on them is a dictionary lookup. These records back every hot path, so both construction and per-chunk [kState] accesses have been paying for it.

The first commit replaces the four per-stream state literals (and their nested transfer records) with classes whose prototype has a null prototype: instances stay in fast mode, while Object.prototype remains excluded from the lookup chain, preserving the pollution protection the literals were there for. Every field ever assigned is declared up front so the shape never transitions. (The controller and reader states were already plain literals and unaffected.) It also adds benchmark/webstreams/lifecycle.js covering the short-lived stream pattern that exposed this.

The second commit removes the per-chunk PromiseWithResolvers for transform sink writes parked on backpressure: the writable controller's write reactions exist before the write algorithm runs, so the parked-write continuation can wire the perform-transform promise directly to them via the parked-result sentinel introduced in #65143, dropping the promise record and the thenable adoption hop.

Results (30 runs, all 36 rows significant at ***, no regressions β€” full table below):

 confidence improvement accuracy (*) (**) (***)
webstreams/creation.js kind='ReadableStream.tee' n=50000 *** 71.39 % Β±11.02% Β±14.79% Β±19.52%
webstreams/creation.js kind='ReadableStream' n=50000 *** 138.13 % Β±14.29% Β±19.14% Β±25.17%
webstreams/creation.js kind='ReadableStreamBYOBReader' n=50000 *** 74.72 % Β±9.66% Β±12.97% Β±17.11%
webstreams/creation.js kind='ReadableStreamDefaultReader' n=50000 *** 75.04 % Β±11.10% Β±14.87% Β±19.58%
webstreams/creation.js kind='TransformStream' n=50000 *** 133.64 % Β±8.61% Β±11.46% Β±14.94%
webstreams/creation.js kind='WritableStream' n=50000 *** 204.00 % Β±7.70% Β±10.26% Β±13.37%
webstreams/encoding-streams.js len=1024 kind='decode' n=100000 *** 57.21 % Β±3.11% Β±4.15% Β±5.42%
webstreams/encoding-streams.js len=1024 kind='encode' n=100000 *** 31.57 % Β±1.86% Β±2.48% Β±3.24%
webstreams/encoding-streams.js len=16 kind='decode' n=100000 *** 57.54 % Β±3.19% Β±4.27% Β±5.61%
webstreams/encoding-streams.js len=16 kind='encode' n=100000 *** 44.84 % Β±2.38% Β±3.17% Β±4.15%
webstreams/from.js kind='async' n=1000000 *** 32.60 % Β±1.34% Β±1.79% Β±2.35%
webstreams/from.js kind='sync' n=1000000 *** 39.66 % Β±1.41% Β±1.88% Β±2.44%
webstreams/js_transfer.js n=10000 payload='ReadableStream' *** 10.98 % Β±1.56% Β±2.08% Β±2.72%
webstreams/js_transfer.js n=10000 payload='TransformStream' *** 12.90 % Β±0.88% Β±1.18% Β±1.53%
webstreams/js_transfer.js n=10000 payload='WritableStream' *** 11.77 % Β±0.96% Β±1.28% Β±1.67%
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 96.41 % Β±3.41% Β±4.58% Β±6.03%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 80.15 % Β±7.47% Β±10.02% Β±13.18%
webstreams/lifecycle.js kind='readable' n=50000 *** 80.04 % Β±3.10% Β±4.15% Β±5.45%
webstreams/pipe-through.js kind='default' n=500000 *** 91.16 % Β±2.56% Β±3.43% Β±4.52%
webstreams/pipe-through.js kind='transform' n=500000 *** 93.68 % Β±3.12% Β±4.19% Β±5.53%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1 n=500000 *** 110.48 % Β±2.91% Β±3.89% Β±5.11%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=1024 n=500000 *** 105.83 % Β±3.89% Β±5.22% Β±6.89%
webstreams/pipe-to.js highWaterMarkW=1 highWaterMarkR=4096 n=500000 *** 109.78 % Β±2.47% Β±3.30% Β±4.33%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1 n=500000 *** 110.77 % Β±2.07% Β±2.78% Β±3.64%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=1024 n=500000 *** 109.64 % Β±2.43% Β±3.26% Β±4.30%
webstreams/pipe-to.js highWaterMarkW=1024 highWaterMarkR=4096 n=500000 *** 111.70 % Β±2.52% Β±3.38% Β±4.46%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1 n=500000 *** 110.87 % Β±2.50% Β±3.34% Β±4.37%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=1024 n=500000 *** 108.40 % Β±2.36% Β±3.15% Β±4.13%
webstreams/pipe-to.js highWaterMarkW=4096 highWaterMarkR=4096 n=500000 *** 109.79 % Β±2.49% Β±3.32% Β±4.36%
webstreams/readable-async-iterator.js type='bytes' n=100000 *** 12.19 % Β±2.06% Β±2.74% Β±3.57%
webstreams/readable-async-iterator.js type='normal' n=100000 *** 15.03 % Β±4.79% Β±6.37% Β±8.30%
webstreams/readable-read-buffered.js bufferSize=1 n=100000 *** 22.05 % Β±5.25% Β±6.99% Β±9.10%
webstreams/readable-read-buffered.js bufferSize=10 n=100000 *** 19.89 % Β±3.04% Β±4.04% Β±5.26%
webstreams/readable-read-buffered.js bufferSize=100 n=100000 *** 27.79 % Β±5.78% Β±7.70% Β±10.04%
webstreams/readable-read-buffered.js bufferSize=1000 n=100000 *** 25.51 % Β±5.83% Β±7.76% Β±10.11%
webstreams/readable-read.js type='byob' n=100000 *** 5.82 % Β±1.45% Β±1.93% Β±2.51%
webstreams/readable-read.js type='normal' n=100000 *** 34.63 % Β±4.79% Β±6.38% Β±8.31%
webstreams/tee.js type='bytes' n=100000 *** 13.65 % Β±1.49% Β±1.98% Β±2.58%
webstreams/tee.js type='normal' n=100000 *** 15.97 % Β±2.70% Β±3.59% Β±4.68%

Validated with the full webstreams test suite, WPT streams/compression/encoding, and two differential stress harnesses (adapter scenarios and transform parked-write error/abort/terminate/reentrancy scenarios) whose observable event logs are byte-identical to the previous implementation.

Other lib/ modules use the same __proto__: null literal pattern on hot paths; I'll follow up separately after auditing them.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. web streams Issues and PRs related to the Web Streams API. labels Aug 28, 2026
@codecov

codecovBot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.05%. Comparing base (7aaf9b4) to head (aaa47dc).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65625 +/- ##
==========================================
- Coverage 90.13% 90.05% -0.08% 
==========================================
Files 751 754 +3 Lines 253639 256415 +2776 Branches 47790 48509 +719 ==========================================
+ Hits 228618 230921 +2303 - Misses 16264 16620 +356 - Partials 8757 8874 +117 
Files with missing linesCoverage Ξ”
lib/internal/webstreams/readablestream.js87.23% <100.00%> (+0.02%)⬆️
lib/internal/webstreams/transformstream.js94.58% <100.00%> (+0.60%)⬆️
lib/internal/webstreams/writablestream.js93.78% <100.00%> (+0.30%)⬆️

... and 110 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment threadbenchmark/webstreams/lifecycle.js Outdated
Comment threadlib/internal/webstreams/readablestream.js Outdated

@Abhirup0Abhirup0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified with %HasFastProperties() that state instances maintain fast-mode Maps without falling back to dictionary mode.

One minor observation: the writableState.state === 'erroring' branch in writeContinuation (lib/internal/webstreams/transformstream.js:628–633) is currently unhit in tests. It might be worth adding a quick test case where a write is parked under backpressure and the writable stream is aborted prior to the next read/pull.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 36d534f to 961f2ceCompareAugust 30, 2026 06:22
@mcollina

Copy link
Copy Markdown
MemberAuthor

@Abhirup0 added test/parallel/test-whatwg-transformstream-parked-write-error.js covering that branch via readable.cancel(), controller.error(), and controller.terminate() with a parked write in flight (verified with an instrumented build that all three hit it). Interesting detail: writer.abort() alone does not reach it β€” the abort settles the in-flight write through the writable's erroring machinery without the backpressure flip β€” and a write issued before the start algorithm settles never parks at all, so the scenarios wait for start first.

@github-actions

github-actionsBot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (webstreams / lifecycle.js): https://github.com/nodejs/node/actions/runs/33296873656

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

 confidence improvement accuracy (*) (**) (***)
webstreams/lifecycle.js kind='pipe-through' n=50000 *** 77.75 % Β±11.08% Β±14.62% Β±18.79%
webstreams/lifecycle.js kind='pipe-to' n=50000 *** 76.04 % Β±10.59% Β±13.98% Β±17.96%
webstreams/lifecycle.js kind='readable' n=50000 *** 66.28 % Β±9.90% Β±13.06% Β±16.77%
Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 3 comparisons, you can thus
expect the following amount of false-positive results:
0.15 false positives, when considering a 5% risk acceptance (*, **, ***),
0.03 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 961f2ce to 9f6906dCompareSeptember 1, 2026 14:37
The per-stream state records were built as object literals with
__proto__: null, which V8 creates as dictionary-mode objects: roughly
50x slower to allocate, and every subsequent property load on them is a
dictionary lookup. These records back every hot path, so both stream
construction and per-chunk field accesses were paying for it.
Replace the literals with classes whose prototype has a null prototype,
so instances stay in fast mode while Object.prototype remains excluded
from the lookup chain. Every field ever assigned is declared up front
so the shape never transitions.
Also add benchmark/webstreams/lifecycle.js covering the short-lived
stream pattern (create, few chunks, close) that first exposed this.
Assisted-by: ChatGPT
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollinamcollina added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 9f6906d to 2943389CompareSeptember 1, 2026 14:46
@aduh95aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 1, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

A transform sink write arriving under backpressure parked the chunk
together with a PromiseWithResolvers record whose promise was returned
to the writable controller and later resolved with the perform-transform
promise. The writable's write reactions already exist before the write
algorithm runs, so the parked write can instead return the parked-result
sentinel and have the continuation wire the perform-transform promise
directly to those reactions, dropping the per-chunk promise record and
the thenable adoption hop. Failures while erroring are delivered in a
microtask, preserving the old rejection position.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollinaforce-pushed the webstream-perf-round16 branch from 2943389 to aaa47dcCompareSeptember 2, 2026 05:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.web streamsIssues and PRs related to the Web Streams API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@mcollina@nodejs-github-bot@jasnell@H4ad@aduh95@gurgunday@Abhirup0