From 3cdd529f2aa0b90ae2fbf76294c5171e7105bf06 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 2 Dec 2016 12:38:57 -0600 Subject: [PATCH] http2: 10% perf improvement. PR-URL: https://github.com/nodejs/http2/pull/20 Reviewed-By: James M Snell --- lib/internal/http2.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/internal/http2.js b/lib/internal/http2.js index 53657a2938..0d973ad1c0 100644 --- a/lib/internal/http2.js +++ b/lib/internal/http2.js @@ -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)); } } @@ -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(); @@ -713,6 +717,7 @@ class Http2Outgoing extends Writable { this[kFinished] = false; this[kHeadersSent] = false; this.on('pipe', onHttp2OutgoingPipe); + this.bufferedCallback = null; } get stream() { @@ -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(); @@ -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) {