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: https://github.com/nodejs/node/pull/27321
Refs: https://github.com/nodejs/node/issues/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>
This commit is contained in:
Joyee Cheung 2019-04-20 17:18:34 +08:00
parent bf5dadeef0
commit eb23b176d8
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
3 changed files with 10 additions and 1 deletions

View File

@ -171,9 +171,13 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
}
Local<Context> context = NewContext(isolate_);
Local<Context> context;
if (deserialize_mode_) {
context =
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
} else {
context = NewContext(isolate_);
}
CHECK(!context.IsEmpty());

View File

@ -75,6 +75,8 @@ class NodeMainInstance {
static const IndexArray* GetIsolateDataIndexes();
static v8::StartupData* GetEmbeddedSnapshotBlob();
static const size_t kNodeContextIndex = 0;
private:
NodeMainInstance(const NodeMainInstance&) = delete;
NodeMainInstance& operator=(const NodeMainInstance&) = delete;

View File

@ -104,6 +104,9 @@ std::string SnapshotBuilder::Generate(
HandleScope scope(isolate);
creator.SetDefaultContext(Context::New(isolate));
isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
size_t index = creator.AddContext(NewContext(isolate));
CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);
}
// Must be out of HandleScope