Skip to content

Commit

Permalink
src: per-isolate eternal template properties
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jul 17, 2022
1 parent 8baf372 commit 5b92a18
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 40 deletions.
33 changes: 26 additions & 7 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,16 @@ void Environment::set_process_exit_handler(
#undef VY
#undef VP

#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
return PropertyName##_.Get(isolate_); \
} \
inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) { \
PropertyName##_.Set(isolate_, value); \
}
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
#undef V

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
Expand All @@ -891,14 +901,23 @@ void Environment::set_process_exit_handler(
#undef VY
#undef VP

#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> Environment::PropertyName() const { \
return PersistentToLocal::Strong(PropertyName ## _); \
} \
inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
PropertyName ## _.Reset(isolate(), value); \
#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> Environment::PropertyName() const { \
return isolate_data()->PropertyName(); \
} \
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
isolate_data()->set_##PropertyName(value); \
}
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
#undef V

#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> Environment::PropertyName() const { \
return PersistentToLocal::Strong(PropertyName##_); \
} \
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
PropertyName##_.Reset(isolate(), value); \
}
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
#undef V

Expand Down
58 changes: 32 additions & 26 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) {
#undef VY
#undef VS
#undef VP

#define V(PropertyName, TypeName) \
do { \
Local<TypeName> field = PropertyName(); \
if (!field.IsEmpty()) { \
indexes.push_back(creator->AddData(field)); \
} else { \
indexes.push_back(0); \
} \
} while (0);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
#undef V

for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++)
indexes.push_back(creator->AddData(async_wrap_provider(i)));

Expand Down Expand Up @@ -290,6 +303,22 @@ void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) {
#undef VS
#undef VP

#define V(PropertyName, TypeName) \
do { \
size_t index = (*indexes)[i++]; \
if (index != 0) { \
MaybeLocal<TypeName> maybe_field = \
isolate_->GetDataFromSnapshotOnce<TypeName>(index); \
Local<TypeName> field; \
if (!maybe_field.ToLocal(&field)) { \
fprintf(stderr, "Failed to deserialize " #PropertyName "\n"); \
} \
PropertyName##_.Set(isolate_, field); \
} \
} while (0);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
#undef V

for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
MaybeLocal<String> maybe_field =
isolate_->GetDataFromSnapshotOnce<String>((*indexes)[i++]);
Expand Down Expand Up @@ -1749,19 +1778,6 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
should_abort_on_uncaught_toggle_.Serialize(ctx, creator);

size_t id = 0;
#define V(PropertyName, TypeName) \
do { \
Local<TypeName> field = PropertyName(); \
if (!field.IsEmpty()) { \
size_t index = creator->AddData(field); \
info.persistent_templates.push_back({#PropertyName, id, index}); \
} \
id++; \
} while (0);
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
#undef V

id = 0;
#define V(PropertyName, TypeName) \
do { \
Local<TypeName> field = PropertyName(); \
Expand Down Expand Up @@ -1818,9 +1834,6 @@ std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) {
<< i.stream_base_state << ", // stream_base_state\n"
<< i.should_abort_on_uncaught_toggle
<< ", // should_abort_on_uncaught_toggle\n"
<< "// -- persistent_templates begins --\n"
<< i.persistent_templates << ",\n"
<< "// persistent_templates ends --\n"
<< "// -- persistent_values begins --\n"
<< i.persistent_values << ",\n"
<< "// -- persistent_values ends --\n"
Expand Down Expand Up @@ -1869,7 +1882,7 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
std::cerr << *info << "\n";
}

const std::vector<PropInfo>& templates = info->persistent_templates;
const std::vector<PropInfo>& values = info->persistent_values;
size_t i = 0; // index to the array
size_t id = 0;
#define SetProperty(PropertyName, TypeName, vector, type, from) \
Expand All @@ -1888,16 +1901,9 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
set_##PropertyName(field); \
i++; \
} \
} while (0); \
id++;
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
templates, template, isolate_)
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V);
#undef V
id++; \
} while (0);

i = 0; // index to the array
id = 0;
const std::vector<PropInfo>& values = info->persistent_values;
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
values, value, ctx)
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V);
Expand Down
16 changes: 12 additions & 4 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ class NoArrayBufferZeroFillScope {
V(x_forwarded_string, "x-forwarded-for") \
V(zero_return_string, "ZERO_RETURN")

#define ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) \
#define PER_ISOLATE_TEMPLATE_PROPERTIES(V) \
V(async_wrap_ctor_template, v8::FunctionTemplate) \
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
V(base_object_ctor_template, v8::FunctionTemplate) \
Expand Down Expand Up @@ -609,6 +609,13 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
#undef VY
#undef VS
#undef VP

#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> PropertyName() const; \
inline void set_##PropertyName(v8::Local<TypeName> value);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
#undef V

inline v8::Local<v8::String> async_wrap_provider(int index) const;

size_t max_young_gen_size = 1;
Expand All @@ -627,11 +634,14 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
#define VT(PropertyName, TypeName) V(TypeName, PropertyName)
#define V(TypeName, PropertyName) \
v8::Eternal<TypeName> PropertyName ## _;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_SYMBOL_PROPERTIES(VY)
PER_ISOLATE_STRING_PROPERTIES(VS)
PER_ISOLATE_TEMPLATE_PROPERTIES(VT)
#undef V
#undef V
#undef VY
#undef VS
Expand Down Expand Up @@ -955,7 +965,6 @@ struct EnvSerializeInfo {
AliasedBufferIndex stream_base_state;
AliasedBufferIndex should_abort_on_uncaught_toggle;

std::vector<PropInfo> persistent_templates;
std::vector<PropInfo> persistent_values;

SnapshotIndex context;
Expand Down Expand Up @@ -1342,8 +1351,8 @@ class Environment : public MemoryRetainer {
#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> PropertyName() const; \
inline void set_ ## PropertyName(v8::Local<TypeName> value);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
#undef V

inline v8::Local<v8::Context> context() const;
Expand Down Expand Up @@ -1646,7 +1655,6 @@ class Environment : public MemoryRetainer {

#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName ## _;
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
#undef V

v8::Global<v8::Context> context_;
Expand Down
5 changes: 2 additions & 3 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,6 @@ int SnapshotBuilder::Generate(SnapshotData* out,
}
});

out->isolate_data_indices =
main_instance->isolate_data()->Serialize(&creator);

// The default context with only things created by V8.
Local<Context> default_context = Context::New(isolate);

Expand Down Expand Up @@ -285,6 +282,8 @@ int SnapshotBuilder::Generate(SnapshotData* out,
}

// Serialize the native states
out->isolate_data_indices =
main_instance->isolate_data()->Serialize(&creator);
out->env_info = env->Serialize(&creator);

#ifdef NODE_USE_NODE_CODE_CACHE
Expand Down

0 comments on commit 5b92a18

Please sign in to comment.