Skip to content

http: reduce parts in chunked response when corking

Avoid splitting chunked responses into multiple parts when corking. This makes a large difference performance-wise on the client side.

Previously when writing to a chunked response Node would create a separate chunk for each call to .write(...) regardless whether the response was corked or not. This leads to unnecessary overhead both on the client and server side.

This PR fixes this by creating a single chunk for all calls to write(...) when uncorking the response.

Consider the following example based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding#chunked_encoding:

res.cork();
res.write('Mozilla');
res.write(' Developer Network');
res.uncork();

At the beginning of each chunk you need to add the length of the current chunk in hexadecimal format, followed by '\r\n' and then the chunk itself, followed by another '\r\n'. The terminating chunk is a regular chunk, with the exception that its length is zero.

Resulting in a response stream looking like this:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

7\r\n
Mozilla\r\n
18\r\n
 Developer Network\r\n
0\r\n
\r\n

After this PR everything is combined into a single chunk when uncorking the response bypassing a lot of unnecessary overhead.

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25\r\n
Mozilla Developer Network\r\n
0\r\n
\r\n

Refs: https://github.com/nodejs/performance/issues/57

Merge request reports

Loading