mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
http: improve performance with known-length calls to end()
This boosts RPS performance for the common API case where you call `res.end(data)` with the entire response by up to 9%. Signed-off-by: Tim Perry <pimterry@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65466 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
1 parent
367549eed5
commit
75df6cb435
3 files changed
+80
-17
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// Responses sent as a single res.end(string) with a known Content-Length -
|
||||
// the shape a JSON or HTML endpoint produces.
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
len: [4, 64, 1024, 16384, 102400],
|
||||
c: [50],
|
||||
duration: 5,
|
||||
});
|
||||
|
||||
function main({ len, c, duration }) {
|
||||
const http = require('http');
|
||||
const body = 'a'.repeat(len);
|
||||
const headers = {
|
||||
'Content-Type': 'text/plain',
|
||||
'Content-Length': `${len}`,
|
||||
};
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, headers);
|
||||
res.end(body);
|
||||
});
|
||||
|
||||
server.listen(0, () => {
|
||||
bench.http({
|
||||
connections: c,
|
||||
duration,
|
||||
port: server.address().port,
|
||||
}, () => {
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
+43
-8
@@ -1026,6 +1026,25 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
|
||||
}
|
||||
|
||||
|
||||
// If this last write can be delivered immediately as the final chunk, this
|
||||
// prepares to do so, and then returns true. If not, it returns false and
|
||||
// a separate _send call and tick will be required to finish up.
|
||||
function maybePrepareFinalChunk(msg, chunk, encoding) {
|
||||
if (typeof chunk !== 'string' && !isUint8Array(chunk))
|
||||
return false;
|
||||
|
||||
if (msg.destroyed || msg.strictContentLength)
|
||||
return false;
|
||||
|
||||
if (!msg._header) {
|
||||
msg._contentLength = typeof chunk === 'string' ?
|
||||
Buffer.byteLength(chunk, encoding) : chunk.byteLength;
|
||||
msg._implicitHeader();
|
||||
}
|
||||
|
||||
return !!msg._header && msg._hasBody && !msg.chunkedEncoding;
|
||||
}
|
||||
|
||||
function connectionCorkNT(conn) {
|
||||
conn.uncork();
|
||||
}
|
||||
@@ -1131,6 +1150,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
let finishCallback = null;
|
||||
|
||||
if (chunk) {
|
||||
if (this.finished) {
|
||||
onError(this,
|
||||
@@ -1143,7 +1164,18 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
|
||||
this[kSocket].cork();
|
||||
}
|
||||
|
||||
write_(this, chunk, encoding, null, true);
|
||||
if (maybePrepareFinalChunk(this, chunk, encoding)) {
|
||||
// If just one final write is required, with nothing to follow, we
|
||||
// attach finish to the write to avoid a separate send() & tick step
|
||||
// later on - this is purely a performance optimization.
|
||||
if (typeof callback === 'function') {
|
||||
queueEndCallback(this, callback);
|
||||
callback = undefined;
|
||||
}
|
||||
finishCallback = onFinish.bind(undefined, this);
|
||||
}
|
||||
|
||||
write_(this, chunk, encoding, finishCallback, true);
|
||||
} else if (this.finished) {
|
||||
if (typeof callback === 'function') {
|
||||
queueEndCallback(this, callback);
|
||||
@@ -1165,14 +1197,17 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
|
||||
throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength);
|
||||
}
|
||||
|
||||
const finish = onFinish.bind(undefined, this);
|
||||
if (finishCallback === null) {
|
||||
// If we didn't early finish, send the last data and schedule 'finish' now:
|
||||
finishCallback = onFinish.bind(undefined, this);
|
||||
|
||||
if (this._hasBody && this.chunkedEncoding) {
|
||||
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
|
||||
} else if (!this._headerSent || this.writableLength || chunk) {
|
||||
this._send('', 'latin1', finish);
|
||||
} else {
|
||||
process.nextTick(finish);
|
||||
if (this._hasBody && this.chunkedEncoding) {
|
||||
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finishCallback);
|
||||
} else if (!this._headerSent || this.writableLength || chunk) {
|
||||
this._send('', 'latin1', finishCallback);
|
||||
} else {
|
||||
process.nextTick(finishCallback);
|
||||
}
|
||||
}
|
||||
|
||||
if (this[kSocket]) {
|
||||
|
||||
@@ -15,18 +15,11 @@ const res = new ServerResponse({
|
||||
httpVersionMinor: 1
|
||||
});
|
||||
|
||||
let firstChunk = true;
|
||||
|
||||
const ws = new Writable({
|
||||
write: common.mustCall((chunk, encoding, callback) => {
|
||||
if (firstChunk) {
|
||||
assert(chunk.toString().endsWith('hello world'));
|
||||
firstChunk = false;
|
||||
} else {
|
||||
assert.strictEqual(chunk.length, 0);
|
||||
}
|
||||
assert(chunk.toString().endsWith('hello world'));
|
||||
setImmediate(callback);
|
||||
}, 2)
|
||||
}, 1)
|
||||
});
|
||||
|
||||
res.assignSocket(ws);
|
||||
|
||||
Reference in new issue
Block a user