From eb23b176d8c7122af6add5ee38f87044288d3d39 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 20 Apr 2019 17:18:34 +0800 Subject: [PATCH] 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 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- src/node_main_instance.cc | 6 +++++- src/node_main_instance.h | 2 ++ tools/snapshot/snapshot_builder.cc | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index 419fd767720..212c6bf5f44 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -171,9 +171,13 @@ std::unique_ptr NodeMainInstance::CreateMainEnvironment( isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true); } - Local context = NewContext(isolate_); + Local context; if (deserialize_mode_) { + context = + Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked(); SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers); + } else { + context = NewContext(isolate_); } CHECK(!context.IsEmpty()); diff --git a/src/node_main_instance.h b/src/node_main_instance.h index 98c17cc8f01..531c919f052 100644 --- a/src/node_main_instance.h +++ b/src/node_main_instance.h @@ -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; diff --git a/tools/snapshot/snapshot_builder.cc b/tools/snapshot/snapshot_builder.cc index d1ec17e302a..835fca9f460 100644 --- a/tools/snapshot/snapshot_builder.cc +++ b/tools/snapshot/snapshot_builder.cc @@ -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