Skip to content

Commit

Permalink
perf: Speed up Dispatcher get calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jun 23, 2024
1 parent f4ad208 commit a6b69b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@ using namespace facebook;

static constexpr auto GLOBAL_DISPATCHER_HOLDER_NAME = "__nitroDispatcher";

std::unordered_map<jsi::Runtime*, std::weak_ptr<Dispatcher>> Dispatcher::_globalCache;

void Dispatcher::installRuntimeGlobalDispatcher(jsi::Runtime& runtime, std::shared_ptr<Dispatcher> dispatcher) {
Logger::log(TAG, "Installing global Dispatcher Holder...");

// Store a weak reference in global cache
_globalCache[&runtime] = std::weak_ptr(dispatcher);

// Inject the dispatcher into Runtime global (runtime will hold a strong reference)
jsi::Object dispatcherHolder(runtime);
dispatcherHolder.setNativeState(runtime, dispatcher);
runtime.global().setProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME, dispatcherHolder);
}

std::shared_ptr<Dispatcher> Dispatcher::getRuntimeGlobalDispatcher(jsi::Runtime& runtime) {
if (_globalCache.contains(&runtime)) [[likely]] {
// the runtime is known - we have something in cache
std::weak_ptr<Dispatcher> weakDispatcher = _globalCache[&runtime];
std::shared_ptr<Dispatcher> strongDispatcher = weakDispatcher.lock();
if (strongDispatcher) {
// the weak reference we cached is still valid - return it!
return strongDispatcher;
}
}

jsi::Value dispatcherHolderValue = getRuntimeGlobalDispatcherHolder(runtime);
jsi::Object dispatcherHolder = dispatcherHolderValue.getObject(runtime);
return dispatcherHolder.getNativeState<Dispatcher>(runtime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Dispatcher : public jsi::NativeState {
// 3. Return an open future that gets resolved later by the dispatcher Thread
return future;
}

private:
static std::unordered_map<jsi::Runtime*, std::weak_ptr<Dispatcher>> _globalCache;

private:
static constexpr auto TAG = "Dispatcher";
Expand Down

0 comments on commit a6b69b3

Please sign in to comment.