Repository navigation
Commit 9c4895a
authored
perf: faster object arguments and receiver lookups (#2145)
### Description
Four changes to the JS→Java call path that cost something on every
object argument, receiver lookup, overloaded call or extended-class
construction. One commit each.
#### 1. Skip cast and null markers when an argument is a Java wrapper
Converting an object argument read two private symbols from it: the
numeric cast marker, then the null-node marker after the Java lookup.
Each read also walked to the object's creation context. Both markers are
only ever put on plain objects: the `long()`/`byte()`/… cast objects
(`Object::New`), `LongNumber` instances (no internal fields) and the
`.null` sentinel (`Object::New`). So an argument with the runtime's
internal fields (a Java wrapper or an extended-class instance) now goes
straight to the Java lookup. Objects without internal fields keep the
full path: TypeScript subclass instances, typed arrays, plain objects
and Proxies. The same skip applies to the overload signature key
(`MethodCache::AppendType`) and to the argument arrays built for
overload resolution.
The `Local<Private>` overloads of
`V8GetPrivateValue`/`V8SetPrivateValue` now use the current context
instead of the creation context. A private-symbol access is an own
lookup that skips interceptors, proxy traps and access checks
(`LookupIterator::ComputeConfiguration` → `OWN_SKIP_INTERCEPTOR`, and
private names bypass `ACCESS_CHECK`). It runs no JS and does not depend
on the context. The creation context remains the fallback when no
context is entered.
#### 2. Keep parsed signatures on resolved method cache entries
Constructors with arguments and overloaded methods resolve through
`MethodCache`. Each such call parsed the JNI signature into a
`vector<string>` before converting its arguments. The tokens are now
parsed once, stored on the `CacheMethodInfo` before it is published, and
immutable afterwards, so no locking changes. Independent of #2137, which
touches the signature-key scratch in the same file; the two merge
cleanly.
#### 3. Cache the resolved class of a JS-extended class
Every `new` of a JS-extended class looked its generated class up in the
process-wide class cache, taking its shared lock and hashing the full
class name. The extended class's per-isolate callback data now keeps the
global ref once resolved. The class cache never drops or replaces an
entry, so the ref stays valid. Plain Java class construction is
unchanged.
#### 4. Skip the jweak check while Java holds the object strongly
Each LRU cache hit in `ObjectManager::GetJavaObjectByJsObject` ran
`ValidateWeakGlobalRefCallback`, which calls `IsSameObject(jweak,
NULL)`: a JNI call with a thread-state transition. That cost lands on
every instance call (receiver) and every object argument. It is now
skipped while the wrapper's link has `javaWeakened == false`. Wrappers
whose Java side was weakened keep the check.
**Why the check exists (#1881).** #1881 added the validator because
cached jweaks read null while the object "exists". That PR had no repro.
Reading the code at the time shows two causes, neither an ART bug:
- **Id rebinding during construction.** `Runtime.initInstance` took
`currentObjectId` without clearing it or checking the class, and the id
stayed published for the whole Java constructor. A binding instance that
Java created inside a JS-driven constructor (a custom view inflated by
it, an instance made by reflection) registered under the outer object's
id. A lookup in that window cached a jweak to the nested instance. When
the outer registration replaced it and the nested instance was
collected, the jweak read null while the table held the live outer
object. Re-fetching then "fixed" it. #2099 later gated the id by class
and made it single-use.
- **A weak Java side behind a live wrapper.** The old finalizer weakened
the Java side synchronously and resurrected the wrapper. A wrapper
handed back to JS later was never re-strengthened, so the jweak could be
legitimately cleared while JS held the wrapper. For these the validator
turns a null into a "cleared object reference" error.
**Invariant on main.** If a linked state has `!javaWeakened`, Java's
`ObjectTable` holds that id's object strongly, and that object is the
one the cached jweak names:
- `ObjectTable` is weakened only by `processFinalizedBatch` →
`weakenAndCheckAlive`.
- A wrapper reacquired while queued is dropped from the batch. A wrapper
reacquired after weakening keeps `javaWeakened`.
- Each `remove()` path either clears `linked` or deletes the state.
- Ids are never reused, and every runtime has its own table and cache.
Two cases broke the invariant and are closed in the same commit:
- **A finalizer batch that failed part way.** Example: an OOM allocating
a `WeakRef` could leave objects weak in Java with `javaWeakened` still
false. The flag is now set before the Java call.
- **Same-class nested construction, still possible on main.** Java can
create an instance of exactly the class under construction during the
outer constructor's `super()`, before the outer's `initInstance`. That
instance takes the reserved id until `registerConstructedInstance`
replaces it, and a jweak cached in that window names it. On main, calls
on the constructed object reach the nested instance until it is
collected, and then the validator re-fetches. `RegisterInstance` now
drops the cached entry after registration. The new `SelfNesting` spec
covers this. With B1 but without that eviction, the spec aborts with
`JNI DETECTED ERROR IN APPLICATION: can't call int
com.tns.tests.SelfNesting.getTag() on null object`.
### Measurements
Release APKs (`-Poptimized -Prelease`, arm64) run on a phone through the
#2092 harness (`debug.ns.jnibench`). The labels are cumulative:
- base is `main` (56c7b0b);
- then +B2, +B3, +B4 and +B1, one commit each.
Every round reinstalls and speed-compiles each label. The first launch
after an install is discarded and only the relaunch is measured. Each
round runs base, +B2, +B3, +B4, +B1, then base2, which is the base APK
again (A/A).
6 rounds. ns per call: median over launches of each launch's median
(min..max over launches).
| row | base | base2 (A/A) | +B2 | +B3 | +B4 | +B1 | A/A | B2 | B3 | B4
| B1 | total |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| instance.noop() | 229 (229..238) | 230 (228..231) | 230 (230..232) |
231 (229..232) | 231 (230..235) | 197 (194..200) | +0.1% | +0.4% | +0.1%
| +0.1% | -14.6% | -14.1% |
| instance.takeObject(Object) | 487 (482..490) | 487 (481..490) | 372
(368..374) | 368 (365..370) | 373 (369..374) | 306 (305..310) | -0.1% |
-23.7% | -1.1% | +1.5% | -18.0% | -37.2% |
| instance.takeObjects(Object x3) | 1046 (1035..1050) | 1048
(1043..1058) | 669 (665..700) | 663 (658..668) | 675 (668..684) | 548
(545..550) | +0.1% | -36.0% | -0.9% | +1.7% | -18.7% | -47.6% |
| overloaded static Math.max(int,int) | 663 (659..666) | 667 (658..675)
| 679 (662..692) | 551 (548..560) | 544 (539..547) | 537 (536..542) |
+0.6% | +2.4% | -18.9% | -1.3% | -1.2% | -19.0% |
| new Object.extend({})() | 3700 (3551..3901) | 3788 (3414..3856) | 3638
(3550..3752) | 3777 (3596..4015) | 3573 (3339..3675) | 3619 (3370..3717)
| +2.4% | -1.7% | +3.8% | -5.4% | +1.3% | -2.2% |
| static.noop() | 89 (88..89) | 89 (89..92) | 90 (89..90) | 88 (88..89)
| 88 (88..89) | 89 (89..90) | +0.8% | +1.4% | -1.9% | +0.2% | +0.7% |
+0.3% |
Rows that are bimodal per launch on this phone. No deltas are given
because the A/A pair alone differs by more than any step:
| row | base | base2 (A/A) | +B1 (all four) | A/A gap |
|---|---|---|---|---|
| new java.lang.Object() | 1598 (1413..2782) | 1816 (1526..2112) | 1771
(1407..2855) | +13.6%: inconclusive |
| new java.lang.StringBuilder(String) | 2047 (1886..6246) | 2868
(2403..4278) | 3162 (1760..3808) | +40.1%: inconclusive |
| overloaded static String.valueOf(Object) | 2378 (2362..4163) | 3165
(2349..3913) | 2042 (1798..2231) | +33.1%: inconclusive |
| field.get int | 158 (157..257) | 207 (157..257) | 140 (121..198) |
+31.5%: inconclusive |
- **Stable rows:** the A/A gap is at most 2.4%, and every label's
launches fall in a narrow band.
- B2 cuts `takeObject` by 24% and `takeObjects` by 36%.
- B3 cuts the overloaded `Math.max` call by 19%.
- B1 cuts `instance.noop` by 15% and the object-argument rows by 18-19%.
- **B4:** `new Object.extend({})()` moves -5.4% at the B4 step, against
a 2.4% A/A gap and ±4% at the other steps. That is suggestive, not
conclusive.
- **Bimodal rows:** each launch of these lands in one of two clusters.
On `new java.lang.StringBuilder(String)` the clusters sit near 1.8-2.6
µs and 3.1-4.3 µs:
- base had 1 of 6 launches in the slow cluster;
- base2, the same APK, had 3 of 6;
- +B1 had 4 of 6, and its fast launches (1760 and 1784 ns) are the
fastest of any label.
The medians therefore reflect which cluster each launch fell into, not
the code.
### Related Pull Requests
- #1881 added the jweak check that item 4 now skips for strongly held
objects.
- #2099 fixed the construction id reuse that caused most of #1881's null
jweaks.
- #2137 (open) touches `MethodCache.cpp` and `CallbackHandlers.cpp` too.
This branch merges with it cleanly.
### Does your pull request have unit tests?
Yes, see Testing.
### Testing
- New `testObjectArgumentPath.js`:
- wrapper, extended-class and TypeScript-subclass instances as object
arguments, repeated;
- a released wrapper as an argument;
- `long`/`char`/`double`/`float` casts, typed nulls and wrappers through
a warm overload cache;
- a cast through a single-signature method;
- constructor overloads on a warm cache;
- repeated and interleaved extended-class construction, with and without
arguments;
- a wrapper reacquired after its Java side was weakened and then
collected, which must throw rather than pass null;
- a reachable wrapper after warm instance calls and a collection;
- the same-class nested construction above, with a new `SelfNesting`
fixture.
- Full suite on an arm64 API 36.1 emulator, rebased on 56c7b0b: **1799
specs, 0 failures** with `enableMultithreadedJavascript` on, and **1799,
0 failures** with it off.1 parent d5b32f8 commit 9c4895a
17 files changed
Lines changed: 329 additions & 73 deletions
File tree
- test-app
- app/src/main
- assets/app
- tests
- java/com/tns/tests
- runtime/src/main/cpp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
| 217 | + | |
| 218 | + | |
218 | 219 | | |
219 | 220 | | |
220 | 221 | | |
| |||
230 | 231 | | |
231 | 232 | | |
232 | 233 | | |
233 | | - | |
234 | | - | |
235 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
236 | 243 | | |
237 | 244 | | |
238 | 245 | | |
| |||
253 | 260 | | |
254 | 261 | | |
255 | 262 | | |
256 | | - | |
| 263 | + | |
257 | 264 | | |
258 | 265 | | |
259 | 266 | | |
| |||
268 | 275 | | |
269 | 276 | | |
270 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
271 | 281 | | |
272 | 282 | | |
273 | 283 | | |
| |||
505 | 515 | | |
506 | 516 | | |
507 | 517 | | |
| 518 | + | |
508 | 519 | | |
509 | | - | |
510 | | - | |
| 520 | + | |
| 521 | + | |
511 | 522 | | |
512 | 523 | | |
513 | 524 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
92 | 108 | | |
93 | 109 | | |
94 | | - | |
| 110 | + | |
| 111 | + | |
95 | 112 | | |
96 | 113 | | |
97 | 114 | | |
98 | 115 | | |
99 | 116 | | |
100 | 117 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 118 | + | |
111 | 119 | | |
112 | 120 | | |
113 | 121 | | |
| |||
127 | 135 | | |
128 | 136 | | |
129 | 137 | | |
130 | | - | |
| 138 | + | |
| 139 | + | |
131 | 140 | | |
132 | 141 | | |
133 | 142 | | |
134 | 143 | | |
135 | 144 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
| 145 | + | |
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | 163 | | |
169 | 164 | | |
170 | 165 | | |
| |||
321 | 316 | | |
322 | 317 | | |
323 | 318 | | |
324 | | - | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
325 | 323 | | |
326 | 324 | | |
327 | 325 | | |
| |||
419 | 417 | | |
420 | 418 | | |
421 | 419 | | |
422 | | - | |
423 | | - | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
| |||
0 commit comments