Skip to content

Commit eecdd20

Browse files
committed
src: per-isolate eternal template properties
1 parent a8f6c56 commit eecdd20

File tree

4 files changed

+72
-39
lines changed

4 files changed

+72
-39
lines changed

src/env-inl.h

+26-7
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,16 @@ void Environment::set_process_exit_handler(
876876
#undef VY
877877
#undef VP
878878

879+
#define V(PropertyName, TypeName) \
880+
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
881+
return PropertyName##_.Get(isolate_); \
882+
} \
883+
inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) { \
884+
PropertyName##_.Set(isolate_, value); \
885+
}
886+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
887+
#undef V
888+
879889
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
880890
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
881891
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -891,14 +901,23 @@ void Environment::set_process_exit_handler(
891901
#undef VY
892902
#undef VP
893903

894-
#define V(PropertyName, TypeName) \
895-
inline v8::Local<TypeName> Environment::PropertyName() const { \
896-
return PersistentToLocal::Strong(PropertyName ## _); \
897-
} \
898-
inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
899-
PropertyName ## _.Reset(isolate(), value); \
904+
#define V(PropertyName, TypeName) \
905+
inline v8::Local<TypeName> Environment::PropertyName() const { \
906+
return isolate_data()->PropertyName(); \
907+
} \
908+
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
909+
isolate_data()->set_##PropertyName(value); \
910+
}
911+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
912+
#undef V
913+
914+
#define V(PropertyName, TypeName) \
915+
inline v8::Local<TypeName> Environment::PropertyName() const { \
916+
return PersistentToLocal::Strong(PropertyName##_); \
917+
} \
918+
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
919+
PropertyName##_.Reset(isolate(), value); \
900920
}
901-
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
902921
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
903922
#undef V
904923

src/env.cc

+32-26
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) {
259259
#undef VY
260260
#undef VS
261261
#undef VP
262+
263+
#define V(PropertyName, TypeName) \
264+
do { \
265+
Local<TypeName> field = PropertyName(); \
266+
if (!field.IsEmpty()) { \
267+
indexes.push_back(creator->AddData(field)); \
268+
} else { \
269+
indexes.push_back(0); \
270+
} \
271+
} while (0);
272+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
273+
#undef V
274+
262275
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++)
263276
indexes.push_back(creator->AddData(async_wrap_provider(i)));
264277

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

306+
#define V(PropertyName, TypeName) \
307+
do { \
308+
size_t index = (*indexes)[i++]; \
309+
if (index != 0) { \
310+
MaybeLocal<TypeName> maybe_field = \
311+
isolate_->GetDataFromSnapshotOnce<TypeName>(index); \
312+
Local<TypeName> field; \
313+
if (!maybe_field.ToLocal(&field)) { \
314+
fprintf(stderr, "Failed to deserialize " #PropertyName "\n"); \
315+
} \
316+
PropertyName##_.Set(isolate_, field); \
317+
} \
318+
} while (0);
319+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
320+
#undef V
321+
293322
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
294323
MaybeLocal<String> maybe_field =
295324
isolate_->GetDataFromSnapshotOnce<String>((*indexes)[i++]);
@@ -1751,19 +1780,6 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
17511780
should_abort_on_uncaught_toggle_.Serialize(ctx, creator);
17521781

17531782
size_t id = 0;
1754-
#define V(PropertyName, TypeName) \
1755-
do { \
1756-
Local<TypeName> field = PropertyName(); \
1757-
if (!field.IsEmpty()) { \
1758-
size_t index = creator->AddData(field); \
1759-
info.persistent_templates.push_back({#PropertyName, id, index}); \
1760-
} \
1761-
id++; \
1762-
} while (0);
1763-
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
1764-
#undef V
1765-
1766-
id = 0;
17671783
#define V(PropertyName, TypeName) \
17681784
do { \
17691785
Local<TypeName> field = PropertyName(); \
@@ -1820,9 +1836,6 @@ std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) {
18201836
<< i.stream_base_state << ", // stream_base_state\n"
18211837
<< i.should_abort_on_uncaught_toggle
18221838
<< ", // should_abort_on_uncaught_toggle\n"
1823-
<< "// -- persistent_templates begins --\n"
1824-
<< i.persistent_templates << ",\n"
1825-
<< "// persistent_templates ends --\n"
18261839
<< "// -- persistent_values begins --\n"
18271840
<< i.persistent_values << ",\n"
18281841
<< "// -- persistent_values ends --\n"
@@ -1871,7 +1884,7 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18711884
std::cerr << *info << "\n";
18721885
}
18731886

1874-
const std::vector<PropInfo>& templates = info->persistent_templates;
1887+
const std::vector<PropInfo>& values = info->persistent_values;
18751888
size_t i = 0; // index to the array
18761889
size_t id = 0;
18771890
#define SetProperty(PropertyName, TypeName, vector, type, from) \
@@ -1890,16 +1903,9 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18901903
set_##PropertyName(field); \
18911904
i++; \
18921905
} \
1893-
} while (0); \
1894-
id++;
1895-
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
1896-
templates, template, isolate_)
1897-
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V);
1898-
#undef V
1906+
id++; \
1907+
} while (0);
18991908

1900-
i = 0; // index to the array
1901-
id = 0;
1902-
const std::vector<PropInfo>& values = info->persistent_values;
19031909
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
19041910
values, value, ctx)
19051911
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V);

src/env.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ class NoArrayBufferZeroFillScope {
469469
V(x_forwarded_string, "x-forwarded-for") \
470470
V(zero_return_string, "ZERO_RETURN")
471471

472-
#define ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) \
472+
#define PER_ISOLATE_TEMPLATE_PROPERTIES(V) \
473473
V(async_wrap_ctor_template, v8::FunctionTemplate) \
474474
V(async_wrap_object_ctor_template, v8::FunctionTemplate) \
475475
V(base_object_ctor_template, v8::FunctionTemplate) \
@@ -609,6 +609,13 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
609609
#undef VY
610610
#undef VS
611611
#undef VP
612+
613+
#define V(PropertyName, TypeName) \
614+
inline v8::Local<TypeName> PropertyName() const; \
615+
inline void set_##PropertyName(v8::Local<TypeName> value);
616+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
617+
#undef V
618+
612619
inline v8::Local<v8::String> async_wrap_provider(int index) const;
613620

614621
size_t max_young_gen_size = 1;
@@ -627,11 +634,14 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
627634
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
628635
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
629636
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
637+
#define VT(PropertyName, TypeName) V(TypeName, PropertyName)
630638
#define V(TypeName, PropertyName) \
631639
v8::Eternal<TypeName> PropertyName ## _;
632640
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
633641
PER_ISOLATE_SYMBOL_PROPERTIES(VY)
634642
PER_ISOLATE_STRING_PROPERTIES(VS)
643+
PER_ISOLATE_TEMPLATE_PROPERTIES(VT)
644+
#undef V
635645
#undef V
636646
#undef VY
637647
#undef VS
@@ -983,7 +993,6 @@ struct EnvSerializeInfo {
983993
AliasedBufferIndex stream_base_state;
984994
AliasedBufferIndex should_abort_on_uncaught_toggle;
985995

986-
std::vector<PropInfo> persistent_templates;
987996
std::vector<PropInfo> persistent_values;
988997

989998
SnapshotIndex context;
@@ -1357,8 +1366,8 @@ class Environment : public MemoryRetainer {
13571366
#define V(PropertyName, TypeName) \
13581367
inline v8::Local<TypeName> PropertyName() const; \
13591368
inline void set_ ## PropertyName(v8::Local<TypeName> value);
1369+
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
13601370
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
1361-
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
13621371
#undef V
13631372

13641373
inline v8::Local<v8::Context> context() const;
@@ -1651,7 +1660,6 @@ class Environment : public MemoryRetainer {
16511660

16521661
#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName ## _;
16531662
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
1654-
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
16551663
#undef V
16561664

16571665
v8::Global<v8::Context> context_;

src/node_snapshotable.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ void SnapshotBuilder::Generate(SnapshotData* out,
189189
per_process::v8_platform.Platform(),
190190
args,
191191
exec_args);
192-
out->isolate_data_indices =
193-
main_instance->isolate_data()->Serialize(&creator);
194192

195193
HandleScope scope(isolate);
196194

@@ -278,6 +276,8 @@ void SnapshotBuilder::Generate(SnapshotData* out,
278276
}
279277

280278
// Serialize the native states
279+
out->isolate_data_indices =
280+
main_instance->isolate_data()->Serialize(&creator);
281281
out->env_info = env->Serialize(&creator);
282282
// Serialize the context
283283
size_t index = creator.AddContext(

0 commit comments

Comments
 (0)