Skip to content

Commit

Permalink
src: enable context snapshot after running per-context scripts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joyeecheung committed Apr 20, 2019
1 parent 52c5c05 commit 8248be6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using v8::Local;
using v8::Locker;
using v8::SealHandleScope;

const size_t NodeMainInstance::kNodeContextIndex = 0;

NodeMainInstance::NodeMainInstance(Isolate* isolate,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
Expand Down Expand Up @@ -171,9 +173,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());
Expand Down
2 changes: 2 additions & 0 deletions src/node_main_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class NodeMainInstance {
static const IndexArray* GetIsolateDataIndexes();
static v8::StartupData* GetEmbeddedSnapshotBlob();

static const size_t kNodeContextIndex;

private:
NodeMainInstance(const NodeMainInstance&) = delete;
NodeMainInstance& operator=(const NodeMainInstance&) = delete;
Expand Down
3 changes: 3 additions & 0 deletions tools/snapshot/snapshot_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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
Expand Down

0 comments on commit 8248be6

Please sign in to comment.