Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
http2: 10% perf improvement.
Browse files Browse the repository at this point in the history
PR-URL: #20
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
mcollina authored and jasnell committed Dec 3, 2016
1 parent ba11612 commit 3cdd529
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/internal/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,7 @@ class Http2Stream extends Duplex {
throw util._errnoException(err, 'write', req.error);
this._bytesDispatched += req.bytes;
} else {
this.once('handle', () => {
this._write(data, encoding, cb);
});
this.once('handle', onHandleWrite(data, encoding, cb));
}
}

Expand Down Expand Up @@ -483,6 +481,12 @@ class Http2Stream extends Duplex {
}
}

function onHandleWrite(data, encoding, cb) {
return function onWriteFinished() {
this._write(data, encoding, cb);
};
}

class Http2Session extends EventEmitter {
constructor(type, options, socket) {
super();
Expand Down Expand Up @@ -713,6 +717,7 @@ class Http2Outgoing extends Writable {
this[kFinished] = false;
this[kHeadersSent] = false;
this.on('pipe', onHttp2OutgoingPipe);
this.bufferedCallback = null;
}

get stream() {
Expand Down Expand Up @@ -791,11 +796,8 @@ class Http2Outgoing extends Writable {
chunk = Buffer.from(chunk, encoding);
if (this.stream) {
this[kBeginSend]();
this.stream.write(chunk, encoding, () => {
this[kResume]();
if (typeof callback === 'function')
callback();
});
this.bufferedCallback = callback;
this.stream.write(chunk, encoding, outWriteResume);
} else {
this[kFinished] = true;
callback();
Expand Down Expand Up @@ -826,6 +828,14 @@ class Http2Outgoing extends Writable {
}
}

function outWriteResume() {
this[kResume]();
const callback = this.bufferedCallback;
this.bufferedCallback = null;
if (typeof callback === 'function')
callback();
}

// Represents an HTTP/2 server response message
class Http2ServerResponse extends Http2Outgoing {
constructor(stream, options) {
Expand Down

0 comments on commit 3cdd529

Please sign in to comment.