Improve transform speed (#780)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent dd5f30f · Browse files · 3 files·+72 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+72 added−18 removed
- other:+72 added−18 removed
Provenance
- Commit
14f040e- Tree
28f697d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
dd5f30f
3 files changed+72 added−18 removed
modified:lib/stdio/generator.js+4 added-2 removed
12344567867 unmodified lines76777879808180828384858643 unmodified linesimport {generatorsToTransform} from './transform.js';import {getEncodingStartGenerator} from './encoding.js';import {getLinesGenerator} from './lines.js';import {isGeneratorOptions} from './type.js';import {isGeneratorOptions, isAsyncGenerator} from './type.js';import {isBinary, pipeStreams} from './utils.js';
export const normalizeGenerators = stdioStreams => { const nonGenerators = stdioStreams.filter(({type}) => type !== 'generator');67 unmodified lines getLinesGenerator(encoding, binary), {transform, final}, {transform: getValidateTransformReturn(readableObjectMode, optionName)}, ].filter(Boolean); const transformAsync = isAsyncGenerator(transform); const finalAsync = isAsyncGenerator(final); const duplexStream = generatorsToTransform(generators, {writableObjectMode, readableObjectMode}); const duplexStream = generatorsToTransform(generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}); return {value: duplexStream};};
const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode43 unmodified linesmodified:lib/stdio/transform.js+65 added-14 removed
1 unmodified line2 import {callbackify} from 'node:util';3 4 // Transform an array of generator functions into a `Transform` stream.5 // `Duplex.from(generator)` cannot be used because it does not allow setting the `objectMode` and `highWaterMark`.6-export const generatorsToTransform = (generators, {writableObjectMode, readableObjectMode}) => new Transform({7- writableObjectMode,8- writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),9- readableObjectMode,10- readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),11- transform(chunk, encoding, done) {12- pushChunks(transformChunk.bind(undefined, chunk, generators, 0), this, done);13- },14- flush(done) {15- pushChunks(finalChunks.bind(undefined, generators), this, done);16- },17-});6+export const generatorsToTransform = (generators, {transformAsync, finalAsync, writableObjectMode, readableObjectMode}) => {7+ const transformMethod = transformAsync8+ ? pushChunks.bind(undefined, transformChunk)9+ : pushChunksSync.bind(undefined, transformChunkSync);10+ const finalMethod = transformAsync || finalAsync11+ ? pushChunks.bind(undefined, finalChunks)12+ : pushChunksSync.bind(undefined, finalChunksSync);13 14+ return new Transform({15+ writableObjectMode,16+ writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),17+ readableObjectMode,18+ readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),19+ transform(chunk, encoding, done) {20+ transformMethod([chunk, generators, 0], this, done);21+ },22+ flush(done) {23+ finalMethod([generators], this, done);24+ },25+ });26+};27+19-const pushChunks = callbackify(async (getChunks, transformStream) => {20- for await (const chunk of getChunks()) {28+const pushChunks = callbackify(async (getChunks, args, transformStream) => {29+ for await (const chunk of getChunks(...args)) {30 transformStream.push(chunk);31 }32 });33 25 unmodified lines59 for await (const finalChunk of final()) {60 yield * transformChunk(finalChunk, generators, index + 1);61 }62 };63+64+// Duplicate the code above but as synchronous functions.65+// This is a performance optimization when the `transform`/`flush` function is synchronous, which is the common case.66+const pushChunksSync = (getChunksSync, args, transformStream, done) => {67+ try {68+ for (const chunk of getChunksSync(...args)) {69+ transformStream.push(chunk);70+ }71+72+ done();73+ } catch (error) {74+ done(error);75+ }76+};77+78+const transformChunkSync = function * (chunk, generators, index) {79+ if (index === generators.length) {80+ yield chunk;81+ return;82+ }83+84+ const {transform} = generators[index];85+ for (const transformedChunk of transform(chunk)) {86+ yield * transformChunkSync(transformedChunk, generators, index + 1);87+ }88+};89+90+const finalChunksSync = function * (generators) {91+ for (const [index, {final}] of Object.entries(generators)) {92+ yield * generatorFinalChunksSync(final, Number(index), generators);93+ }94+};95+96+const generatorFinalChunksSync = function * (final, index, generators) {97+ if (final === undefined) {98+ return;99+ }100+101+ for (const finalChunk of final()) {102+ yield * transformChunkSync(finalChunk, generators, index + 1);103+ }104+};modified:lib/stdio/type.js+3 added-2 removed
57 unmodified lines58 throw new TypeError(`The \`${optionName}\` option must use a boolean.`);59 }60 };61 62-const isGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]'63- || Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';62+const isGenerator = stdioOption => isAsyncGenerator(stdioOption) || isSyncGenerator(stdioOption);63+export const isAsyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]';64+const isSyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';65 export const isGeneratorOptions = stdioOption => typeof stdioOption === 'object'66 && stdioOption !== null67 && stdioOption.transform !== undefined;68 32 unmodified lines