Skip to content
Myr

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

3 files+72 added−18 removed

modified:lib/stdio/generator.js+4 added-2 removed
modified:lib/stdio/transform.js+65 added-14 removed
1 unmodified line
2import {callbackify} from 'node:util';34// 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`.6export 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});6export 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);1314 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};2719const pushChunks = callbackify(async (getChunks, transformStream) => {20 for await (const chunk of getChunks()) {28const pushChunks = callbackify(async (getChunks, args, transformStream) => {29 for await (const chunk of getChunks(...args)) {30 transformStream.push(chunk);31 }32});33
25 unmodified lines
59 for await (const finalChunk of final()) {60 yield * transformChunk(finalChunk, generators, index + 1);61 }62};6364// 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.66const pushChunksSync = (getChunksSync, args, transformStream, done) => {67 try {68 for (const chunk of getChunksSync(...args)) {69 transformStream.push(chunk);70 }7172 done();73 } catch (error) {74 done(error);75 }76};7778const transformChunkSync = function * (chunk, generators, index) {79 if (index === generators.length) {80 yield chunk;81 return;82 }8384 const {transform} = generators[index];85 for (const transformedChunk of transform(chunk)) {86 yield * transformChunkSync(transformedChunk, generators, index + 1);87 }88};8990const finalChunksSync = function * (generators) {91 for (const [index, {final}] of Object.entries(generators)) {92 yield * generatorFinalChunksSync(final, Number(index), generators);93 }94};9596const generatorFinalChunksSync = function * (final, index, generators) {97 if (final === undefined) {98 return;99 }100101 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 lines
58 throw new TypeError(`The \`${optionName}\` option must use a boolean.`);59 }60};6162const isGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]'63 || Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';62const isGenerator = stdioOption => isAsyncGenerator(stdioOption) || isSyncGenerator(stdioOption);63export const isAsyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]';64const isSyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';65export const isGeneratorOptions = stdioOption => typeof stdioOption === 'object'66 && stdioOption !== null67 && stdioOption.transform !== undefined;68
32 unmodified lines