From b5cd93c66154c7911e56b26ba130feac53e64fb9 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 12 Jul 2022 23:34:10 +0800 Subject: [PATCH] src: per-isolate eternal template properties --- src/env-inl.h | 34 ++++++++++++++++++----- src/env.cc | 58 ++++++++++++++++++++++------------------ src/env.h | 19 +++++++++---- src/node_snapshotable.cc | 5 ++-- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 084cb9f83c51af..e2da40141d7740 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -876,6 +876,16 @@ void Environment::set_process_exit_handler( #undef VY #undef VP +#define V(PropertyName, TypeName) \ + inline v8::Local IsolateData::PropertyName() const { \ + return PropertyName##_.Get(isolate_); \ + } \ + inline void IsolateData::set_##PropertyName(v8::Local 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) @@ -891,14 +901,24 @@ void Environment::set_process_exit_handler( #undef VY #undef VP -#define V(PropertyName, TypeName) \ - inline v8::Local Environment::PropertyName() const { \ - return PersistentToLocal::Strong(PropertyName ## _); \ - } \ - inline void Environment::set_ ## PropertyName(v8::Local value) { \ - PropertyName ## _.Reset(isolate(), value); \ +#define V(PropertyName, TypeName) \ + inline v8::Local Environment::PropertyName() const { \ + return isolate_data()->PropertyName(); \ + } \ + inline void Environment::set_##PropertyName(v8::Local value) { \ + DCHECK(isolate_data()->PropertyName().IsEmpty()); \ + isolate_data()->set_##PropertyName(value); \ + } + PER_ISOLATE_TEMPLATE_PROPERTIES(V) +#undef V + +#define V(PropertyName, TypeName) \ + inline v8::Local Environment::PropertyName() const { \ + return PersistentToLocal::Strong(PropertyName##_); \ + } \ + inline void Environment::set_##PropertyName(v8::Local value) { \ + PropertyName##_.Reset(isolate(), value); \ } - ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) #undef V diff --git a/src/env.cc b/src/env.cc index e4a708ec1de645..aab39ea0f69085 100644 --- a/src/env.cc +++ b/src/env.cc @@ -259,6 +259,19 @@ std::vector IsolateData::Serialize(SnapshotCreator* creator) { #undef VY #undef VS #undef VP + +#define V(PropertyName, TypeName) \ + do { \ + Local 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))); @@ -290,6 +303,22 @@ void IsolateData::DeserializeProperties(const std::vector* indexes) { #undef VS #undef VP +#define V(PropertyName, TypeName) \ + do { \ + size_t index = (*indexes)[i++]; \ + if (index != 0) { \ + MaybeLocal maybe_field = \ + isolate_->GetDataFromSnapshotOnce(index); \ + Local 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 maybe_field = isolate_->GetDataFromSnapshotOnce((*indexes)[i++]); @@ -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 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 field = PropertyName(); \ @@ -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" @@ -1869,7 +1882,7 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) { std::cerr << *info << "\n"; } - const std::vector& templates = info->persistent_templates; + const std::vector& values = info->persistent_values; size_t i = 0; // index to the array size_t id = 0; #define SetProperty(PropertyName, TypeName, vector, type, from) \ @@ -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& values = info->persistent_values; #define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \ values, value, ctx) ENVIRONMENT_STRONG_PERSISTENT_VALUES(V); diff --git a/src/env.h b/src/env.h index 04528583cb47f4..b7dce51cd7ef63 100644 --- a/src/env.h +++ b/src/env.h @@ -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) \ @@ -609,6 +609,13 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { #undef VY #undef VS #undef VP + +#define V(PropertyName, TypeName) \ + inline v8::Local PropertyName() const; \ + inline void set_##PropertyName(v8::Local value); + PER_ISOLATE_TEMPLATE_PROPERTIES(V) +#undef V + inline v8::Local async_wrap_provider(int index) const; size_t max_young_gen_size = 1; @@ -627,14 +634,18 @@ 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 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 VY +#undef V +#undef VT #undef VS +#undef VY #undef VP // Keep a list of all Persistent strings used for AsyncWrap Provider types. std::array, AsyncWrap::PROVIDERS_LENGTH> @@ -955,7 +966,6 @@ struct EnvSerializeInfo { AliasedBufferIndex stream_base_state; AliasedBufferIndex should_abort_on_uncaught_toggle; - std::vector persistent_templates; std::vector persistent_values; SnapshotIndex context; @@ -1342,8 +1352,8 @@ class Environment : public MemoryRetainer { #define V(PropertyName, TypeName) \ inline v8::Local PropertyName() const; \ inline void set_ ## PropertyName(v8::Local value); + PER_ISOLATE_TEMPLATE_PROPERTIES(V) ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) - ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) #undef V inline v8::Local context() const; @@ -1646,7 +1656,6 @@ class Environment : public MemoryRetainer { #define V(PropertyName, TypeName) v8::Global PropertyName ## _; ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) - ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) #undef V v8::Global context_; diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 7375c8cdecf286..e67b3c10391466 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -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 default_context = Context::New(isolate); @@ -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