Repository navigation
perf(runtime): drop three emulated thread_locals from hot paths - #2137
Draft
edusperoni wants to merge 3 commits into
Draft
edusperoni wants to merge 3 commits into
edusperoni wants to merge 3 commits into
Conversation
MethodCache encoded every overload-resolved call's and constructor's cache key into a `static thread_local std::string`. The runtime's minSdk is below 29, so that thread_local is emulated: every access goes through __emutls_get_address, and because the string has a destructor, through a second lookup of the TU's __tls_guard as well. A cache hit made three such calls, on every overload-resolved JS->Java call and every Java construction from JS (e.g. `new java.lang.Object()`). The key is only ever built by the thread holding the isolate's Locker, so a scratch string owned by the Runtime serves the same purpose without TLS. The key is still copied before resolving, because resolving calls into Java, which can call back into JS and reuse the scratch.
SetDefaultOverrides cached each class's defaultOverrides__ns field in a function-local thread_local map. Below API 29 thread_locals are emulated, so every construction of an implementation with default-method overrides paid two __emutls_get_address lookups (the map and its thread_local initialization guard), and each thread that touched it registered a TLS destructor. Only the thread holding the isolate's Locker constructs instances, so the cache moves onto the Runtime.
Every eventfd/timerfd ALooper callback incremented and decremented a thread_local depth counter. Below API 29 that thread_local is emulated, so each callback paid an __emutls_get_address lookup. The fd callbacks only run on the loop's home thread and already receive the EventLoop as their data pointer, so the depth becomes an EventLoop member and IsInLooperCallback() an instance method answering for that loop's home thread. It currently has no callers.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The runtime's minSdk is below 29, so every
thread_localit declares is emulated: each access is a call to__emutls_get_address, and athread_localwith 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'sLocker, or the event loop's home thread. So it can live on the object instead.MethodCache's signature key (static thread_local std::string s_signatureKey) becomes a per-Runtimescratch string, reached throughRuntime::GetRuntime(args.GetIsolate())->GetSignatureKeyScratch(). On a cache hit,ResolveMethodSignatureandResolveConstructorSignatureeach made three__emutls_get_addresscalls: 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.SetDefaultOverrides' field cache (a function-localthread_local robin_hood::unordered_map<jclass, jfieldID>) moves onto theRuntime. 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.EventLoop's looper callback depth (thread_local int t_looperCallbackDepth) becomes anEventLoopmember. This removes one lookup per eventfd/timerfd callback. The fd callbacks already receive theEventLoopas 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-
Runtimevalue is safe: a runtime's JS, and soMethodCacheandRegisterInstance, only runs on the thread holding its isolate'sLocker, 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 thethread_local.Emulated-TLS call sites
bl __emutls_get_addresssites per control variable in the linked, unstripped arm64-v8alibNativeScript.so(:runtime:externalNativeBuildRelease, RelWithDebInfo, NDK 27.3):MethodCaches_signatureKey__tls_guard(all)SetDefaultOverridesmap + its init guardEventLoopt_looperCallbackDepthThe 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 onet_looperCallbackDepthsite was inIsInLooperCallback(), which has no callers and is dropped at link. The only emutls variables left in those objects areJEnv::s_threadand, inEventLoop.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.