Skip to content

Commit b981891

Browse files
joyeecheungguangwong
authored andcommitted
bootstrap: move embedded snapshot to SnapshotBuilder
So that the embedded snapshot can be reused by the worker. PR-URL: nodejs/node#42702 Refs: nodejs/node#35711 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d11dec5 commit b981891

10 files changed

+78
-44
lines changed

Diff for: node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@
558558
'src/node_revert.h',
559559
'src/node_root_certs.h',
560560
'src/node_snapshotable.h',
561+
'src/node_snapshot_builder.h',
561562
'src/node_sockaddr.h',
562563
'src/node_sockaddr-inl.h',
563564
'src/node_stat_watcher.h',

Diff for: src/node.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
#include "debug_utils-inl.h"
2727
#include "env-inl.h"
28-
#include "memory_tracker-inl.h"
2928
#include "histogram-inl.h"
29+
#include "memory_tracker-inl.h"
3030
#include "node_binding.h"
3131
#include "node_errors.h"
3232
#include "node_internals.h"
@@ -38,6 +38,7 @@
3838
#include "node_process-inl.h"
3939
#include "node_report.h"
4040
#include "node_revert.h"
41+
#include "node_snapshot_builder.h"
4142
#include "node_v8_platform-inl.h"
4243
#include "node_version.h"
4344

@@ -1198,7 +1199,7 @@ int Start(int argc, char** argv) {
11981199
bool use_node_snapshot =
11991200
per_process::cli_options->per_isolate->node_snapshot;
12001201
const SnapshotData* snapshot_data =
1201-
use_node_snapshot ? NodeMainInstance::GetEmbeddedSnapshotData()
1202+
use_node_snapshot ? SnapshotBuilder::GetEmbeddedSnapshotData()
12021203
: nullptr;
12031204
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);
12041205

Diff for: src/node_external_reference.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
namespace node {
88

99
const std::vector<intptr_t>& ExternalReferenceRegistry::external_references() {
10-
CHECK(!is_finalized_);
11-
external_references_.push_back(reinterpret_cast<intptr_t>(nullptr));
12-
is_finalized_ = true;
10+
if (!is_finalized_) {
11+
external_references_.push_back(reinterpret_cast<intptr_t>(nullptr));
12+
is_finalized_ = true;
13+
}
14+
1315
return external_references_;
1416
}
1517

Diff for: src/node_main_instance.cc

+3-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "node_external_reference.h"
55
#include "node_internals.h"
66
#include "node_options-inl.h"
7+
#include "node_snapshot_builder.h"
78
#include "node_snapshotable.h"
89
#include "node_v8_platform-inl.h"
910
#include "util-inl.h"
@@ -23,8 +24,6 @@ using v8::Isolate;
2324
using v8::Local;
2425
using v8::Locker;
2526

26-
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
27-
nullptr;
2827
NodeMainInstance::NodeMainInstance(Isolate* isolate,
2928
uv_loop_t* event_loop,
3029
MultiIsolatePlatform* platform,
@@ -43,13 +42,6 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
4342
SetIsolateMiscHandlers(isolate_, {});
4443
}
4544

46-
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
47-
// Cannot be called more than once.
48-
CHECK_NULL(registry_);
49-
registry_.reset(new ExternalReferenceRegistry());
50-
return registry_->external_references();
51-
}
52-
5345
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
5446
Isolate* isolate,
5547
uv_loop_t* event_loop,
@@ -75,13 +67,8 @@ NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
7567
snapshot_data_(snapshot_data) {
7668
isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
7769
if (snapshot_data != nullptr) {
78-
// TODO(joyeecheung): collect external references and set it in
79-
// params.external_references.
80-
const std::vector<intptr_t>& external_references =
81-
CollectExternalReferences();
82-
isolate_params_->external_references = external_references.data();
83-
isolate_params_->snapshot_blob =
84-
const_cast<v8::StartupData*>(&(snapshot_data->blob));
70+
SnapshotBuilder::InitializeIsolateParams(snapshot_data,
71+
isolate_params_.get());
8572
}
8673

8774
isolate_ = Isolate::Allocate();

Diff for: src/node_main_instance.h

-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ class NodeMainInstance {
6565
DeleteFnPtr<Environment, FreeEnvironment> CreateMainEnvironment(
6666
int* exit_code);
6767

68-
// If nullptr is returned, the binary is not built with embedded
69-
// snapshot.
70-
static const SnapshotData* GetEmbeddedSnapshotData();
71-
static const std::vector<intptr_t>& CollectExternalReferences();
72-
7368
static const size_t kNodeContextIndex = 0;
7469
NodeMainInstance(const NodeMainInstance&) = delete;
7570
NodeMainInstance& operator=(const NodeMainInstance&) = delete;

Diff for: src/node_snapshot_builder.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
#ifndef SRC_NODE_SNAPSHOT_BUILDER_H_
3+
#define SRC_NODE_SNAPSHOT_BUILDER_H_
4+
5+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6+
7+
#include <cstdint>
8+
#include "node_mutex.h"
9+
#include "v8.h"
10+
11+
namespace node {
12+
13+
class ExternalReferenceRegistry;
14+
struct SnapshotData;
15+
16+
class SnapshotBuilder {
17+
public:
18+
static std::string Generate(const std::vector<std::string> args,
19+
const std::vector<std::string> exec_args);
20+
21+
// Generate the snapshot into out.
22+
static void Generate(SnapshotData* out,
23+
const std::vector<std::string> args,
24+
const std::vector<std::string> exec_args);
25+
26+
// If nullptr is returned, the binary is not built with embedded
27+
// snapshot.
28+
static const SnapshotData* GetEmbeddedSnapshotData();
29+
static void InitializeIsolateParams(const SnapshotData* data,
30+
v8::Isolate::CreateParams* params);
31+
32+
private:
33+
// Used to synchronize access to the snapshot data
34+
static Mutex snapshot_data_mutex_;
35+
static const std::vector<intptr_t>& CollectExternalReferences();
36+
37+
static std::unique_ptr<ExternalReferenceRegistry> registry_;
38+
};
39+
} // namespace node
40+
41+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
42+
43+
#endif // SRC_NODE_SNAPSHOT_BUILDER_H_

Diff for: src/node_snapshot_stub.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// NODE_WANT_INTERNALS, so we define it here manually.
33
#define NODE_WANT_INTERNALS 1
44

5-
#include "node_main_instance.h"
5+
#include "node_snapshot_builder.h"
66

77
namespace node {
88

9-
const SnapshotData* NodeMainInstance::GetEmbeddedSnapshotData() {
9+
const SnapshotData* SnapshotBuilder::GetEmbeddedSnapshotData() {
1010
return nullptr;
1111
}
1212

Diff for: src/node_snapshotable.cc

+19-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "node_internals.h"
1313
#include "node_main_instance.h"
1414
#include "node_process.h"
15+
#include "node_snapshot_builder.h"
1516
#include "node_v8.h"
1617
#include "node_v8_platform-inl.h"
1718

@@ -49,7 +50,7 @@ std::string FormatBlob(SnapshotData* data) {
4950

5051
ss << R"(#include <cstddef>
5152
#include "env.h"
52-
#include "node_main_instance.h"
53+
#include "node_snapshot_builder.h"
5354
#include "v8.h"
5455
5556
// This file is generated by tools/snapshot. Do not edit.
@@ -78,11 +79,12 @@ SnapshotData snapshot_data {
7879
// -- isolate_data_indices ends --
7980
// -- env_info begins --
8081
)" << data->env_info
81-
<< R"(
82+
<< R"(
8283
// -- env_info ends --
8384
};
8485
85-
const SnapshotData* NodeMainInstance::GetEmbeddedSnapshotData() {
86+
const SnapshotData* SnapshotBuilder::GetEmbeddedSnapshotData() {
87+
Mutex::ScopedLock lock(snapshot_data_mutex_);
8688
return &snapshot_data;
8789
}
8890
} // namespace node
@@ -91,6 +93,19 @@ const SnapshotData* NodeMainInstance::GetEmbeddedSnapshotData() {
9193
return ss.str();
9294
}
9395

96+
Mutex SnapshotBuilder::snapshot_data_mutex_;
97+
98+
const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
99+
static auto registry = std::make_unique<ExternalReferenceRegistry>();
100+
return registry->external_references();
101+
}
102+
103+
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
104+
Isolate::CreateParams* params) {
105+
params->external_references = CollectExternalReferences().data();
106+
params->snapshot_blob = const_cast<v8::StartupData*>(&(data->blob));
107+
}
108+
94109
void SnapshotBuilder::Generate(SnapshotData* out,
95110
const std::vector<std::string> args,
96111
const std::vector<std::string> exec_args) {
@@ -104,7 +119,7 @@ void SnapshotBuilder::Generate(SnapshotData* out,
104119

105120
{
106121
const std::vector<intptr_t>& external_references =
107-
NodeMainInstance::CollectExternalReferences();
122+
CollectExternalReferences();
108123
SnapshotCreator creator(isolate, external_references.data());
109124
Environment* env;
110125
{

Diff for: src/node_snapshotable.h

+1-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace node {
1212
class Environment;
1313
struct EnvSerializeInfo;
1414
struct SnapshotData;
15+
class ExternalReferenceRegistry;
1516

1617
#define SERIALIZABLE_OBJECT_TYPES(V) \
1718
V(fs_binding_data, fs::BindingData) \
@@ -122,17 +123,6 @@ void SerializeBindingData(Environment* env,
122123
EnvSerializeInfo* info);
123124

124125
bool IsSnapshotableType(FastStringKey key);
125-
126-
class SnapshotBuilder {
127-
public:
128-
static std::string Generate(const std::vector<std::string> args,
129-
const std::vector<std::string> exec_args);
130-
131-
// Generate the snapshot into out.
132-
static void Generate(SnapshotData* out,
133-
const std::vector<std::string> args,
134-
const std::vector<std::string> exec_args);
135-
};
136126
} // namespace node
137127

138128
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

Diff for: tools/snapshot/node_mksnapshot.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "libplatform/libplatform.h"
99
#include "node_internals.h"
10-
#include "node_snapshotable.h"
10+
#include "node_snapshot_builder.h"
1111
#include "util-inl.h"
1212
#include "v8.h"
1313

0 commit comments

Comments
 (0)