diff options
author | Denys Otrishko <shishugi@gmail.com> | 2019-12-01 18:20:01 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-12-06 01:17:02 +0100 |
commit | 51ccf1b5e90540a11c33866da15d4a7f52d7fddb (patch) | |
tree | fdf5dcd7cbcbcbb87880a456fdb8563d758ff291 | |
parent | 53fb7be5eaa2392086aa428e4b8400e9a3a95a81 (diff) | |
download | android-node-v8-51ccf1b5e90540a11c33866da15d4a7f52d7fddb.tar.gz android-node-v8-51ccf1b5e90540a11c33866da15d4a7f52d7fddb.tar.bz2 android-node-v8-51ccf1b5e90540a11c33866da15d4a7f52d7fddb.zip |
http2: streamline OnStreamRead streamline memory accounting
* avoid consecutive decrement/increment session memory calls.
* only Resize the buffer when it is needed.
* flip `stream_buf_offset_` condition to the LIKELY case.
PR-URL: https://github.com/nodejs/node/pull/30351
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | src/node_http2.cc | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/node_http2.cc b/src/node_http2.cc index 2bcbcbe078..33960af184 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1862,7 +1862,11 @@ 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); + IncrementCurrentSessionMemory(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 @@ -1871,20 +1875,18 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { AllocatedBuffer new_buf = env()->AllocateManaged(pending_len + nread); memcpy(new_buf.data(), stream_buf_.base + stream_buf_offset_, pending_len); memcpy(new_buf.data() + pending_len, buf.data(), nread); + + // The data in stream_buf_ is already accounted for, add nread received + // bytes to session memory but remove the already processed + // stream_buf_offset_ bytes. + IncrementCurrentSessionMemory(nread - stream_buf_offset_); + buf = std::move(new_buf); nread = buf.size(); stream_buf_offset_ = 0; stream_buf_ab_.Reset(); - - // We have now fully processed the stream_buf_ input chunk (by moving the - // remaining part into buf, which will be accounted for below). - 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 // offset of a DATA frame's data into the socket read buffer. stream_buf_ = uv_buf_init(buf.data(), nread); |