From 6fdc502a3245aa08b32d028d68ab584663ffd402 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 23 Feb 2019 01:24:09 +0100 Subject: [PATCH] worker: make MessagePort `uv_async_t` inline field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s not obvious why this was a heap allocation in the first place, but it’s unneccessary. Most other `HandleWrap`s also store the libuv handle directly. PR-URL: https://github.com/nodejs/node/pull/26271 Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Gireesh Punathil --- src/node_messaging.cc | 15 +++++---------- src/node_messaging.h | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index c9be44f4102816..d727f27063376d 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -469,18 +469,18 @@ MessagePort::MessagePort(Environment* env, Local wrap) : HandleWrap(env, wrap, - reinterpret_cast(new uv_async_t()), + reinterpret_cast(&async_), AsyncWrap::PROVIDER_MESSAGEPORT), data_(new MessagePortData(this)) { auto onmessage = [](uv_async_t* handle) { // Called when data has been put into the queue. - MessagePort* channel = static_cast(handle->data); + MessagePort* channel = ContainerOf(&MessagePort::async_, handle); channel->OnMessage(); }; CHECK_EQ(uv_async_init(env->event_loop(), - async(), + &async_, onmessage), 0); - async()->data = static_cast(this); + async_.data = static_cast(this); Local fn; if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn)) @@ -494,17 +494,13 @@ MessagePort::MessagePort(Environment* env, Debug(this, "Created message port"); } -uv_async_t* MessagePort::async() { - return reinterpret_cast(GetHandle()); -} - bool MessagePort::IsDetached() const { return data_ == nullptr || IsHandleClosing(); } void MessagePort::TriggerAsync() { if (IsHandleClosing()) return; - CHECK_EQ(uv_async_send(async()), 0); + CHECK_EQ(uv_async_send(&async_), 0); } void MessagePort::Close(v8::Local close_callback) { @@ -639,7 +635,6 @@ void MessagePort::OnClose() { data_->Disentangle(); } data_.reset(); - delete async(); } std::unique_ptr MessagePort::Detach() { diff --git a/src/node_messaging.h b/src/node_messaging.h index 00fd4d7f9f00f4..282ad93468d95f 100644 --- a/src/node_messaging.h +++ b/src/node_messaging.h @@ -198,9 +198,9 @@ class MessagePort : public HandleWrap { void OnClose() override; void OnMessage(); void TriggerAsync(); - inline uv_async_t* async(); std::unique_ptr data_ = nullptr; + uv_async_t async_; friend class MessagePortData; };