next.js
ee06c616 - Write the chunks of a web stream to the response as Buffers, without a copy

Commit
22 days ago
Write the chunks of a web stream to the response as Buffers, without a copy pipeToNodeResponse wrote each Uint8Array chunk of a web stream to the response as it was. The compression middleware copies every chunk that is not a Buffer before it passes it to zlib, so a compressed response allocated a second copy of its body. V8 counts an ArrayBuffer as external memory until a GC sweeps it, and adds external memory allocated since the last mark-compact to the old generation when it checks its limit. Since the request graph is no longer promoted, that limit leaves little slack, and the copies filled it: compressed responses under serial load ran major GCs where scavenges would do. Hand the response a Buffer over the same memory instead: same bytes, same offset, no copy, as pipeNodeReadableToNodeResponse already does. Without the compression middleware nothing changes: a Node.js stream wraps a Uint8Array in the same view. With it, the response now refers to the chunk's memory until zlib has read it, where the middleware used to copy it at once. That is what the response already does whenever it is not compressed (the socket is corked until the next tick), so a stream must not change a chunk after enqueuing it either way. An empty chunk is passed on as it is: there is nothing to copy, and a view cannot be created on a detached buffer.
Author
Committer
Parents
Loading