Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 20dc54d

Browse files
committedOct 12, 2023
http: reduce parts in chunked response when corking
Refs: nodejs/performance#57 PR-URL: #50167
1 parent ac2a68c commit 20dc54d

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed
 

‎lib/_http_outgoing.js

+50-13
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
8282
});
8383

8484
const kCorked = Symbol('corked');
85+
const kChunkedBuffer = Symbol('kChunkedBuffer');
86+
const kChunkedLength = Symbol('kChunkedLength');
8587
const kUniqueHeaders = Symbol('kUniqueHeaders');
8688
const kBytesWritten = Symbol('kBytesWritten');
8789
const kErrored = Symbol('errored');
@@ -140,6 +142,8 @@ function OutgoingMessage(options) {
140142
this.finished = false;
141143
this._headerSent = false;
142144
this[kCorked] = 0;
145+
this[kChunkedBuffer] = [];
146+
this[kChunkedLength] = 0;
143147
this._closed = false;
144148

145149
this.socket = null;
@@ -192,7 +196,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
192196
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
193197
__proto__: null,
194198
get() {
195-
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
199+
return this.outputSize + this[kChunkedLength] + (this.socket ? this.socket.writableLength : 0);
196200
},
197201
});
198202

@@ -206,8 +210,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
206210
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
207211
__proto__: null,
208212
get() {
209-
const corked = this.socket ? this.socket.writableCorked : 0;
210-
return corked + this[kCorked];
213+
return this[kCorked];
211214
},
212215
});
213216

@@ -299,19 +302,45 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
299302
};
300303

301304
OutgoingMessage.prototype.cork = function() {
305+
this[kCorked]++;
302306
if (this.socket) {
303307
this.socket.cork();
304-
} else {
305-
this[kCorked]++;
306308
}
307309
};
308310

309311
OutgoingMessage.prototype.uncork = function() {
312+
this[kCorked]--;
310313
if (this.socket) {
311314
this.socket.uncork();
312-
} else if (this[kCorked]) {
313-
this[kCorked]--;
314315
}
316+
317+
if (this[kCorked] || this[kChunkedBuffer].length === 0) {
318+
return;
319+
}
320+
321+
const len = this[kChunkedLength];
322+
const buf = this[kChunkedBuffer];
323+
324+
assert(this.chunkedEncoding);
325+
326+
let callbacks;
327+
this._send(NumberPrototypeToString(len, 16), 'latin1', null);
328+
this._send(crlf_buf, null, null);
329+
for (let n = 0; n < buf.length; n += 3) {
330+
this._send(buf[n + 0], buf[n + 1], null);
331+
if (buf[n + 2]) {
332+
callbacks ??= [];
333+
callbacks.push(buf[n + 2]);
334+
}
335+
}
336+
this._send(crlf_buf, null, callbacks.length ? (err) => {
337+
for (const callback of callbacks) {
338+
callback(err);
339+
}
340+
} : null);
341+
342+
this[kChunkedBuffer].length = 0;
343+
this[kChunkedLength] = 0;
315344
};
316345

317346
OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
@@ -937,11 +966,18 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
937966

938967
let ret;
939968
if (msg.chunkedEncoding && chunk.length !== 0) {
940-
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
941-
msg._send(NumberPrototypeToString(len, 16), 'latin1', null);
942-
msg._send(crlf_buf, null, null);
943-
msg._send(chunk, encoding, null, len);
944-
ret = msg._send(crlf_buf, null, callback);
969+
if (msg[kCorked] && msg._headerSent) {
970+
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
971+
msg[kChunkedBuffer].push(chunk, encoding, callback);
972+
msg[kChunkedLength] += len;
973+
ret = msg[kChunkedLength] < msg[kHighWaterMark];
974+
} else {
975+
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
976+
msg._send(NumberPrototypeToString(len, 16), 'latin1', null);
977+
msg._send(crlf_buf, null, null);
978+
msg._send(chunk, encoding, null, len);
979+
ret = msg._send(crlf_buf, null, callback);
980+
}
945981
} else {
946982
ret = msg._send(chunk, encoding, callback, len);
947983
}
@@ -1068,7 +1104,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
10681104
this.socket._writableState.corked = 1;
10691105
this.socket.uncork();
10701106
}
1071-
this[kCorked] = 0;
1107+
this[kCorked] = 1;
1108+
this.uncork();
10721109

10731110
this.finished = true;
10741111

0 commit comments

Comments
 (0)
Please sign in to comment.