-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
stream: add kStreamBase marker for internal pipe optimization #61277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mertcanaltin
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
mertcanaltin:mert/stream/webstreams-internal-pipe-optimization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+336
−2
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58011f6
stream: add kStreamBase marker for internal pipe optimization
mertcanaltin 2828147
update comments
mertcanaltin 7fdc21f
stream: optimize internal pipe handling with optional chaining
mertcanaltin 0ef5be9
stream: enhance internal pipe handling with additional error manageme…
mertcanaltin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const fsp = require('fs/promises'); | ||
| const path = require('path'); | ||
| const os = require('os'); | ||
| const { pipeline } = require('stream/promises'); | ||
| const { | ||
| ReadableStream, | ||
| WritableStream, | ||
| } = require('node:stream/web'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| type: [ | ||
| 'node-streams', | ||
| 'webstream-js', | ||
| 'webstream-file-read', | ||
| ], | ||
| size: [1024, 16384, 65536], | ||
| n: [1e4, 1e5], | ||
| }); | ||
|
|
||
| async function main({ type, size, n }) { | ||
| const chunk = Buffer.alloc(size, 'x'); | ||
| const totalBytes = size * n; | ||
|
|
||
| switch (type) { | ||
| case 'node-streams': { | ||
| // Baseline: Node.js streams | ||
| let received = 0; | ||
| const readable = new (require('stream').Readable)({ | ||
| read() { | ||
| for (let i = 0; i < 100 && received < n; i++) { | ||
| this.push(chunk); | ||
| received++; | ||
| } | ||
| if (received >= n) this.push(null); | ||
| }, | ||
| }); | ||
|
|
||
| const writable = new (require('stream').Writable)({ | ||
| write(data, enc, cb) { cb(); }, | ||
| }); | ||
|
|
||
| bench.start(); | ||
| await pipeline(readable, writable); | ||
| bench.end(totalBytes); | ||
| break; | ||
| } | ||
|
|
||
| case 'webstream-js': { | ||
| // Web streams with pure JS source/sink | ||
| let sent = 0; | ||
| const rs = new ReadableStream({ | ||
| pull(controller) { | ||
| if (sent++ < n) { | ||
| controller.enqueue(chunk); | ||
| } else { | ||
| controller.close(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WritableStream({ | ||
| write() {}, | ||
| close() { bench.end(totalBytes); }, | ||
| }); | ||
|
|
||
| bench.start(); | ||
| await rs.pipeTo(ws); | ||
| break; | ||
| } | ||
|
|
||
| case 'webstream-file-read': { | ||
| // Create a temporary file with test data | ||
| const tmpDir = os.tmpdir(); | ||
| const tmpFile = path.join(tmpDir, `bench-webstream-${process.pid}.tmp`); | ||
|
|
||
| // Write test data to file | ||
| const fd = await fsp.open(tmpFile, 'w'); | ||
| for (let i = 0; i < n; i++) { | ||
| await fd.write(chunk); | ||
| } | ||
| await fd.close(); | ||
|
|
||
| // Read using readableWebStream | ||
| const readFd = await fsp.open(tmpFile, 'r'); | ||
| const rs = readFd.readableWebStream({ type: 'bytes' }); | ||
|
|
||
| const ws = new WritableStream({ | ||
| write() {}, | ||
| close() { | ||
| bench.end(totalBytes); | ||
| // Cleanup | ||
| readFd.close().then(() => fsp.unlink(tmpFile)); | ||
| }, | ||
| }); | ||
|
|
||
| bench.start(); | ||
| await rs.pipeTo(ws); | ||
| break; | ||
| } | ||
|
|
||
| default: | ||
| throw new Error(`Unknown type: ${type}`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Flags: --expose-internals --no-warnings | ||
| 'use strict'; | ||
|
|
||
| // Tests for the internal StreamBase pipe optimization infrastructure | ||
| // described in nodejs/performance#134 | ||
| // | ||
| // Note(mertcanaltin): Full fast-path testing requires real StreamBase implementations | ||
| // (like HTTP/2 streams or TCP sockets), not JSStream mocks. | ||
| // These tests verify the marker attachment and fallback behavior. | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| const assert = require('assert'); | ||
|
|
||
| const { | ||
| internalBinding, | ||
| } = require('internal/test/binding'); | ||
|
|
||
| const { | ||
| newWritableStreamFromStreamBase, | ||
| newReadableStreamFromStreamBase, | ||
| } = require('internal/webstreams/adapters'); | ||
|
|
||
| const { | ||
| kStreamBase, | ||
| } = require('internal/webstreams/util'); | ||
|
|
||
| const { | ||
| JSStream, | ||
| } = internalBinding('js_stream'); | ||
|
|
||
| // kStreamBase marker is attached to ReadableStream | ||
| { | ||
| const stream = new JSStream(); | ||
| const readable = newReadableStreamFromStreamBase(stream); | ||
|
|
||
| assert.strictEqual(readable[kStreamBase], stream); | ||
|
|
||
| // Cleanup | ||
| stream.emitEOF(); | ||
| } | ||
|
|
||
| // kStreamBase marker is attached to WritableStream | ||
| { | ||
| const stream = new JSStream(); | ||
| stream.onwrite = common.mustNotCall(); | ||
| stream.onshutdown = (req) => req.oncomplete(); | ||
|
|
||
| const writable = newWritableStreamFromStreamBase(stream); | ||
|
|
||
| assert.strictEqual(writable[kStreamBase], stream); | ||
|
|
||
| // Cleanup | ||
| writable.close(); | ||
| } | ||
|
|
||
| // Regular JS streams don't have kStreamBase | ||
| { | ||
| const { ReadableStream, WritableStream } = require('stream/web'); | ||
|
|
||
| const rs = new ReadableStream({ | ||
| pull(controller) { | ||
| controller.enqueue('chunk'); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WritableStream({ | ||
| write() {}, | ||
| }); | ||
|
|
||
| assert.strictEqual(rs[kStreamBase], undefined); | ||
| assert.strictEqual(ws[kStreamBase], undefined); | ||
|
|
||
| // Pipe should still work (standard path) | ||
| rs.pipeTo(ws).then(common.mustCall()); | ||
| } | ||
|
|
||
| // Mixed streams (one internal, one JS) use standard path | ||
| { | ||
| const stream = new JSStream(); | ||
| stream.onshutdown = (req) => req.oncomplete(); | ||
| const readable = newReadableStreamFromStreamBase(stream); | ||
|
|
||
| const { WritableStream } = require('stream/web'); | ||
| const chunks = []; | ||
| const ws = new WritableStream({ | ||
| write(chunk) { | ||
| chunks.push(chunk); | ||
| }, | ||
| }); | ||
|
|
||
| // Readable has kStreamBase, ws does not - should use standard path | ||
| assert.ok(readable[kStreamBase]); | ||
| assert.strictEqual(ws[kStreamBase], undefined); | ||
|
|
||
| const pipePromise = readable.pipeTo(ws); | ||
|
|
||
| stream.readBuffer(Buffer.from('hello')); | ||
| stream.emitEOF(); | ||
|
|
||
| pipePromise.then(common.mustCall(() => { | ||
| assert.strictEqual(chunks.length, 1); | ||
| })); | ||
| } | ||
|
|
||
| // Verify kStreamBase is the correct symbol from util | ||
| { | ||
| const { | ||
| kStreamBase: kStreamBase2, | ||
| } = require('internal/webstreams/util'); | ||
|
|
||
| // Should be the same symbol | ||
| assert.strictEqual(kStreamBase, kStreamBase2); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.