Skip to content

Commit 0b58545

Browse files
committed
http: reduce multiple output arrays into one
Now we are using `output`, `outputEncodings` and `outputCallbacks` to hold pending data. Reducing them into one array `outputData` can slightly improve performance and reduce some redundant codes. PR-URL: #26004 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 2e405e5 commit 0b58545

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

lib/_http_client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,8 @@ function socketCloseListener() {
374374
// Too bad. That output wasn't getting written.
375375
// This is pretty terrible that it doesn't raise an error.
376376
// Fixed better in v0.10
377-
if (req.output)
378-
req.output.length = 0;
379-
if (req.outputEncodings)
380-
req.outputEncodings.length = 0;
377+
if (req.outputData)
378+
req.outputData.length = 0;
381379

382380
if (parser) {
383381
parser.finish();

lib/_http_outgoing.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ function OutgoingMessage() {
7070

7171
// Queue that holds all currently pending data, until the response will be
7272
// assigned to the socket (until it will its turn in the HTTP pipeline).
73-
this.output = [];
74-
this.outputEncodings = [];
75-
this.outputCallbacks = [];
73+
this.outputData = [];
7674

7775
// `outputSize` is an approximate measure of how much data is queued on this
7876
// response. `_onPendingData` will be invoked to update similar global
@@ -220,14 +218,18 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
220218
data = this._header + data;
221219
} else {
222220
var header = this._header;
223-
if (this.output.length === 0) {
224-
this.output = [header];
225-
this.outputEncodings = ['latin1'];
226-
this.outputCallbacks = [null];
221+
if (this.outputData.length === 0) {
222+
this.outputData = [{
223+
data: header,
224+
encoding: 'latin1',
225+
callback: null
226+
}];
227227
} else {
228-
this.output.unshift(header);
229-
this.outputEncodings.unshift('latin1');
230-
this.outputCallbacks.unshift(null);
228+
this.outputData.unshift({
229+
data: header,
230+
encoding: 'latin1',
231+
callback: null
232+
});
231233
}
232234
this.outputSize += header.length;
233235
this._onPendingData(header.length);
@@ -254,7 +256,7 @@ function _writeRaw(data, encoding, callback) {
254256

255257
if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
256258
// There might be pending data in the this.output buffer.
257-
if (this.output.length) {
259+
if (this.outputData.length) {
258260
this._flushOutput(conn);
259261
} else if (!data.length) {
260262
if (typeof callback === 'function') {
@@ -273,9 +275,7 @@ function _writeRaw(data, encoding, callback) {
273275
return conn.write(data, encoding, callback);
274276
}
275277
// Buffer, as long as we're not destroyed.
276-
this.output.push(data);
277-
this.outputEncodings.push(encoding);
278-
this.outputCallbacks.push(callback);
278+
this.outputData.push({ data, encoding, callback });
279279
this.outputSize += data.length;
280280
this._onPendingData(data.length);
281281
return false;
@@ -738,7 +738,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
738738
// There is the first message on the outgoing queue, and we've sent
739739
// everything to the socket.
740740
debug('outgoing message end.');
741-
if (this.output.length === 0 &&
741+
if (this.outputData.length === 0 &&
742742
this.connection &&
743743
this.connection._httpMessage === this) {
744744
this._finish();
@@ -793,22 +793,19 @@ OutgoingMessage.prototype._flush = function _flush() {
793793

794794
OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
795795
var ret;
796-
var outputLength = this.output.length;
796+
var outputLength = this.outputData.length;
797797
if (outputLength <= 0)
798798
return ret;
799799

800-
var output = this.output;
801-
var outputEncodings = this.outputEncodings;
802-
var outputCallbacks = this.outputCallbacks;
800+
var outputData = this.outputData;
803801
socket.cork();
804802
for (var i = 0; i < outputLength; i++) {
805-
ret = socket.write(output[i], outputEncodings[i], outputCallbacks[i]);
803+
const { data, encoding, callback } = outputData[i];
804+
ret = socket.write(data, encoding, callback);
806805
}
807806
socket.uncork();
808807

809-
this.output = [];
810-
this.outputEncodings = [];
811-
this.outputCallbacks = [];
808+
this.outputData = [];
812809
this._onPendingData(-this.outputSize);
813810
this.outputSize = 0;
814811

test/parallel/test-http-destroyed-socket-write2.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ server.listen(0, function() {
6969
}
7070

7171

72-
assert.strictEqual(req.output.length, 0);
73-
assert.strictEqual(req.outputEncodings.length, 0);
72+
assert.strictEqual(req.outputData.length, 0);
7473
server.close();
7574
}));
7675

0 commit comments

Comments
 (0)