Skip to content

Commit c4a45a9

Browse files
joyeecheungRafaelGSS
authored andcommitted
vm: avoid unnecessary property getter interceptor calls
Access to the global object from within a vm context is intercepted so it's slow, therefore we should try to avoid unnecessary access to it during the initialization of vm contexts. - Remove the Atomics.wake deletion as V8 now does not install it anymore. - Move the Intl.v8BreakIterator deletion into the snapshot. - Do not query the Object prototype if --disable-proto is not set. This should speed up the creation of vm contexts by about ~12%. PR-URL: #44252 Refs: #44014 Refs: #37476 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 736a04a commit c4a45a9

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

src/api/environment.cc

+33-46
Original file line numberDiff line numberDiff line change
@@ -549,50 +549,8 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
549549
Isolate* isolate = context->GetIsolate();
550550
HandleScope handle_scope(isolate);
551551

552-
// Delete `Intl.v8BreakIterator`
553-
// https://github.com/nodejs/node/issues/14909
554-
{
555-
Local<String> intl_string =
556-
FIXED_ONE_BYTE_STRING(isolate, "Intl");
557-
Local<String> break_iter_string =
558-
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
559-
560-
Local<Value> intl_v;
561-
if (!context->Global()
562-
->Get(context, intl_string)
563-
.ToLocal(&intl_v)) {
564-
return Nothing<bool>();
565-
}
566-
567-
if (intl_v->IsObject() &&
568-
intl_v.As<Object>()
569-
->Delete(context, break_iter_string)
570-
.IsNothing()) {
571-
return Nothing<bool>();
572-
}
573-
}
574-
575-
// Delete `Atomics.wake`
576-
// https://github.com/nodejs/node/issues/21219
577-
{
578-
Local<String> atomics_string =
579-
FIXED_ONE_BYTE_STRING(isolate, "Atomics");
580-
Local<String> wake_string =
581-
FIXED_ONE_BYTE_STRING(isolate, "wake");
582-
583-
Local<Value> atomics_v;
584-
if (!context->Global()
585-
->Get(context, atomics_string)
586-
.ToLocal(&atomics_v)) {
587-
return Nothing<bool>();
588-
}
589-
590-
if (atomics_v->IsObject() &&
591-
atomics_v.As<Object>()
592-
->Delete(context, wake_string)
593-
.IsNothing()) {
594-
return Nothing<bool>();
595-
}
552+
if (per_process::cli_options->disable_proto == "") {
553+
return Just(true);
596554
}
597555

598556
// Remove __proto__
@@ -654,7 +612,32 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
654612
return Just(true);
655613
}
656614

657-
Maybe<bool> InitializeContextForSnapshot(Local<Context> context) {
615+
Maybe<bool> InitializeBaseContextForSnapshot(Local<Context> context) {
616+
Isolate* isolate = context->GetIsolate();
617+
HandleScope handle_scope(isolate);
618+
619+
// Delete `Intl.v8BreakIterator`
620+
// https://github.com/nodejs/node/issues/14909
621+
{
622+
Context::Scope context_scope(context);
623+
Local<String> intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl");
624+
Local<String> break_iter_string =
625+
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
626+
627+
Local<Value> intl_v;
628+
if (!context->Global()->Get(context, intl_string).ToLocal(&intl_v)) {
629+
return Nothing<bool>();
630+
}
631+
632+
if (intl_v->IsObject() &&
633+
intl_v.As<Object>()->Delete(context, break_iter_string).IsNothing()) {
634+
return Nothing<bool>();
635+
}
636+
}
637+
return Just(true);
638+
}
639+
640+
Maybe<bool> InitializeMainContextForSnapshot(Local<Context> context) {
658641
Isolate* isolate = context->GetIsolate();
659642
HandleScope handle_scope(isolate);
660643

@@ -664,6 +647,9 @@ Maybe<bool> InitializeContextForSnapshot(Local<Context> context) {
664647
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
665648
True(isolate));
666649

650+
if (InitializeBaseContextForSnapshot(context).IsNothing()) {
651+
return Nothing<bool>();
652+
}
667653
return InitializePrimordials(context);
668654
}
669655

@@ -710,8 +696,9 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
710696
return Just(true);
711697
}
712698

699+
// This initializes the main context (i.e. vm contexts are not included).
713700
Maybe<bool> InitializeContext(Local<Context> context) {
714-
if (InitializeContextForSnapshot(context).IsNothing()) {
701+
if (InitializeMainContextForSnapshot(context).IsNothing()) {
715702
return Nothing<bool>();
716703
}
717704

src/node_contextify.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,16 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
206206
{}, // global object
207207
{}, // deserialization callback
208208
queue);
209-
if (ctx.IsEmpty()) return MaybeLocal<Context>();
209+
if (ctx.IsEmpty() || InitializeBaseContextForSnapshot(ctx).IsNothing()) {
210+
return MaybeLocal<Context>();
211+
}
210212
} else if (!Context::FromSnapshot(isolate,
211-
SnapshotData::kNodeVMContextIndex,
212-
{}, // deserialization callback
213-
nullptr, // extensions
214-
{}, // global object
215-
queue)
216-
.ToLocal(&ctx)) {
213+
SnapshotData::kNodeVMContextIndex,
214+
{}, // deserialization callback
215+
nullptr, // extensions
216+
{}, // global object
217+
queue)
218+
.ToLocal(&ctx)) {
217219
return MaybeLocal<Context>();
218220
}
219221
return scope.Escape(ctx);

src/node_internals.h

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext);
9292
std::string GetProcessTitle(const char* default_title);
9393
std::string GetHumanReadableProcessName();
9494

95+
v8::Maybe<bool> InitializeBaseContextForSnapshot(
96+
v8::Local<v8::Context> context);
9597
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
9698
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
9799

0 commit comments

Comments
 (0)