Skip to content

Commit 6fdc502

Browse files
committed
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: #26271 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 51f01aa commit 6fdc502

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

src/node_messaging.cc

+5-10
Original file line numberDiff line numberDiff line change
@@ -469,18 +469,18 @@ MessagePort::MessagePort(Environment* env,
469469
Local<Object> wrap)
470470
: HandleWrap(env,
471471
wrap,
472-
reinterpret_cast<uv_handle_t*>(new uv_async_t()),
472+
reinterpret_cast<uv_handle_t*>(&async_),
473473
AsyncWrap::PROVIDER_MESSAGEPORT),
474474
data_(new MessagePortData(this)) {
475475
auto onmessage = [](uv_async_t* handle) {
476476
// Called when data has been put into the queue.
477-
MessagePort* channel = static_cast<MessagePort*>(handle->data);
477+
MessagePort* channel = ContainerOf(&MessagePort::async_, handle);
478478
channel->OnMessage();
479479
};
480480
CHECK_EQ(uv_async_init(env->event_loop(),
481-
async(),
481+
&async_,
482482
onmessage), 0);
483-
async()->data = static_cast<void*>(this);
483+
async_.data = static_cast<void*>(this);
484484

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

497-
uv_async_t* MessagePort::async() {
498-
return reinterpret_cast<uv_async_t*>(GetHandle());
499-
}
500-
501497
bool MessagePort::IsDetached() const {
502498
return data_ == nullptr || IsHandleClosing();
503499
}
504500

505501
void MessagePort::TriggerAsync() {
506502
if (IsHandleClosing()) return;
507-
CHECK_EQ(uv_async_send(async()), 0);
503+
CHECK_EQ(uv_async_send(&async_), 0);
508504
}
509505

510506
void MessagePort::Close(v8::Local<v8::Value> close_callback) {
@@ -639,7 +635,6 @@ void MessagePort::OnClose() {
639635
data_->Disentangle();
640636
}
641637
data_.reset();
642-
delete async();
643638
}
644639

645640
std::unique_ptr<MessagePortData> MessagePort::Detach() {

src/node_messaging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ class MessagePort : public HandleWrap {
198198
void OnClose() override;
199199
void OnMessage();
200200
void TriggerAsync();
201-
inline uv_async_t* async();
202201

203202
std::unique_ptr<MessagePortData> data_ = nullptr;
203+
uv_async_t async_;
204204

205205
friend class MessagePortData;
206206
};

0 commit comments

Comments
 (0)