Skip to content

Commit 150b8f7

Browse files
committed
src: minor c++ refactors to module_wrap
- Move `module_map` to `Environment` instead of having it be global state - `std::map` → `std::unordered_map` - Remove one level of indirection for the map values - Clean up empty vectors in `module_map` - Call `Reset()` on all persistent handles in `resolve_cache_` - Add a missing `HandleScope` to `ModuleWrap::~ModuleWrap()` PR-URL: #15515 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent b605b15 commit 150b8f7

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

src/env.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ namespace performance {
5252
struct performance_state;
5353
}
5454

55+
namespace loader {
56+
class ModuleWrap;
57+
}
58+
5559
// Pick an index that's hopefully out of the way when we're embedded inside
5660
// another application. Performance-wise or memory-wise it doesn't matter:
5761
// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
@@ -585,6 +589,8 @@ class Environment {
585589
// List of id's that have been destroyed and need the destroy() cb called.
586590
inline std::vector<double>* destroy_ids_list();
587591

592+
std::unordered_multimap<int, loader::ModuleWrap*> module_map;
593+
588594
inline double* heap_statistics_buffer() const;
589595
inline void set_heap_statistics_buffer(double* pointer);
590596

src/module_wrap.cc

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using v8::EscapableHandleScope;
1818
using v8::Function;
1919
using v8::FunctionCallbackInfo;
2020
using v8::FunctionTemplate;
21+
using v8::HandleScope;
2122
using v8::Integer;
2223
using v8::IntegrityLevel;
2324
using v8::Isolate;
@@ -27,32 +28,31 @@ using v8::Maybe;
2728
using v8::MaybeLocal;
2829
using v8::Module;
2930
using v8::Object;
30-
using v8::Persistent;
3131
using v8::Promise;
3232
using v8::ScriptCompiler;
3333
using v8::ScriptOrigin;
3434
using v8::String;
3535
using v8::Value;
3636

37-
static const char* EXTENSIONS[] = {".mjs", ".js", ".json", ".node"};
38-
std::map<int, std::vector<ModuleWrap*>*> ModuleWrap::module_map_;
37+
static const char* const EXTENSIONS[] = {".mjs", ".js", ".json", ".node"};
3938

4039
ModuleWrap::ModuleWrap(Environment* env,
4140
Local<Object> object,
4241
Local<Module> module,
4342
Local<String> url) : BaseObject(env, object) {
44-
Isolate* iso = Isolate::GetCurrent();
45-
module_.Reset(iso, module);
46-
url_.Reset(iso, url);
43+
module_.Reset(env->isolate(), module);
44+
url_.Reset(env->isolate(), url);
4745
}
4846

4947
ModuleWrap::~ModuleWrap() {
50-
Local<Module> module = module_.Get(Isolate::GetCurrent());
51-
std::vector<ModuleWrap*>* same_hash = module_map_[module->GetIdentityHash()];
52-
auto it = std::find(same_hash->begin(), same_hash->end(), this);
53-
54-
if (it != same_hash->end()) {
55-
same_hash->erase(it);
48+
HandleScope scope(env()->isolate());
49+
Local<Module> module = module_.Get(env()->isolate());
50+
auto range = env()->module_map.equal_range(module->GetIdentityHash());
51+
for (auto it = range.first; it != range.second; ++it) {
52+
if (it->second == this) {
53+
env()->module_map.erase(it);
54+
break;
55+
}
5656
}
5757

5858
module_.Reset();
@@ -120,12 +120,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
120120
ModuleWrap* obj =
121121
new ModuleWrap(Environment::GetCurrent(ctx), that, mod, url);
122122

123-
if (ModuleWrap::module_map_.count(mod->GetIdentityHash()) == 0) {
124-
ModuleWrap::module_map_[mod->GetIdentityHash()] =
125-
new std::vector<ModuleWrap*>();
126-
}
127-
128-
ModuleWrap::module_map_[mod->GetIdentityHash()]->push_back(obj);
123+
env->module_map.emplace(mod->GetIdentityHash(), obj);
129124
Wrap(that, obj);
130125

131126
that->SetIntegrityLevel(ctx, IntegrityLevel::kFrozen);
@@ -171,8 +166,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
171166
env->ThrowError("linking error, expected resolver to return a promise");
172167
}
173168
Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
174-
obj->resolve_cache_[specifier_std] = new Persistent<Promise>();
175-
obj->resolve_cache_[specifier_std]->Reset(iso, resolve_promise);
169+
obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);
176170
}
177171

178172
args.GetReturnValue().Set(handle_scope.Escape(that));
@@ -188,6 +182,8 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
188182
Maybe<bool> ok = mod->InstantiateModule(ctx, ModuleWrap::ResolveCallback);
189183

190184
// clear resolve cache on instantiate
185+
for (auto& entry : obj->resolve_cache_)
186+
entry.second.Reset();
191187
obj->resolve_cache_.clear();
192188

193189
if (!ok.FromMaybe(false)) {
@@ -215,18 +211,17 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
215211
Local<Module> referrer) {
216212
Environment* env = Environment::GetCurrent(context);
217213
Isolate* iso = Isolate::GetCurrent();
218-
if (ModuleWrap::module_map_.count(referrer->GetIdentityHash()) == 0) {
214+
if (env->module_map.count(referrer->GetIdentityHash()) == 0) {
219215
env->ThrowError("linking error, unknown module");
220216
return MaybeLocal<Module>();
221217
}
222218

223-
std::vector<ModuleWrap*>* possible_deps =
224-
ModuleWrap::module_map_[referrer->GetIdentityHash()];
225219
ModuleWrap* dependent = nullptr;
226-
227-
for (auto possible_dep : *possible_deps) {
228-
if (possible_dep->module_ == referrer) {
229-
dependent = possible_dep;
220+
auto range = env->module_map.equal_range(referrer->GetIdentityHash());
221+
for (auto it = range.first; it != range.second; ++it) {
222+
if (it->second->module_ == referrer) {
223+
dependent = it->second;
224+
break;
230225
}
231226
}
232227

@@ -244,7 +239,7 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
244239
}
245240

246241
Local<Promise> resolve_promise =
247-
dependent->resolve_cache_[specifier_std]->Get(iso);
242+
dependent->resolve_cache_[specifier_std].Get(iso);
248243

249244
if (resolve_promise->State() != Promise::kFulfilled) {
250245
env->ThrowError("linking error, dependency promises must be resolved on "

src/module_wrap.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include <map>
6+
#include <unordered_map>
77
#include <string>
88
#include <vector>
99
#include "node_url.h"
@@ -45,9 +45,7 @@ class ModuleWrap : public BaseObject {
4545
v8::Persistent<v8::Module> module_;
4646
v8::Persistent<v8::String> url_;
4747
bool linked_ = false;
48-
std::map<std::string, v8::Persistent<v8::Promise>*> resolve_cache_;
49-
50-
static std::map<int, std::vector<ModuleWrap*>*> module_map_;
48+
std::unordered_map<std::string, v8::Persistent<v8::Promise>> resolve_cache_;
5149
};
5250

5351
} // namespace loader

0 commit comments

Comments
 (0)