src: implement IsolateData serialization and deserialization · nodejs/node@631bea8 · GitHub
Skip to content

Commit 631bea8

Browse files
joyeecheungtargos
authored andcommitted
src: implement IsolateData serialization and deserialization
This patch allows serializing per-isolate data into an isolate snapshot and deserializing them from an isolate snapthot. PR-URL: #27321 Refs: #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>
1 parent a636338 commit 631bea8

4 files changed

Lines changed: 149 additions & 50 deletions

File tree

src/env.cc

Lines changed: 104 additions & 43 deletions

src/env.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "node.h"
3434
#include "node_binding.h"
3535
#include "node_http2_state.h"
36+
#include "node_main_instance.h"
3637
#include "node_options.h"
3738
#include "req_wrap.h"
3839
#include "util.h"
@@ -418,10 +419,12 @@ class IsolateData : public MemoryRetainer {
418419
IsolateData(v8::Isolate* isolate,
419420
uv_loop_t* event_loop,
420421
MultiIsolatePlatform* platform = nullptr,
421-
ArrayBufferAllocator* node_allocator = nullptr);
422+
ArrayBufferAllocator* node_allocator = nullptr,
423+
const NodeMainInstance::IndexArray* indexes = nullptr);
422424
SET_MEMORY_INFO_NAME(IsolateData);
423425
SET_SELF_SIZE(IsolateData);
424426
void MemoryInfo(MemoryTracker* tracker) const override;
427+
std::vector<size_t> Serialize(v8::SnapshotCreator* creator);
425428

426429
inline uv_loop_t* event_loop() const;
427430
inline MultiIsolatePlatform* platform() const;
@@ -451,6 +454,9 @@ class IsolateData : public MemoryRetainer {
451454
IsolateData& operator=(const IsolateData&) = delete;
452455

453456
private:
457+
void DeserializeProperties(const NodeMainInstance::IndexArray* indexes);
458+
void CreateProperties();
459+
454460
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
455461
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
456462
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)

src/node_main_instance.cc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
2323
isolate_(isolate),
2424
platform_(platform),
2525
isolate_data_(nullptr),
26-
owns_isolate_(false) {
26+
owns_isolate_(false),
27+
deserialize_mode_(false) {
2728
isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
2829
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
2930
}
@@ -41,7 +42,8 @@ NodeMainInstance::NodeMainInstance(Isolate::CreateParams* params,
4142
uv_loop_t* event_loop,
4243
MultiIsolatePlatform* platform,
4344
const std::vector<std::string>& args,
44-
const std::vector<std::string>& exec_args)
45+
const std::vector<std::string>& exec_args,
46+
const IndexArray* per_isolate_data_indexes)
4547
: args_(args),
4648
exec_args_(exec_args),
4749
array_buffer_allocator_(ArrayBufferAllocator::Create()),
@@ -58,10 +60,20 @@ NodeMainInstance::NodeMainInstance(Isolate::CreateParams* params,
5860
SetIsolateCreateParamsForNode(params);
5961
Isolate::Initialize(isolate_, *params);
6062

61-
isolate_data_.reset(new IsolateData(
62-
isolate_, event_loop, platform, array_buffer_allocator_.get()));
63+
deserialize_mode_ = per_isolate_data_indexes != nullptr;
64+
// If the indexes are not nullptr, we are not deserializing
65+
CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr);
66+
isolate_data_.reset(new IsolateData(isolate_,
67+
event_loop,
68+
platform,
69+
array_buffer_allocator_.get(),
70+
per_isolate_data_indexes));
6371
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
64-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
72+
if (!deserialize_mode_) {
73+
// If in deserialize mode, delay until after the deserialization is
74+
// complete.
75+
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
76+
}
6577
}
6678

6779
void NodeMainInstance::Dispose() {
@@ -160,6 +172,9 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
160172
}
161173

162174
Local<Context> context = NewContext(isolate_);
175+
if (deserialize_mode_) {
176+
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
177+
}
163178
CHECK(!context.IsEmpty());
164179
Context::Scope context_scope(context);
165180

src/node_main_instance.h

Lines changed: 18 additions & 1 deletion

0 commit comments

Comments
 (0)