Skip to content

Commit 43d2e24

Browse files
committed
bootstrap: move embedded snapshot to SnapshotBuilder
So that the embedded snapshot can be reused by the worker. PR-URL: #42702 Refs: #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 77373aa commit 43d2e24

10 files changed

+78
-44
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@
637637
'src/node_revert.h',
638638
'src/node_root_certs.h',
639639
'src/node_snapshotable.h',
640+
'src/node_snapshot_builder.h',
640641
'src/node_sockaddr.h',
641642
'src/node_sockaddr-inl.h',
642643
'src/node_stat_watcher.h',

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

@@ -1171,7 +1172,7 @@ int Start(int argc, char** argv) {
11711172
bool use_node_snapshot =
11721173
per_process::cli_options->per_isolate->node_snapshot;
11731174
const SnapshotData* snapshot_data =
1174-
use_node_snapshot ? NodeMainInstance::GetEmbeddedSnapshotData()
1175+
use_node_snapshot ? SnapshotBuilder::GetEmbeddedSnapshotData()
11751176
: nullptr;
11761177
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);
11771178

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

src/node_main_instance.cc

+3-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "node_external_reference.h"
88
#include "node_internals.h"
99
#include "node_options-inl.h"
10+
#include "node_snapshot_builder.h"
1011
#include "node_snapshotable.h"
1112
#include "node_v8_platform-inl.h"
1213
#include "util-inl.h"
@@ -26,8 +27,6 @@ using v8::Isolate;
2627
using v8::Local;
2728
using v8::Locker;
2829

29-
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
30-
nullptr;
3130
NodeMainInstance::NodeMainInstance(Isolate* isolate,
3231
uv_loop_t* event_loop,
3332
MultiIsolatePlatform* platform,
@@ -46,13 +45,6 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
4645
SetIsolateMiscHandlers(isolate_, {});
4746
}
4847

49-
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
50-
// Cannot be called more than once.
51-
CHECK_NULL(registry_);
52-
registry_.reset(new ExternalReferenceRegistry());
53-
return registry_->external_references();
54-
}
55-
5648
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
5749
Isolate* isolate,
5850
uv_loop_t* event_loop,
@@ -78,13 +70,8 @@ NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
7870
snapshot_data_(snapshot_data) {
7971
isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
8072
if (snapshot_data != nullptr) {
81-
// TODO(joyeecheung): collect external references and set it in
82-
// params.external_references.
83-
const std::vector<intptr_t>& external_references =
84-
CollectExternalReferences();
85-
isolate_params_->external_references = external_references.data();
86-
isolate_params_->snapshot_blob =
87-
const_cast<v8::StartupData*>(&(snapshot_data->blob));
73+
SnapshotBuilder::InitializeIsolateParams(snapshot_data,
74+
isolate_params_.get());
8875
}
8976

9077
isolate_ = Isolate::Allocate();

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;

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_

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

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
{

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

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)