Repository navigation
perf(runtime): cheaper runtime and RuntimeState lookups - #2146
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 20 minutes. View limit details
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Every V8 callback that looks up its runtime paid an out-of-line call for what is a single isolate data-slot load, and every Java -> JS call did the same for a thread-local read. Only the locked map lookups and the throws stay out of line. GetRuntimeFromIsolateData had no callers left.
Slot indices are claimed while the library loads instead of behind a function-local static's guard, and the slots are a fixed-size array, so a lookup of existing state is an inlined index and null check: no vector bounds check, and no separate disposed check, since teardown empties every slot first. Creation stays out of line, and callers that already hold the Runtime can skip the isolate data-slot read.
7e43b8d to
5a634ee
Compare
Description
Makes the runtime lookups on the JS ↔ Java bridge cheaper:
Runtime::GetRuntimeandRuntimeState::For.perf(runtime): inline the runtime lookups' fast pathsGetRuntime(v8::Isolate*)is now inline. Its fast path is the isolate data-slot read thatTryGetRuntimealready did inline. Only the locked setup-window map lookup and the throw stay out of line, inGetRuntimeSlow. Before this, every V8 callback that looks up its runtime paid a call for one load.GetRuntime(int), used on every Java → JS call, is now inline in the same way: the calling thread's own runtime is a thread-local read.s_currentis now declaredconstinit. Without it, other translation units check the thread-local's init hook (_ZTH…, a GOT load and a branch) before__emutls_get_address.GetRuntimeFromIsolateDatahad no callers left after perf(runtime): hot-path quick wins on the JNI bridge #2141, so it is removed.perf(runtime): make RuntimeState lookups cheaperFor<T>call checked a function-local static's guard (__cxa_guardbyte).std::vector<unique_ptr<void, deleter>>. A lookup of existing state is now an inlined index and null check. The vector data-pointer load, the size check and thedisposed_check are gone from the fast path. Going past 32 state types fails anNS_CHECKwhen the library loads, in every build type.Clear()empties every slot before it destroys anything, then destroys in reverse index order. That keeps the semanticsmainhas: from the start of teardown,Forreturns null, its own type included, and never recreates state.For<T>(Runtime*)overload, which skips the isolate data-slot read. It is used byGetCallbackMethodName, which runs on every Java → JS call from aRuntimemember.Each subsystem still declares its state struct in its own
.cpp. Nothing lists the state types centrally.Generated code (arm64,
MetadataNode::GetMetadataNodeCache)mainldr state; cbz; bl RuntimeState::GetOrCreate<T>: out of line, with guard byte,disposed_check, vector grow loop and bounds checkldr state; cbz; adrp/ldr index; ldrb disposed; tbnz; ldp begin,end; sub; cmp; b.hs; lsl; ldr slot; cbzldr state; cbz; adrp/ldr index; ldr [state, index, lsl #3]; cbnzThe
kSlotIndex<T>load is a directadrp+ldr, not GOT-indirect. SinceJNI TryGetRuntime(int)now inlinesGetRuntime(int), it is just__emutls_get_address, a compare and a return. The shippedlibNativeScript.sois the same size (24,528,560 bytes) onmainand on this branch.Options evaluated and not taken
callback.noArgs, (b) ran 7-10 fewer instructions per op than (a) in both stat sessions (one onmainbefore perf(runtime): hot-path quick wins on the JNI bridge #2141): −44.7 vs −37.6 here, against an A/A difference of 0.6. The wrapper row went the other way (−35.9 vs −45.2), but its count swings with GC: in the earlier session it moved by about 800 instructions/op between identical runs.RuntimeStateinline inRuntimeinstead of behind aunique_ptr. That would remove one more dependent load (runtime → state). ButRuntime.hwould need the fullRuntimeStatedefinition, whileForneeds the completeRuntime, so the headers would have to be split or reordered. The saving is one L1 load, at most once per call, on rows that take 700 ns or more. That is far below what this phone can resolve.RuntimeStateexists to avoid. All they would remove is theadrp/ldrof the index, which is off the critical path: it issues in parallel with the state-pointer load.Measurements
Samsung SM-G780G (Android 13), optimized release APKs (
-Poptimized -Prelease -Pabis=arm64-v8a), #2092 harness.baseismainat 56c7b0b, andbase2is the same APK under a second label, as the A/A pair. Every label was reinstalled and speed-compiled every round, the first launch after the install was discarded, and only the relaunch was measured. There were six rounds with the order rotated. Ratios are per-round label/base; the table shows the median, with the min..max range in parentheses.Time (ns/op, median of rounds)
All 22 rows the run measured are listed. Wall-clock times can't support any claim about the object-creation, GC-release and callback rows: their A/A ranges span 10-100%, so the large ratios on
new java.lang.Object()and its lifecycle row are noise, not a result. On this phonecallback.noArgs()is bimodal per launch (about 730 or about 1950 ns), and its twoInts reference doesn't track it. The instruction counts below cover those paths.simpleperf staton the JS thread (two-scale difference, so per op of the row;instructions:uandcpu-cycles:u, no multiplexing)Commit 1 accounts for the per-call
GetRuntimesavings on every JS → Java row. Commit 2 adds about 23 instructions/op on Java → JS callbacks (CallbackMethodNames) and about 22 on wrapper creation (MetadataNodeCache), the two paths that callRuntimeState::Foronce per op. Onmain, a plain JS → Java call does not callRuntimeState::For.No row is slower than
basebeyond the A/A noise. With commit 1 alone,lifecycle new java.lang.Object() [release: V8 gc]reads 1.031 against A/A 1.003, and[release: Java gc]reads 1.013 against 0.997. These are GC-release rows whose ranges overlap the A/A ones, and the full PR is back at A/A on both.Related Pull Requests
git merge-tree). It keeps its two hot caches asRuntimemembers, and that is unchanged here.Does your pull request have unit tests?
No new specs: the behavior is unchanged. The full suite passes on an API 36.1 arm64 emulator (1788 specs, 0 failures). Teardown semantics are the existing ones:
Forreturns null onceClearhas started, and never recreates state.Summary by CodeRabbit