Skip to content

Commit 90c9f1d

Browse files
starkwangtargos
authored andcommitted
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 <info@bnoordhuis.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
1 parent 6c6e678 commit 90c9f1d

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

lib/_http_client.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,8 @@ function socketCloseListener() {
376376
// Too bad. That output wasn't getting written.
377377
// This is pretty terrible that it doesn't raise an error.
378378
// Fixed better in v0.10
379-
if (req.output)
380-
req.output.length = 0;
381-
if (req.outputEncodings)
382-
req.outputEncodings.length = 0;
379+
if (req.outputData)
380+
req.outputData.length = 0;
383381

384382
if (parser) {
385383
parser.finish();

lib/_http_outgoing.js

+20-23
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
@@ -219,14 +217,18 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
219217
data = this._header + data;
220218
} else {
221219
var header = this._header;
222-
if (this.output.length === 0) {
223-
this.output = [header];
224-
this.outputEncodings = ['latin1'];
225-
this.outputCallbacks = [null];
220+
if (this.outputData.length === 0) {
221+
this.outputData = [{
222+
data: header,
223+
encoding: 'latin1',
224+
callback: null
225+
}];
226226
} else {
227-
this.output.unshift(header);
228-
this.outputEncodings.unshift('latin1');
229-
this.outputCallbacks.unshift(null);
227+
this.outputData.unshift({
228+
data: header,
229+
encoding: 'latin1',
230+
callback: null
231+
});
230232
}
231233
this.outputSize += header.length;
232234
this._onPendingData(header.length);
@@ -253,7 +255,7 @@ function _writeRaw(data, encoding, callback) {
253255

254256
if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
255257
// There might be pending data in the this.output buffer.
256-
if (this.output.length) {
258+
if (this.outputData.length) {
257259
this._flushOutput(conn);
258260
} else if (!data.length) {
259261
if (typeof callback === 'function') {
@@ -272,9 +274,7 @@ function _writeRaw(data, encoding, callback) {
272274
return conn.write(data, encoding, callback);
273275
}
274276
// Buffer, as long as we're not destroyed.
275-
this.output.push(data);
276-
this.outputEncodings.push(encoding);
277-
this.outputCallbacks.push(callback);
277+
this.outputData.push({ data, encoding, callback });
278278
this.outputSize += data.length;
279279
this._onPendingData(data.length);
280280
return false;
@@ -737,7 +737,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
737737
// There is the first message on the outgoing queue, and we've sent
738738
// everything to the socket.
739739
debug('outgoing message end.');
740-
if (this.output.length === 0 &&
740+
if (this.outputData.length === 0 &&
741741
this.connection &&
742742
this.connection._httpMessage === this) {
743743
this._finish();
@@ -792,22 +792,19 @@ OutgoingMessage.prototype._flush = function _flush() {
792792

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

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

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

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

+1-2
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)