Skip to content

Commit f589f4d

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 a534058 commit f589f4d

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
@@ -1902,7 +1902,11 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
19021902

19031903
statistics_.data_received += nread;
19041904

1905-
if (UNLIKELY(stream_buf_offset_ > 0)) {
1905+
if (LIKELY(stream_buf_offset_ == 0)) {
1906+
// Shrink to the actual amount of used data.
1907+
buf.Resize(nread);
1908+
IncrementCurrentSessionMemory(nread);
1909+
} else {
19061910
// This is a very unlikely case, and should only happen if the ReadStart()
19071911
// call in OnStreamAfterWrite() immediately provides data. If that does
19081912
// happen, we concatenate the data we received with the already-stored
@@ -1911,20 +1915,18 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
19111915
AllocatedBuffer new_buf = env()->AllocateManaged(pending_len + nread);
19121916
memcpy(new_buf.data(), stream_buf_.base + stream_buf_offset_, pending_len);
19131917
memcpy(new_buf.data() + pending_len, buf.data(), nread);
1918+
1919+
// The data in stream_buf_ is already accounted for, add nread received
1920+
// bytes to session memory but remove the already processed
1921+
// stream_buf_offset_ bytes.
1922+
IncrementCurrentSessionMemory(nread - stream_buf_offset_);
1923+
19141924
buf = std::move(new_buf);
19151925
nread = buf.size();
19161926
stream_buf_offset_ = 0;
19171927
stream_buf_ab_.Reset();
1918-
1919-
// We have now fully processed the stream_buf_ input chunk (by moving the
1920-
// remaining part into buf, which will be accounted for below).
1921-
DecrementCurrentSessionMemory(stream_buf_.len);
19221928
}
19231929

1924-
// Shrink to the actual amount of used data.
1925-
buf.Resize(nread);
1926-
IncrementCurrentSessionMemory(nread);
1927-
19281930
// Remember the current buffer, so that OnDataChunkReceived knows the
19291931
// offset of a DATA frame's data into the socket read buffer.
19301932
stream_buf_ = uv_buf_init(buf.data(), nread);

0 commit comments

Comments
 (0)