Skip to content

Commit

Permalink
feat: drop perIsolateCaches_ in favor of v8 data slots
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored and NathanWalker committed Nov 30, 2022
1 parent c5a8863 commit 44daeb3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 5 additions & 3 deletions NativeScript/runtime/Caches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Caches::~Caches() {
}

void Caches::Remove(v8::Isolate* isolate) {
Caches::perIsolateCaches_->Remove(isolate);
auto cache = isolate->GetData(0);
isolate->SetData(0, nullptr);
if (cache != nullptr) {
delete reinterpret_cast<std::shared_ptr<Caches>*>(cache);
}
}

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

std::shared_ptr<ConcurrentMap<Isolate*, std::shared_ptr<Caches>>> Caches::perIsolateCaches_ = std::make_shared<ConcurrentMap<Isolate*, std::shared_ptr<Caches>>>();

std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Caches::Metadata = std::make_shared<ConcurrentMap<std::string, const Meta*>>();
std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Caches::Workers = std::make_shared<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>>();

Expand Down
19 changes: 12 additions & 7 deletions NativeScript/runtime/Caches.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ class Caches {
static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
static std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Workers;

inline static std::shared_ptr<Caches> Init(v8::Isolate* isolate) {
auto cache = std::make_shared<Caches>(isolate);
// create a new shared_ptr that will live until Remove is called
isolate->SetData(0, static_cast<void*>(new std::shared_ptr<Caches>(cache)));
return cache;
}
inline static std::shared_ptr<Caches> Get(v8::Isolate* isolate) {
std::shared_ptr<Caches> cache = perIsolateCaches_->Get(isolate);
if (cache == nullptr) {
cache = std::make_shared<Caches>(isolate);
Caches::perIsolateCaches_->Insert(isolate, cache);
auto cache = isolate->GetData(0);
if (cache != nullptr) {
return *reinterpret_cast<std::shared_ptr<Caches>*>(cache);
}

return cache;
// this should only happen when an isolate is accessed after disposal
// so we return a dummy cache
return std::make_shared<Caches>(isolate);
}
static void Remove(v8::Isolate* isolate);

Expand Down Expand Up @@ -96,7 +102,6 @@ class Caches {
std::unique_ptr<v8::Persistent<v8::Function>> FunctionReferenceCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr);
std::unique_ptr<v8::Persistent<v8::Function>> UnmanagedTypeCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr);
private:
static std::shared_ptr<ConcurrentMap<v8::Isolate*, std::shared_ptr<Caches>>> perIsolateCaches_;
v8::Isolate* isolate_;
std::shared_ptr<v8::Persistent<v8::Context>> context_;
};
Expand Down
2 changes: 1 addition & 1 deletion NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}

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

Expand Down

0 comments on commit 44daeb3

Please sign in to comment.