src: cut small costs in node::MakeCallback · nodejs/node@5a566ac · GitHub
Skip to content

Commit 5a566ac

Browse files
authored
src: cut small costs in node::MakeCallback
Enter the Environment's context only when it is not already the entered and current one, set the async context frame only when it changes, pass the resource variant to push_async_context() by const reference, and grow and shrink the native resource stack with push_back() and pop_back() instead of resize(). Refs: nodejs/performance#24 Signed-off-by: Nigro Simone <nigro.simone@gmail.com> PR-URL: #66395 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 6698675 commit 5a566ac

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

‎src/api/callback.cc‎

Lines changed: 10 additions & 1 deletion

‎src/async_context_frame.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void set(Isolate* isolate, Local<Value> value) {
4949
// but sometimes (such as enterWith) a direct exchange is needed.
5050
Local<Value> exchange(Environment* env, Local<Value> value) {
5151
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);
5354
return prior;
5455
}
5556

‎src/env.cc‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void Environment::ResetPromiseHooks(Local<Function> init,
133133
void AsyncHooks::push_async_context(
134134
double async_id,
135135
double trigger_async_id,
136-
std::variant<Local<Object>*, Global<Object>*> resource) {
136+
const std::variant<Local<Object>*, Global<Object>*>& resource) {
137137
std::visit([](auto* ptr) { CHECK_IMPLIES(ptr != nullptr, !ptr->IsEmpty()); },
138138
resource);
139139

@@ -161,9 +161,13 @@ void AsyncHooks::push_async_context(
161161
// False positive: https://github.com/cpplint/cpplint/issues/410
162162
// NOLINTNEXTLINE(whitespace/newline)
163163
if (std::visit([](auto* ptr) { return ptr != nullptr; }, resource)) {
164-
native_execution_async_resources_.resize(offset + 1);
165164
// 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+
}
167171
}
168172
}
169173

@@ -196,7 +200,11 @@ bool AsyncHooks::pop_async_context(double async_id) {
196200
native_execution_async_resources_[i]);
197201
}
198202
#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+
}
200208
native_execution_async_resources_.shrink_to_fit();
201209
}
202210

‎src/env.h‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)