From 5bc5a4b67b2d22da2cd6e43844ef0874a3b4853a Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Sun, 20 Jun 2021 11:08:39 +0200 Subject: [PATCH 1/4] scale the socket receive buffer on the fly #39062 --- src/stream_wrap.cc | 12 ++++++++++-- src/stream_wrap.h | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 78d20f912b4c..8c89121b360c 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -117,7 +117,8 @@ LibuvStreamWrap::LibuvStreamWrap(Environment* env, reinterpret_cast(stream), provider), StreamBase(env), - stream_(stream) { + stream_(stream), + recvQSize(STREAM_WRAP_RECVQ_INITIAL) { StreamBase::AttachToObject(object); } @@ -209,7 +210,7 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) { HandleScope scope(env()->isolate()); Context::Scope context_scope(env()->context()); - *buf = EmitAlloc(suggested_size); + *buf = EmitAlloc(recvQSize); } template @@ -252,6 +253,13 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { CHECK_EQ(persistent().IsEmpty(), false); if (nread > 0) { + // Adjust the dynamic receive buffer + if (nread == recvQSize && recvQSize < STREAM_WRAP_RECVQ_MAX) { + recvQSize <<= 1; + } else if (nread < recvQSize / 4 && recvQSize > STREAM_WRAP_RECVQ_MIN) { + recvQSize >>= 1; + } + MaybeLocal pending_obj; if (type == UV_TCP) { diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 3016e8d89cd3..5163ac10c271 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -28,6 +28,10 @@ #include "handle_wrap.h" #include "v8.h" +#define STREAM_WRAP_RECVQ_INITIAL 16*1024 +#define STREAM_WRAP_RECVQ_MIN 16*1024 +#define STREAM_WRAP_RECVQ_MAX 256*1024 + namespace node { class Environment; @@ -111,6 +115,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase { static void AfterUvShutdown(uv_shutdown_t* req, int status); uv_stream_t* const stream_; + ssize_t recvQSize; #ifdef _WIN32 // We don't always have an FD that we could look up on the stream_ From 96d8078466f5fdeefd660faa5dccf00fe0aedd8a Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Mon, 21 Jun 2021 10:11:45 +0200 Subject: [PATCH 2/4] adopt the review changes --- src/stream_wrap.cc | 12 ++++++------ src/stream_wrap.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 8c89121b360c..9c5173b79b7a 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -118,7 +118,7 @@ LibuvStreamWrap::LibuvStreamWrap(Environment* env, provider), StreamBase(env), stream_(stream), - recvQSize(STREAM_WRAP_RECVQ_INITIAL) { + recvq_size(STREAM_WRAP_RECVQ_INITIAL) { StreamBase::AttachToObject(object); } @@ -210,7 +210,7 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) { HandleScope scope(env()->isolate()); Context::Scope context_scope(env()->context()); - *buf = EmitAlloc(recvQSize); + *buf = EmitAlloc(recvq_size); } template @@ -254,10 +254,10 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { if (nread > 0) { // Adjust the dynamic receive buffer - if (nread == recvQSize && recvQSize < STREAM_WRAP_RECVQ_MAX) { - recvQSize <<= 1; - } else if (nread < recvQSize / 4 && recvQSize > STREAM_WRAP_RECVQ_MIN) { - recvQSize >>= 1; + if (nread == recvq_size && recvq_size < STREAM_WRAP_RECVQ_MAX) { + recvq_size *= 2; + } else if (nread < recvq_size / 4 && recvq_size > STREAM_WRAP_RECVQ_MIN) { + recvq_size /= 2; } MaybeLocal pending_obj; diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 5163ac10c271..13f6ebf050ed 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -115,7 +115,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase { static void AfterUvShutdown(uv_shutdown_t* req, int status); uv_stream_t* const stream_; - ssize_t recvQSize; + ssize_t recvq_size; #ifdef _WIN32 // We don't always have an FD that we could look up on the stream_ From ae2bff74ac97c496e3016f36ec164599fa50f345 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Mon, 21 Jun 2021 10:30:18 +0200 Subject: [PATCH 3/4] use snake_case_ --- src/stream_wrap.cc | 12 ++++++------ src/stream_wrap.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 9c5173b79b7a..c01323c63db8 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -118,7 +118,7 @@ LibuvStreamWrap::LibuvStreamWrap(Environment* env, provider), StreamBase(env), stream_(stream), - recvq_size(STREAM_WRAP_RECVQ_INITIAL) { + recvq_size_(STREAM_WRAP_RECVQ_INITIAL) { StreamBase::AttachToObject(object); } @@ -210,7 +210,7 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) { HandleScope scope(env()->isolate()); Context::Scope context_scope(env()->context()); - *buf = EmitAlloc(recvq_size); + *buf = EmitAlloc(recvq_size_); } template @@ -254,10 +254,10 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { if (nread > 0) { // Adjust the dynamic receive buffer - if (nread == recvq_size && recvq_size < STREAM_WRAP_RECVQ_MAX) { - recvq_size *= 2; - } else if (nread < recvq_size / 4 && recvq_size > STREAM_WRAP_RECVQ_MIN) { - recvq_size /= 2; + if (nread == recvq_size_ && recvq_size_ < STREAM_WRAP_RECVQ_MAX) { + recvq_size_ *= 2; + } else if (nread < recvq_size_ / 4 && recvq_size_ > STREAM_WRAP_RECVQ_MIN) { + recvq_size_ /= 2; } MaybeLocal pending_obj; diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 13f6ebf050ed..8773e984f86f 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -115,7 +115,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase { static void AfterUvShutdown(uv_shutdown_t* req, int status); uv_stream_t* const stream_; - ssize_t recvq_size; + ssize_t recvq_size_; #ifdef _WIN32 // We don't always have an FD that we could look up on the stream_ From ea8b191f6e88e67f212c5c249031a3b1a3ae9d48 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Mon, 21 Jun 2021 16:22:48 +0200 Subject: [PATCH 4/4] Update src/stream_wrap.h Co-authored-by: Darshan Sen --- src/stream_wrap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 8773e984f86f..b951d0f48792 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -28,9 +28,9 @@ #include "handle_wrap.h" #include "v8.h" -#define STREAM_WRAP_RECVQ_INITIAL 16*1024 -#define STREAM_WRAP_RECVQ_MIN 16*1024 -#define STREAM_WRAP_RECVQ_MAX 256*1024 +constexpr ssize_t STREAM_WRAP_RECVQ_INITIAL = 16 * 1024; +constexpr ssize_t STREAM_WRAP_RECVQ_MIN = 16 * 1024; +constexpr ssize_t STREAM_WRAP_RECVQ_MAX = 256 * 1024; namespace node {