Skip to content

Commit a7fdfc0

Browse files
mscdexevanlucas
authored andcommitted
lib: avoid recompilation of anonymous functions
Since at least V8 5.4, using function.bind() is now fast enough to use to avoid recompiling/reoptimizing the same anonymous functions. These changes especially impact http servers. PR-URL: #6533 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent df41a74 commit a7fdfc0

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

lib/_http_outgoing.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
545545

546546
const crlf_buf = Buffer.from('\r\n');
547547

548+
function onFinish(outmsg) {
549+
outmsg.emit('finish');
550+
}
548551

549552
OutgoingMessage.prototype.end = function end(data, encoding, callback) {
550553
if (typeof data === 'function') {
@@ -592,9 +595,7 @@ OutgoingMessage.prototype.end = function end(data, encoding, callback) {
592595
if (typeof callback === 'function')
593596
this.once('finish', callback);
594597

595-
const finish = () => {
596-
this.emit('finish');
597-
};
598+
var finish = onFinish.bind(undefined, this);
598599

599600
var ret;
600601
if (this._hasBody && this.chunkedEncoding) {

lib/_stream_writable.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ function WritableState(options, stream) {
8686
this.bufferProcessing = false;
8787

8888
// the callback that's passed to _write(chunk,cb)
89-
this.onwrite = function(er) {
90-
onwrite(stream, er);
91-
};
89+
this.onwrite = onwrite.bind(undefined, stream);
9290

9391
// the callback that the user supplies to write(chunk,encoding,cb)
9492
this.writecb = null;
@@ -538,20 +536,21 @@ function endWritable(stream, state, cb) {
538536
function CorkedRequest(state) {
539537
this.next = null;
540538
this.entry = null;
539+
this.finish = onCorkedFinish.bind(undefined, this, state);
540+
}
541541

542-
this.finish = (err) => {
543-
var entry = this.entry;
544-
this.entry = null;
545-
while (entry) {
546-
var cb = entry.callback;
547-
state.pendingcb--;
548-
cb(err);
549-
entry = entry.next;
550-
}
551-
if (state.corkedRequestsFree) {
552-
state.corkedRequestsFree.next = this;
553-
} else {
554-
state.corkedRequestsFree = this;
555-
}
556-
};
542+
function onCorkedFinish(corkReq, state, err) {
543+
var entry = corkReq.entry;
544+
corkReq.entry = null;
545+
while (entry) {
546+
var cb = entry.callback;
547+
state.pendingcb--;
548+
cb(err);
549+
entry = entry.next;
550+
}
551+
if (state.corkedRequestsFree) {
552+
state.corkedRequestsFree.next = corkReq;
553+
} else {
554+
state.corkedRequestsFree = corkReq;
555+
}
557556
}

0 commit comments

Comments
 (0)