GeezerVison is showing github.com — src: cut small costs in node::MakeCallback · gabylb/node@3cbedbc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/api/callback.cc +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #include "node.h" | 5 | 5 | #include "v8.h" | 6 | 6 |
| | 7 | +#include <optional> | | 8 | + | 7 | 9 | namespace node { | 8 | 10 |
| 9 | 11 | using v8::Context; | @@ -348,7 +350,14 @@ MaybeLocal<Value> InternalMakeCallback(Isolate* isolate, | 348 | 350 | } | 349 | 351 | Environment* env = Environment::GetCurrent(context); | 350 | 352 | CHECK_NOT_NULL(env); | 351 | | - Context::Scope context_scope(env->context()); | | 353 | + // Entering the context changes nothing when it is already the entered and | | 354 | + // the current one, which is the common case for addons. | | 355 | + Local<Context> env_context = env->context(); | | 356 | + std::optional<Context::Scope> context_scope; | | 357 | + if (isolate->GetEnteredOrMicrotaskContext() != env_context || | | 358 | + isolate->GetCurrentContext() != env_context) { | | 359 | + context_scope.emplace(env_context); | | 360 | + } | 352 | 361 | MaybeLocal<Value> ret = InternalMakeCallback( | 353 | 362 | env, recv, recv, callback, argc, argv, asyncContext, context_frame); | 354 | 363 | if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) { | |
Collapse file Copy file name to clipboardExpand all lines: src/async_context_frame.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,8 @@ void set(Isolate* isolate, Local<Value> value) { | 49 | 49 | // but sometimes (such as enterWith) a direct exchange is needed. | 50 | 50 | Local<Value> exchange(Environment* env, Local<Value> value) { | 51 | 51 | auto prior = current(env->isolate()); | 52 | | - set(env, value); | | 52 | + // Usually the frame does not change, and there is nothing to set. | | 53 | + if (prior != value) set(env, value); | 53 | 54 | return prior; | 54 | 55 | } | 55 | 56 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +12-4Lines changed: 12 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,7 +133,7 @@ void Environment::ResetPromiseHooks(Local<Function> init, | 133 | 133 | void AsyncHooks::push_async_context( | 134 | 134 | double async_id, | 135 | 135 | double trigger_async_id, | 136 | | - std::variant<Local<Object>*, Global<Object>*> resource) { | | 136 | + const std::variant<Local<Object>*, Global<Object>*>& resource) { | 137 | 137 | std::visit([](auto* ptr) { CHECK_IMPLIES(ptr != nullptr, !ptr->IsEmpty()); }, | 138 | 138 | resource); | 139 | 139 |
| @@ -161,9 +161,13 @@ void AsyncHooks::push_async_context( | 161 | 161 | // False positive: https://github.com/cpplint/cpplint/issues/410 | 162 | 162 | // NOLINTNEXTLINE(whitespace/newline) | 163 | 163 | if (std::visit([](auto* ptr) { return ptr != nullptr; }, resource)) { | 164 | | - native_execution_async_resources_.resize(offset + 1); | 165 | 164 | // Caveat: This is a v8::Local<>* assignment, we do not keep a v8::Global<>! | 166 | | - native_execution_async_resources_[offset] = resource; | | 165 | + if (native_execution_async_resources_.size() == offset) { | | 166 | + native_execution_async_resources_.push_back(resource); | | 167 | + } else { | | 168 | + native_execution_async_resources_.resize(offset + 1); | | 169 | + native_execution_async_resources_[offset] = resource; | | 170 | + } | 167 | 171 | } | 168 | 172 | } | 169 | 173 |
| @@ -196,7 +200,11 @@ bool AsyncHooks::pop_async_context(double async_id) { | 196 | 200 | native_execution_async_resources_[i]); | 197 | 201 | } | 198 | 202 | #endif | 199 | | - native_execution_async_resources_.resize(offset); | | 203 | + if (native_execution_async_resources_.size() == offset + 1) { | | 204 | + native_execution_async_resources_.pop_back(); | | 205 | + } else { | | 206 | + native_execution_async_resources_.resize(offset); | | 207 | + } | 200 | 208 | native_execution_async_resources_.shrink_to_fit(); | 201 | 209 | } | 202 | 210 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -433,7 +433,7 @@ class AsyncHooks : public MemoryRetainer { | 433 | 433 | void push_async_context( | 434 | 434 | double async_id, | 435 | 435 | double trigger_async_id, | 436 | | - std::variant<v8::Local<v8::Object>*, v8::Global<v8::Object>*> | | 436 | + const std::variant<v8::Local<v8::Object>*, v8::Global<v8::Object>*>& | 437 | 437 | execution_async_resource); | 438 | 438 | bool pop_async_context(double async_id); | 439 | 439 | void clear_async_id_stack(); // Used in fatal exceptions. | |
|