From 48e62899b9cf2c882d7a2cfee478ee1b143d0f2f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 25 Apr 2018 15:16:45 +0200 Subject: [PATCH 1/2] src: use EnqueueMicrotask for tls writes Instead of using SetImmediate, use EnqueueMicrotask as it appears to be significantly more performant in certain cases and even in the optimal case yields roughly 10% higher throughput. --- src/tls_wrap.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 71adbdfc52ca36..05d6b431751c25 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -292,12 +292,10 @@ void TLSWrap::EncOut() { NODE_COUNT_NET_BYTES_SENT(write_size_); if (!res.async) { - HandleScope handle_scope(env()->isolate()); - // Simulate asynchronous finishing, TLS cannot handle this at the moment. - env()->SetImmediate([](Environment* env, void* data) { + env()->isolate()->EnqueueMicrotask([](void* data) { static_cast(data)->OnStreamAfterWrite(nullptr, 0); - }, this, object()); + }, this); } } From f89600d9e3051fefa5cbc9b395aab7f843e05618 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 25 Apr 2018 20:47:04 +0200 Subject: [PATCH 2/2] fixup: try something --- src/tls_wrap.cc | 22 ++++++++++++++++------ src/tls_wrap.h | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 05d6b431751c25..82f12bd6821085 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -291,12 +291,22 @@ void TLSWrap::EncOut() { NODE_COUNT_NET_BYTES_SENT(write_size_); - if (!res.async) { - // Simulate asynchronous finishing, TLS cannot handle this at the moment. - env()->isolate()->EnqueueMicrotask([](void* data) { - static_cast(data)->OnStreamAfterWrite(nullptr, 0); - }, this); - } + if (!res.async) + SyncAfterWrite(); +} + +// Simulate asynchronous finishing, TLS cannot handle this at the moment. +void TLSWrap::SyncAfterWrite() { + env()->isolate()->EnqueueMicrotask([](void* data) { + TLSWrap* wrap = static_cast(data); + wrap->MakeWeak(wrap); + wrap->OnStreamAfterWrite(nullptr, 0); + }, this); + ClearWeak(); + // This is a cheap way for us to ensure that microtasks will get to run + // at some point in the near future. + if (env()->immediate_info()->count() == 0) + env()->SetImmediate([](Environment* env, void* data) {}, this); } diff --git a/src/tls_wrap.h b/src/tls_wrap.h index 95e0c09cd8f971..8b8104c4c6f419 100644 --- a/src/tls_wrap.h +++ b/src/tls_wrap.h @@ -164,6 +164,8 @@ class TLSWrap : public AsyncWrap, bool eof_; private: + void SyncAfterWrite(); + static void GetWriteQueueSize( const v8::FunctionCallbackInfo& info); };