Transform a one-chunk string with one push and no stream state #4

Closed natemoo.re
targeting jsx-automatic from one-chunk-transform
Closed opened by natemoo.re
targeting jsx-automatic from one-chunk-transform

Improves memory use of transform and transformSync on a string, which now allocate about a quarter of what they did per call.

What changes #

A whole-string call went through the full stream: open()'s closures and state, the JSX option strings encoded again on every call, two pushes, and two decoded pieces joined at the end. A string that fits in one 64Ki-character chunk now makes one push with fin set and one decode, and the JSX option bytes are encoded once per option value. Longer input, and erase-mode input with a lone surrogate, still go through the stream.

Results #

before after
fixed allocation per call 2,411 B 666 B
allocated per pass, TSX sample 17.5 MiB 14.5 MiB
first-pass RSS growth, TSX automatic +18.6 MiB +15.6 MiB
first-pass RSS growth, 20MB .ts sample +31 MiB +28 MiB

Throughput is unchanged within noise. Of the 14.5 MiB, 12.4 MiB is the output strings themselves.

Verification #

pnpm test, the TypeScript erase sweep and the TSX sample pass.

Stacked on #3.

Commits 1

Transform a one-chunk string with one push and no stream state