Skip to content

Commit

Permalink
worker: make MessagePort uv_async_t inline field
Browse files Browse the repository at this point in the history
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: #26271
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
  • Loading branch information
addaleax committed Mar 1, 2019
1 parent 4dd7c4b commit 0fc40c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
15 changes: 5 additions & 10 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,18 +469,18 @@ MessagePort::MessagePort(Environment* env,
Local<Object> wrap)
: HandleWrap(env,
wrap,
reinterpret_cast<uv_handle_t*>(new uv_async_t()),
reinterpret_cast<uv_handle_t*>(&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<MessagePort*>(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<void*>(this);
async_.data = static_cast<void*>(this);

Local<Value> fn;
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
Expand All @@ -494,17 +494,13 @@ MessagePort::MessagePort(Environment* env,
Debug(this, "Created message port");
}

uv_async_t* MessagePort::async() {
return reinterpret_cast<uv_async_t*>(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<v8::Value> close_callback) {
Expand Down Expand Up @@ -639,7 +635,6 @@ void MessagePort::OnClose() {
data_->Disentangle();
}
data_.reset();
delete async();
}

std::unique_ptr<MessagePortData> MessagePort::Detach() {
Expand Down
2 changes: 1 addition & 1 deletion src/node_messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ class MessagePort : public HandleWrap {
void OnClose() override;
void OnMessage();
void TriggerAsync();
inline uv_async_t* async();

std::unique_ptr<MessagePortData> data_ = nullptr;
uv_async_t async_;

friend class MessagePortData;
};
Expand Down

0 comments on commit 0fc40c8

Please sign in to comment.