worker: make MessagePort uv_async_t inline field

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 <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>
This commit is contained in:
Anna Henningsen 2019-02-23 01:24:09 +01:00
parent 4dd7c4b2e4
commit 0fc40c8049
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 6 additions and 11 deletions

View File

@ -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))
@ -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) {
@ -639,7 +635,6 @@ void MessagePort::OnClose() {
data_->Disentangle();
}
data_.reset();
delete async();
}
std::unique_ptr<MessagePortData> MessagePort::Detach() {

View File

@ -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;
};