Skip to content

Commit d521995

Browse files
committed
perf: resolve the calling thread's own runtime without the global lock
Every Java -> JS call (and lock/unlock) resolved its runtime id through GetRuntime(int), which takes the process-wide s_runtimeCacheMutex, so workers and multithreaded JS serialized on one lock. A thread's own runtime is now matched against a thread_local id first; other threads keep the locked map. That relies on two invariants that are now checked: a thread hosts at most one runtime (as com.tns.Runtime's constructor already enforces in Java), and a runtime is only destroyed on the thread that created it.
1 parent af0d2cc commit d521995

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎test-app/runtime/src/main/cpp/Runtime.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,15 @@ int Runtime::GetAndroidVersion() {
121121

122122
Runtime::Runtime(JNIEnv* env, jobject runtime, int id)
123123
: m_id(id),
124+
m_ownerThread(std::this_thread::get_id()),
124125
m_isolate(nullptr),
125126
m_lastUsedMemory(0),
126127
m_gcFunc(nullptr),
127128
m_runGC(false) {
129+
// GetRuntime(int) and the thread's pinned JNIEnv assume one runtime per
130+
// thread; com.tns.Runtime's constructor enforces the same on the Java side.
131+
NS_CHECK(s_currentRuntime == nullptr);
132+
128133
m_runtime = env->NewGlobalRef(runtime);
129134
m_objectManager = new ObjectManager(m_runtime);
130135
m_state = std::make_unique<RuntimeState>();
@@ -144,6 +149,12 @@ Runtime::Runtime(JNIEnv* env, jobject runtime, int id)
144149
}
145150

146151
Runtime* Runtime::GetRuntime(int runtimeId) {
152+
// A runtime is only destroyed on its own thread, which is busy in this call,
153+
// so the calling thread's own runtime needs neither the lock nor the map.
154+
if (runtimeId == s_currentRuntimeId) {
155+
return s_currentRuntime;
156+
}
157+
147158
Runtime* runtime = nullptr;
148159
{
149160
std::lock_guard<std::mutex> lock(s_runtimeCacheMutex);
@@ -302,6 +313,12 @@ void Runtime::Init(JNIEnv* env, jstring filesPath, jstring nativeLibDir,
302313
}
303314

304315
Runtime::~Runtime() {
316+
NS_CHECK(std::this_thread::get_id() == m_ownerThread);
317+
if (s_currentRuntime == this) {
318+
s_currentRuntime = nullptr;
319+
s_currentRuntimeId = -1;
320+
}
321+
305322
/*
306323
* Idempotent backstop for the erase DestroyRuntime does: a bootstrap that
307324
* failed before PrepareV8Runtime ran never reached it. The isolate entry is
@@ -1099,6 +1116,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath,
10991116

11001117
this->m_napiEnv = NapiEnv::Create(context, m_eventLoop);
11011118
s_currentRuntime = this;
1119+
s_currentRuntimeId = m_id;
11021120
// The runtime's thread stays attached until DestroyRuntime: the main thread
11031121
// for the process, a worker thread until after it tears its runtime down.
11041122
JEnv::PinThreadEnv(JEnv());
@@ -1140,6 +1158,7 @@ double Runtime::PerformanceNowMillis() {
11401158
}
11411159

11421160
void Runtime::DestroyRuntime() {
1161+
NS_CHECK(std::this_thread::get_id() == m_ownerThread);
11431162
CrashBreadcrumbs::UnregisterRuntime(m_id);
11441163
{
11451164
std::lock_guard<std::mutex> lock(s_runtimeCacheMutex);
@@ -1178,6 +1197,7 @@ void Runtime::DestroyRuntime() {
11781197
}
11791198
if (s_currentRuntime == this) {
11801199
s_currentRuntime = nullptr;
1200+
s_currentRuntimeId = -1;
11811201
JEnv::UnpinThreadEnv();
11821202
}
11831203
// The events state holds v8::Global handles (backing event target, dispatch
@@ -1275,6 +1295,7 @@ int Runtime::m_androidVersion = Runtime::GetAndroidVersion();
12751295
std::shared_ptr<EventLoop> Runtime::s_mainEventLoop;
12761296

12771297
thread_local Runtime* Runtime::s_currentRuntime = nullptr;
1298+
thread_local int Runtime::s_currentRuntimeId = -1;
12781299
thread_local PendingIsolateSetup Runtime::s_pendingIsolateSetup;
12791300

12801301
void Runtime::SetPendingIsolateSetup(PendingIsolateSetup setup) {

‎test-app/runtime/src/main/cpp/Runtime.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RUNTIME_H_
33

44
#include "v8.h"
5+
#include <thread>
56
#include "JniLocalRef.h"
67
#include "ObjectManager.h"
78
#include "SimpleAllocator.h"
@@ -103,9 +104,8 @@ class Runtime {
103104

104105
/*
105106
* The runtime whose home thread is the calling thread, or null. Set at
106-
* the end of PrepareV8Runtime; may be stale after a Runtime destroyed
107-
* on another thread, so consumers must validate through
108-
* GetNapiEnvIfAlive rather than dereference it.
107+
* the end of PrepareV8Runtime and cleared when it is torn down; a
108+
* thread hosts at most one runtime and only that thread may destroy it.
109109
*/
110110
static Runtime* GetCurrentRuntime() {
111111
return s_currentRuntime;
@@ -317,6 +317,8 @@ class Runtime {
317317
Runtime(JNIEnv* env, jobject runtime, int id);
318318

319319
int m_id;
320+
321+
std::thread::id m_ownerThread;
320322
jobject m_runtime;
321323
v8::Isolate* m_isolate;
322324

@@ -425,6 +427,7 @@ class Runtime {
425427
static std::shared_ptr<EventLoop> s_mainEventLoop;
426428

427429
static thread_local Runtime* s_currentRuntime;
430+
static thread_local int s_currentRuntimeId;
428431
static thread_local PendingIsolateSetup s_pendingIsolateSetup;
429432

430433
#ifdef APPLICATION_IN_DEBUG

0 commit comments

Comments
 (0)