diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-19 14:25:03 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-02-21 15:39:18 +0100 |
commit | 83e1b9744317496b2a3b257bbf87e241496dd5ab (patch) | |
tree | 1751aa29fe86b58450d0cd3e9d0c260654d9f321 /src/node_http2.cc | |
parent | e51da1fcadc822fb514cd6b32e932d7c0c3239a7 (diff) | |
download | android-node-v8-83e1b9744317496b2a3b257bbf87e241496dd5ab.tar.gz android-node-v8-83e1b9744317496b2a3b257bbf87e241496dd5ab.tar.bz2 android-node-v8-83e1b9744317496b2a3b257bbf87e241496dd5ab.zip |
http2: shrink memory to match read data
Perform a shrinking `Realloc()` so that less data is
used for HTTP2 reads.
PR-URL: https://github.com/nodejs/node/pull/26201
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/node_http2.cc')
-rw-r--r-- | src/node_http2.cc | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/src/node_http2.cc b/src/node_http2.cc index 19c3e77fa7..e5451d1fc7 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1774,17 +1774,8 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { Http2Scope h2scope(this); CHECK_NOT_NULL(stream_); Debug(this, "receiving %d bytes", nread); - IncrementCurrentSessionMemory(buf.len); CHECK(stream_buf_ab_.IsEmpty()); - OnScopeLeave on_scope_leave([&]() { - // Once finished handling this write, reset the stream buffer. - // The memory has either been free()d or was handed over to V8. - DecrementCurrentSessionMemory(buf.len); - stream_buf_ab_ = Local<ArrayBuffer>(); - stream_buf_ = uv_buf_init(nullptr, 0); - }); - // Only pass data on if nread > 0 if (nread <= 0) { free(buf.base); @@ -1794,19 +1785,25 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { return; } + // Shrink to the actual amount of used data. + char* base = Realloc(buf.base, nread); + + IncrementCurrentSessionMemory(nread); + OnScopeLeave on_scope_leave([&]() { + // Once finished handling this write, reset the stream buffer. + // The memory has either been free()d or was handed over to V8. + DecrementCurrentSessionMemory(nread); + stream_buf_ab_ = Local<ArrayBuffer>(); + stream_buf_ = uv_buf_init(nullptr, 0); + }); + // Make sure that there was no read previously active. CHECK_NULL(stream_buf_.base); CHECK_EQ(stream_buf_.len, 0); // 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.base, nread); - - // Verify that currently: There is memory allocated into which - // the data has been read, and that memory buffer is at least as large - // as the amount of data we have read, but we have not yet made an - // ArrayBuffer out of it. - CHECK_LE(static_cast<size_t>(nread), stream_buf_.len); + stream_buf_ = uv_buf_init(base, nread); Isolate* isolate = env()->isolate(); @@ -1814,9 +1811,9 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { // as slices of this array buffer to avoid having to copy memory. stream_buf_ab_ = ArrayBuffer::New(isolate, - buf.base, - nread, - ArrayBufferCreationMode::kInternalized); + base, + nread, + ArrayBufferCreationMode::kInternalized); statistics_.data_received += nread; ssize_t ret = Write(&stream_buf_, 1); |