From 098b193eab0db9b22ba97cfdc54fa6c629ab1583 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 3227ae66a2dead..a7cdb77b8c07a8 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1754,7 +1754,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 @@ -1775,8 +1778,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