From 554ec2c11a8e507f8479f39df89d8ae738eb6f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20St=C3=B6bich?= <18708370+Flarna@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:10:22 +0200 Subject: [PATCH] src: add async context frame to AsyncResource Add member to hold the async context frame to AsyncResource to avoid the need for the async_resource_context_frames_ map in env. Semver major because it changes ABI. --- src/api/async_resource.cc | 25 ++++++++----------------- src/env-inl.h | 20 -------------------- src/env.h | 11 ----------- src/node.h | 1 + 4 files changed, 9 insertions(+), 48 deletions(-) diff --git a/src/api/async_resource.cc b/src/api/async_resource.cc index 2ea5d1269875eb..38d53a9aec0e58 100644 --- a/src/api/async_resource.cc +++ b/src/api/async_resource.cc @@ -17,28 +17,23 @@ AsyncResource::AsyncResource(Isolate* isolate, const char* name, async_id trigger_async_id) : env_(Environment::GetCurrent(isolate)), - resource_(isolate, resource) { + resource_(isolate, resource), + context_frame_(isolate, async_context_frame::current(isolate)) { CHECK_NOT_NULL(env_); - env_->SetAsyncResourceContextFrame( - reinterpret_cast(this), - {isolate, async_context_frame::current(isolate)}); async_context_ = EmitAsyncInit(isolate, resource, name, trigger_async_id); } AsyncResource::~AsyncResource() { CHECK_NOT_NULL(env_); EmitAsyncDestroy(env_, async_context_); - env_->RemoveAsyncResourceContextFrame(reinterpret_cast(this)); } MaybeLocal AsyncResource::MakeCallback(Local callback, int argc, Local* argv) { auto isolate = env_->isolate(); - auto context_frame = - env_->GetAsyncResourceContextFrame(reinterpret_cast(this)) - .Get(isolate); - async_context_frame::Scope async_context_frame_scope(isolate, context_frame); + async_context_frame::Scope async_context_frame_scope( + isolate, context_frame_.Get(isolate)); return node::MakeCallback( isolate, get_resource(), callback, argc, argv, async_context_); @@ -48,10 +43,8 @@ MaybeLocal AsyncResource::MakeCallback(const char* method, int argc, Local* argv) { auto isolate = env_->isolate(); - auto context_frame = - env_->GetAsyncResourceContextFrame(reinterpret_cast(this)) - .Get(isolate); - async_context_frame::Scope async_context_frame_scope(isolate, context_frame); + async_context_frame::Scope async_context_frame_scope( + isolate, context_frame_.Get(isolate)); return node::MakeCallback( isolate, get_resource(), method, argc, argv, async_context_); @@ -61,10 +54,8 @@ MaybeLocal AsyncResource::MakeCallback(Local symbol, int argc, Local* argv) { auto isolate = env_->isolate(); - auto context_frame = - env_->GetAsyncResourceContextFrame(reinterpret_cast(this)) - .Get(isolate); - async_context_frame::Scope async_context_frame_scope(isolate, context_frame); + async_context_frame::Scope async_context_frame_scope( + isolate, context_frame_.Get(isolate)); return node::MakeCallback( isolate, get_resource(), symbol, argc, argv, async_context_); diff --git a/src/env-inl.h b/src/env-inl.h index d266eca6fc3300..79496fab1a7528 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -908,26 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback( heap_limit); } -inline void Environment::SetAsyncResourceContextFrame( - std::uintptr_t async_resource_handle, - v8::Global&& context_frame) { - async_resource_context_frames_.emplace( - std::make_pair(async_resource_handle, std::move(context_frame))); -} - -inline const v8::Global& Environment::GetAsyncResourceContextFrame( - std::uintptr_t async_resource_handle) { - auto&& async_resource_context_frame = - async_resource_context_frames_.find(async_resource_handle); - CHECK_NE(async_resource_context_frame, async_resource_context_frames_.end()); - - return async_resource_context_frame->second; -} - -inline void Environment::RemoveAsyncResourceContextFrame( - std::uintptr_t async_resource_handle) { - async_resource_context_frames_.erase(async_resource_handle); -} } // namespace node // These two files depend on each other. Including base_object-inl.h after this diff --git a/src/env.h b/src/env.h index 11d956710b8969..7373c20ffb05de 100644 --- a/src/env.h +++ b/src/env.h @@ -1070,14 +1070,6 @@ class Environment final : public MemoryRetainer { v8::Global temporary_required_module_facade_original; - void SetAsyncResourceContextFrame(std::uintptr_t async_resource_handle, - v8::Global&&); - - const v8::Global& GetAsyncResourceContextFrame( - std::uintptr_t async_resource_handle); - - void RemoveAsyncResourceContextFrame(std::uintptr_t async_resource_handle); - private: inline void ThrowError(v8::Local (*fun)(v8::Local, v8::Local), @@ -1252,9 +1244,6 @@ class Environment final : public MemoryRetainer { // track of the BackingStore for a given pointer. std::unordered_map> released_allocated_buffers_; - - std::unordered_map> - async_resource_context_frames_; }; } // namespace node diff --git a/src/node.h b/src/node.h index 643f964115b0ae..06304fbdda9c32 100644 --- a/src/node.h +++ b/src/node.h @@ -1524,6 +1524,7 @@ class NODE_EXTERN AsyncResource { private: Environment* env_; v8::Global resource_; + v8::Global context_frame_; async_context async_context_; };