Skip to content
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

stream: add fast-path for readable streams #45489

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions benchmark/streams/readable-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const common = require('../common');
const Readable = require('stream').Readable;

const BASE = 'hello world\n\n';

const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'latin1'],
len: [256, 512, 1024 * 16],
op: ['unshift', 'push'],
n: [1e3]
});

function main({ n, encoding, len, op }) {
const b = BASE.repeat(len);
const s = new Readable({
objectMode: false,
});
function noop() {}
s._read = noop;

bench.start();
switch (op) {
case 'unshift': {
for (let i = 0; i < n; i++)
s.unshift(b, encoding);
break;
}
case 'push': {
for (let i = 0; i < n; i++)
s.push(b, encoding);
break;
}
}
bench.end(n);
}
6 changes: 4 additions & 2 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
Promise,
SafeSet,
SymbolAsyncIterator,
Symbol
Symbol,
} = primordials;

module.exports = Readable;
Expand Down Expand Up @@ -73,6 +73,7 @@ const kPaused = Symbol('kPaused');

const { StringDecoder } = require('string_decoder');
const from = require('internal/streams/from');
const { encodeWithFastPath } = require('internal/streams/utils');

ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
ObjectSetPrototypeOf(Readable, Stream);
Expand Down Expand Up @@ -251,9 +252,10 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
if (addToFront && state.encoding) {
// When unshifting, if state.encoding is set, we have to save
// the string in the BufferList with the state encoding.

chunk = Buffer.from(chunk, encoding).toString(state.encoding);
} else {
chunk = Buffer.from(chunk, encoding);
chunk = encodeWithFastPath(chunk, encoding);
encoding = '';
}
}
Expand Down
32 changes: 32 additions & 0 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ const {
Symbol,
SymbolAsyncIterator,
SymbolIterator,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteOffset,
TypedArrayPrototypeGetByteLength,
} = primordials;

const { normalizeEncoding } = require('internal/util');
const { FastBuffer } = require('internal/buffer');
const { TextEncoder } = require('internal/encoding');
const { isTypedArray } = require('internal/util/types');

let buffer;
function lazyLoadBuffer() {
buffer ??= require('buffer').Buffer;
return buffer;
}
const encoder = new TextEncoder();
anonrig marked this conversation as resolved.
Show resolved Hide resolved

const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
Expand Down Expand Up @@ -261,7 +276,24 @@ function isErrored(stream) {
));
}

function encodeWithFastPath(input, encoding) {
const enc = normalizeEncoding(encoding);
const byteLength = enc === 'utf8' && isTypedArray(input) && TypedArrayPrototypeGetByteLength(input);

if (byteLength > 512) {
const buf = encoder.encode(input);
return new FastBuffer(
TypedArrayPrototypeGetBuffer(buf),
TypedArrayPrototypeGetByteOffset(buf),
byteLength,
);
}

return lazyLoadBuffer().from(input, enc);
}

module.exports = {
encodeWithFastPath,
kDestroyed,
isDisturbed,
kIsDisturbed,
Expand Down