Skip to content

Commit 44daeb3

Browse files
edusperoniNathanWalker
authored andcommitted
feat: drop perIsolateCaches_ in favor of v8 data slots
1 parent c5a8863 commit 44daeb3

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

NativeScript/runtime/Caches.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ Caches::~Caches() {
2424
}
2525

2626
void Caches::Remove(v8::Isolate* isolate) {
27-
Caches::perIsolateCaches_->Remove(isolate);
27+
auto cache = isolate->GetData(0);
28+
isolate->SetData(0, nullptr);
29+
if (cache != nullptr) {
30+
delete reinterpret_cast<std::shared_ptr<Caches>*>(cache);
31+
}
2832
}
2933

3034
void Caches::SetContext(Local<Context> context) {
@@ -35,8 +39,6 @@ Local<Context> Caches::GetContext() {
3539
return this->context_->Get(this->isolate_);
3640
}
3741

38-
std::shared_ptr<ConcurrentMap<Isolate*, std::shared_ptr<Caches>>> Caches::perIsolateCaches_ = std::make_shared<ConcurrentMap<Isolate*, std::shared_ptr<Caches>>>();
39-
4042
std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Caches::Metadata = std::make_shared<ConcurrentMap<std::string, const Meta*>>();
4143
std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Caches::Workers = std::make_shared<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>>();
4244

NativeScript/runtime/Caches.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ class Caches {
5252
static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
5353
static std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Workers;
5454

55+
inline static std::shared_ptr<Caches> Init(v8::Isolate* isolate) {
56+
auto cache = std::make_shared<Caches>(isolate);
57+
// create a new shared_ptr that will live until Remove is called
58+
isolate->SetData(0, static_cast<void*>(new std::shared_ptr<Caches>(cache)));
59+
return cache;
60+
}
5561
inline static std::shared_ptr<Caches> Get(v8::Isolate* isolate) {
56-
std::shared_ptr<Caches> cache = perIsolateCaches_->Get(isolate);
57-
if (cache == nullptr) {
58-
cache = std::make_shared<Caches>(isolate);
59-
Caches::perIsolateCaches_->Insert(isolate, cache);
62+
auto cache = isolate->GetData(0);
63+
if (cache != nullptr) {
64+
return *reinterpret_cast<std::shared_ptr<Caches>*>(cache);
6065
}
61-
62-
return cache;
66+
// this should only happen when an isolate is accessed after disposal
67+
// so we return a dummy cache
68+
return std::make_shared<Caches>(isolate);
6369
}
6470
static void Remove(v8::Isolate* isolate);
6571

@@ -96,7 +102,6 @@ class Caches {
96102
std::unique_ptr<v8::Persistent<v8::Function>> FunctionReferenceCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr);
97103
std::unique_ptr<v8::Persistent<v8::Function>> UnmanagedTypeCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr);
98104
private:
99-
static std::shared_ptr<ConcurrentMap<v8::Isolate*, std::shared_ptr<Caches>>> perIsolateCaches_;
100105
v8::Isolate* isolate_;
101106
std::shared_ptr<v8::Persistent<v8::Context>> context_;
102107
};

NativeScript/runtime/Runtime.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
}
8080

8181
void Runtime::Init(Isolate* isolate) {
82-
std::shared_ptr<Caches> cache = Caches::Get(isolate);
82+
std::shared_ptr<Caches> cache = Caches::Init(isolate);
8383
cache->ObjectCtorInitializer = MetadataBuilder::GetOrCreateConstructorFunctionTemplate;
8484
cache->StructCtorInitializer = MetadataBuilder::GetOrCreateStructCtorFunction;
8585

0 commit comments

Comments
 (0)