Skip to content

Commit 228127f

Browse files
joyeecheungtargos
authored andcommitted
src: enable context snapshot after running per-context scripts
At build time, snapshot the context after running per-context scripts in the main instance, and in the final build, deserialize the context in the main instance. This provides a ~5% in the misc/startup benchmark when the instance is launched within a process that runs test/fixtures/semicolon.js. PR-URL: #27321 Refs: #17058 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 45d6106 commit 228127f

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/node_main_instance.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
171171
isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
172172
}
173173

174-
Local<Context> context = NewContext(isolate_);
174+
Local<Context> context;
175175
if (deserialize_mode_) {
176+
context =
177+
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
176178
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
179+
} else {
180+
context = NewContext(isolate_);
177181
}
178182

179183
CHECK(!context.IsEmpty());

src/node_main_instance.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class NodeMainInstance {
7575
static const IndexArray* GetIsolateDataIndexes();
7676
static v8::StartupData* GetEmbeddedSnapshotBlob();
7777

78+
static const size_t kNodeContextIndex = 0;
79+
7880
private:
7981
NodeMainInstance(const NodeMainInstance&) = delete;
8082
NodeMainInstance& operator=(const NodeMainInstance&) = delete;

tools/snapshot/snapshot_builder.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ std::string SnapshotBuilder::Generate(
104104
HandleScope scope(isolate);
105105
creator.SetDefaultContext(Context::New(isolate));
106106
isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
107+
108+
size_t index = creator.AddContext(NewContext(isolate));
109+
CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);
107110
}
108111

109112
// Must be out of HandleScope

0 commit comments

Comments
 (0)