Skip to content

Commit

Permalink
lib: avoid recompilation of anonymous functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Fedor Indutny <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
mscdex authored and evanlucas committed Jan 4, 2017
1 parent 8a2a763 commit aed5e27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
7 changes: 4 additions & 3 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {

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

function onFinish(outmsg) {
outmsg.emit('finish');
}

OutgoingMessage.prototype.end = function end(data, encoding, callback) {
if (typeof data === 'function') {
Expand Down Expand Up @@ -592,9 +595,7 @@ OutgoingMessage.prototype.end = function end(data, encoding, callback) {
if (typeof callback === 'function')
this.once('finish', callback);

const finish = () => {
this.emit('finish');
};
var finish = onFinish.bind(undefined, this);

var ret;
if (this._hasBody && this.chunkedEncoding) {
Expand Down
35 changes: 17 additions & 18 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ function WritableState(options, stream) {
this.bufferProcessing = false;

// the callback that's passed to _write(chunk,cb)
this.onwrite = function(er) {
onwrite(stream, er);
};
this.onwrite = onwrite.bind(undefined, stream);

// the callback that the user supplies to write(chunk,encoding,cb)
this.writecb = null;
Expand Down Expand Up @@ -538,20 +536,21 @@ function endWritable(stream, state, cb) {
function CorkedRequest(state) {
this.next = null;
this.entry = null;
this.finish = onCorkedFinish.bind(undefined, this, state);
}

this.finish = (err) => {
var entry = this.entry;
this.entry = null;
while (entry) {
var cb = entry.callback;
state.pendingcb--;
cb(err);
entry = entry.next;
}
if (state.corkedRequestsFree) {
state.corkedRequestsFree.next = this;
} else {
state.corkedRequestsFree = this;
}
};
function onCorkedFinish(corkReq, state, err) {
var entry = corkReq.entry;
corkReq.entry = null;
while (entry) {
var cb = entry.callback;
state.pendingcb--;
cb(err);
entry = entry.next;
}
if (state.corkedRequestsFree) {
state.corkedRequestsFree.next = corkReq;
} else {
state.corkedRequestsFree = corkReq;
}
}

0 comments on commit aed5e27

Please sign in to comment.