From 8d8d2b44ba700947b9ffdfc7f2b90c53bd42730a Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Wed, 22 Jul 2020 19:18:28 +0300 Subject: [PATCH] http2: avoid unnecessary buffer resize Refs: https://github.com/nodejs/node/pull/34315 Refs: https://github.com/nodejs/node/pull/30351 PR-URL: https://github.com/nodejs/node/pull/34480 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Anna Henningsen Reviewed-By: David Carlier --- src/node_http2.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node_http2.cc b/src/node_http2.cc index ee084a11ea18e5..ba39610d64cbac 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1841,7 +1841,10 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { statistics_.data_received += nread; - if (UNLIKELY(stream_buf_offset_ > 0)) { + if (LIKELY(stream_buf_offset_ == 0)) { + // Shrink to the actual amount of used data. + buf.Resize(nread); + } else { // This is a very unlikely case, and should only happen if the ReadStart() // call in OnStreamAfterWrite() immediately provides data. If that does // happen, we concatenate the data we received with the already-stored @@ -1861,8 +1864,6 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { DecrementCurrentSessionMemory(stream_buf_.len); } - // Shrink to the actual amount of used data. - buf.Resize(nread); IncrementCurrentSessionMemory(nread); // Remember the current buffer, so that OnDataChunkReceived knows the