Skip to content

Commit 0e9f9b7

Browse files
addaleaxBethGriggs
authored andcommitted
src: include AsyncWrap provider strings in snapshot
… and move them to `IsolateData`, because they should exist once per Isolate. PR-URL: #32572 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent c5763e8 commit 0e9f9b7

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/env-inl.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ inline worker::Worker* IsolateData::worker_context() const {
7373
return worker_context_;
7474
}
7575

76+
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
77+
return async_wrap_providers_[index].Get(isolate_);
78+
}
79+
7680
inline AsyncHooks::AsyncHooks()
7781
: async_ids_stack_(env()->isolate(), 16 * 2),
7882
fields_(env()->isolate(), kFieldsCount),
@@ -95,20 +99,6 @@ inline AsyncHooks::AsyncHooks()
9599
// kAsyncIdCounter should start at 1 because that'll be the id the execution
96100
// context during bootstrap (code that runs before entering uv_run()).
97101
async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
98-
99-
// Create all the provider strings that will be passed to JS. Place them in
100-
// an array so the array index matches the PROVIDER id offset. This way the
101-
// strings can be retrieved quickly.
102-
#define V(Provider) \
103-
providers_[AsyncWrap::PROVIDER_ ## Provider].Set( \
104-
env()->isolate(), \
105-
v8::String::NewFromOneByte( \
106-
env()->isolate(), \
107-
reinterpret_cast<const uint8_t*>(#Provider), \
108-
v8::NewStringType::kInternalized, \
109-
sizeof(#Provider) - 1).ToLocalChecked());
110-
NODE_ASYNC_PROVIDER_TYPES(V)
111-
#undef V
112102
}
113103
inline AliasedUint32Array& AsyncHooks::fields() {
114104
return fields_;
@@ -127,7 +117,7 @@ inline v8::Local<v8::Array> AsyncHooks::execution_async_resources() {
127117
}
128118

129119
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
130-
return providers_[idx].Get(env()->isolate());
120+
return env()->isolate_data()->async_wrap_provider(idx);
131121
}
132122

133123
inline void AsyncHooks::no_force_checks() {

src/env.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) {
7676
#undef VY
7777
#undef VS
7878
#undef VP
79+
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++)
80+
indexes.push_back(creator->AddData(async_wrap_provider(i)));
7981

8082
return indexes;
8183
}
@@ -103,6 +105,15 @@ void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) {
103105
#undef VY
104106
#undef VS
105107
#undef VP
108+
109+
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
110+
MaybeLocal<String> field =
111+
isolate_->GetDataFromSnapshotOnce<String>((*indexes)[i++]);
112+
if (field.IsEmpty()) {
113+
fprintf(stderr, "Failed to deserialize AsyncWrap provider %zu\n", j);
114+
}
115+
async_wrap_providers_[j].Set(isolate_, field.ToLocalChecked());
116+
}
106117
}
107118

108119
void IsolateData::CreateProperties() {
@@ -153,6 +164,20 @@ void IsolateData::CreateProperties() {
153164
.ToLocalChecked());
154165
PER_ISOLATE_STRING_PROPERTIES(V)
155166
#undef V
167+
168+
// Create all the provider strings that will be passed to JS. Place them in
169+
// an array so the array index matches the PROVIDER id offset. This way the
170+
// strings can be retrieved quickly.
171+
#define V(Provider) \
172+
async_wrap_providers_[AsyncWrap::PROVIDER_ ## Provider].Set( \
173+
isolate_, \
174+
v8::String::NewFromOneByte( \
175+
isolate_, \
176+
reinterpret_cast<const uint8_t*>(#Provider), \
177+
v8::NewStringType::kInternalized, \
178+
sizeof(#Provider) - 1).ToLocalChecked());
179+
NODE_ASYNC_PROVIDER_TYPES(V)
180+
#undef V
156181
}
157182

158183
IsolateData::IsolateData(Isolate* isolate,
@@ -190,6 +215,8 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
190215
PER_ISOLATE_STRING_PROPERTIES(V)
191216
#undef V
192217

218+
tracker->TrackField("async_wrap_providers", async_wrap_providers_);
219+
193220
if (node_allocator_ != nullptr) {
194221
tracker->TrackFieldWithSize(
195222
"node_allocator", sizeof(*node_allocator_), "NodeArrayBufferAllocator");
@@ -951,7 +978,6 @@ void TickInfo::MemoryInfo(MemoryTracker* tracker) const {
951978
}
952979

953980
void AsyncHooks::MemoryInfo(MemoryTracker* tracker) const {
954-
tracker->TrackField("providers", providers_);
955981
tracker->TrackField("async_ids_stack", async_ids_stack_);
956982
tracker->TrackField("fields", fields_);
957983
tracker->TrackField("async_id_fields", async_id_fields_);

src/env.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ class IsolateData : public MemoryRetainer {
512512
#undef VY
513513
#undef VS
514514
#undef VP
515+
inline v8::Local<v8::String> async_wrap_provider(int index) const;
515516

516517
std::unordered_map<const char*, v8::Eternal<v8::String>> http_static_strs;
517518
inline v8::Isolate* isolate() const;
@@ -536,6 +537,9 @@ class IsolateData : public MemoryRetainer {
536537
#undef VY
537538
#undef VS
538539
#undef VP
540+
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
541+
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
542+
async_wrap_providers_;
539543

540544
v8::Isolate* const isolate_;
541545
uv_loop_t* const event_loop_;
@@ -694,8 +698,6 @@ class AsyncHooks : public MemoryRetainer {
694698
private:
695699
friend class Environment; // So we can call the constructor.
696700
inline AsyncHooks();
697-
// Keep a list of all Persistent strings used for Provider types.
698-
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH> providers_;
699701
// Stores the ids of the current execution context stack.
700702
AliasedFloat64Array async_ids_stack_;
701703
// Attached to a Uint32Array that tracks the number of active hooks for

0 commit comments

Comments
 (0)