Repository navigation
Commit 2cb821d
authored
perf(runtime): drop three emulated thread_locals from hot paths (#2137)
### Description
The runtime's minSdk is below 29, so every `thread_local` it declares is
emulated: each access is a call to `__emutls_get_address`, and a
`thread_local` with a destructor also has to look up `__tls_guard` (or
its own init guard) first. This PR removes three of them from hot paths.
In each case only one thread touches the data: the thread holding the
isolate's `Locker`, or the event loop's home thread. So it can live on
the object instead.
1. **`MethodCache`'s signature key** (`static thread_local std::string
s_signatureKey`) becomes a per-`Runtime` scratch string, reached through
`Runtime::GetRuntime(args.GetIsolate())->GetSignatureKeyScratch()`. On a
cache hit, `ResolveMethodSignature` and `ResolveConstructorSignature`
each made three `__emutls_get_address` calls: guard, string, guard. That
cost was paid on every overload-resolved JS→Java call and every Java
construction from JS (e.g. `new java.lang.Object()`). The key is still
copied before resolving, because resolving calls into Java, which can
call back into JS and reuse the scratch.
2. **`SetDefaultOverrides`' field cache** (a function-local
`thread_local robin_hood::unordered_map<jclass, jfieldID>`) moves onto
the `Runtime`. This removes two lookups (the map and its init guard)
from every construction of an implementation with default-method
overrides, plus the per-thread TLS destructor registration.
3. **`EventLoop`'s looper callback depth** (`thread_local int
t_looperCallbackDepth`) becomes an `EventLoop` member. This removes one
lookup per eventfd/timerfd callback. The fd callbacks already receive
the `EventLoop` as their data pointer and only run on its home thread.
As a result, `IsInLooperCallback()` changes from a static to an instance
method that answers for that loop's home thread. It has no callers
today.
Why a per-`Runtime` value is safe: a runtime's JS, and so `MethodCache`
and `RegisterInstance`, only runs on the thread holding its isolate's
`Locker`, so two threads never share the scratch at once. Same-thread
re-entry (Java calling back into JS mid-resolve) is handled by the copy
noted in (1). Same-thread re-entry already existed with the
`thread_local`.
#### Emulated-TLS call sites
`bl __emutls_get_address` sites per control variable in the linked,
unstripped arm64-v8a `libNativeScript.so`
(`:runtime:externalNativeBuildRelease`, RelWithDebInfo, NDK 27.3):
| variable | before | after |
|---|---:|---:|
| `MethodCache` `s_signatureKey` | 8 | 0 |
| `__tls_guard` (all) | 14 | 2 (V8's) |
| `SetDefaultOverrides` map + its init guard | 4 | 0 |
| `EventLoop` `t_looperCallbackDepth` | 2 | 0 |
| whole library, all variables | 1627 | 1595 |
The four rows account for 26 of the 32 removed sites. The other 6 come
from code-generation and inlining differences elsewhere in the library,
which were not attributed to a variable. In the three changed objects
(`MethodCache.cpp.o`, `CallbackHandlers.cpp.o`, `EventLoop.cpp.o`), the
relocations go from 68 to 41 calls. That is 27 rather than 26 because
one `t_looperCallbackDepth` site was in `IsInLooperCallback()`, which
has no callers and is dropped at link. The only emutls variables left in
those objects are `JEnv::s_thread` and, in `EventLoop.cpp.o`,
`t_pumpDepth`.
No benchmark numbers: each removed lookup is a few ns, and the counts
above are the claim.
### Testing
Full suite (`runtestsAndVerifyResults`) on an arm64 API 36.1 emulator:
1773 specs, 0 failures.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Callback tracking and cached runtime data are now scoped to individual
runtime instances, improving consistency when multiple runtimes are
active.
* Callback status checks now report whether the specific event loop is
running a callback on its home thread.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 9c4895a commit 2cb821d
5 files changed
Lines changed: 46 additions & 28 deletions
File tree
- test-app/runtime/src/main/cpp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
155 | | - | |
| 154 | + | |
| 155 | + | |
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
273 | | - | |
| 273 | + | |
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
792 | 792 | | |
793 | 793 | | |
794 | 794 | | |
795 | | - | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | 795 | | |
800 | | - | |
801 | | - | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
802 | 799 | | |
803 | 800 | | |
804 | 801 | | |
805 | | - | |
806 | | - | |
807 | 802 | | |
808 | 803 | | |
809 | 804 | | |
| |||
853 | 848 | | |
854 | 849 | | |
855 | 850 | | |
856 | | - | |
| 851 | + | |
857 | 852 | | |
858 | 853 | | |
859 | 854 | | |
| |||
867 | 862 | | |
868 | 863 | | |
869 | 864 | | |
870 | | - | |
| 865 | + | |
871 | 866 | | |
872 | 867 | | |
873 | 868 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
| 202 | + | |
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
422 | 426 | | |
423 | 427 | | |
424 | 428 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | 45 | | |
48 | 46 | | |
49 | 47 | | |
50 | 48 | | |
51 | 49 | | |
52 | | - | |
| 50 | + | |
| 51 | + | |
53 | 52 | | |
54 | 53 | | |
55 | | - | |
| 54 | + | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | | - | |
61 | | - | |
| 59 | + | |
| 60 | + | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
| |||
91 | 90 | | |
92 | 91 | | |
93 | 92 | | |
94 | | - | |
| 93 | + | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
172 | 187 | | |
173 | 188 | | |
174 | 189 | | |
| |||
400 | 415 | | |
401 | 416 | | |
402 | 417 | | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
403 | 422 | | |
404 | 423 | | |
405 | 424 | | |
| |||
0 commit comments