Skip to content

Commit 71bb026

Browse files
lundibunditargos
authored andcommitted
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: #30351 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3840abe commit 71bb026

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/node_http2.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,11 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
18621862

18631863
statistics_.data_received += nread;
18641864

1865-
if (UNLIKELY(stream_buf_offset_ > 0)) {
1865+
if (LIKELY(stream_buf_offset_ == 0)) {
1866+
// Shrink to the actual amount of used data.
1867+
buf.Resize(nread);
1868+
IncrementCurrentSessionMemory(nread);
1869+
} else {
18661870
// This is a very unlikely case, and should only happen if the ReadStart()
18671871
// call in OnStreamAfterWrite() immediately provides data. If that does
18681872
// 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_) {
18711875
AllocatedBuffer new_buf = env()->AllocateManaged(pending_len + nread);
18721876
memcpy(new_buf.data(), stream_buf_.base + stream_buf_offset_, pending_len);
18731877
memcpy(new_buf.data() + pending_len, buf.data(), nread);
1878+
1879+
// The data in stream_buf_ is already accounted for, add nread received
1880+
// bytes to session memory but remove the already processed
1881+
// stream_buf_offset_ bytes.
1882+
IncrementCurrentSessionMemory(nread - stream_buf_offset_);
1883+
18741884
buf = std::move(new_buf);
18751885
nread = buf.size();
18761886
stream_buf_offset_ = 0;
18771887
stream_buf_ab_.Reset();
1878-
1879-
// We have now fully processed the stream_buf_ input chunk (by moving the
1880-
// remaining part into buf, which will be accounted for below).
1881-
DecrementCurrentSessionMemory(stream_buf_.len);
18821888
}
18831889

1884-
// Shrink to the actual amount of used data.
1885-
buf.Resize(nread);
1886-
IncrementCurrentSessionMemory(nread);
1887-
18881890
// Remember the current buffer, so that OnDataChunkReceived knows the
18891891
// offset of a DATA frame's data into the socket read buffer.
18901892
stream_buf_ = uv_buf_init(buf.data(), nread);

0 commit comments

Comments
 (0)