Repository navigation
perf: faster object arguments and receiver lookups - #2145
edusperoni wants to merge 4 commits into
Conversation
…wrapper Converting an object argument read two private symbols from it: the numeric cast marker, then the null-node marker after the Java lookup. Both are only ever put on plain objects (cast objects, LongNumber instances and the .null sentinel), never on an object with the runtime's internal fields, so a Java wrapper or extended-class instance now goes straight to the Java lookup. Objects without internal fields (TypeScript subclass instances, typed arrays, plain objects) keep the full path. The same applies to the overload signature key and to argument arrays built for overload resolution. The Local<Private> helpers now use the current context instead of walking to the object's creation context: a private-symbol access is an own lookup that skips interceptors, proxy traps and access checks, so it runs no JS and does not depend on the context. The creation context is still the fallback when no context is entered.
A call resolved through MethodCache (constructors with arguments, overloaded methods) parsed the JNI signature into a vector of type strings on every call before converting its arguments. The tokens are now parsed once, when the entry is created, and stored on it before it is published to the cache; the entry is immutable afterwards, so readers need no extra locking.
Every `new` of a JS-extended class looked its generated Java 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.
Every cache hit in GetJavaObjectByJsObject asked JNI whether the cached weak global ref was cleared (IsSameObject, a thread-state transition). While a linked wrapper's Java side has not been weakened, Java's ObjectTable holds the object strongly under its id, so the jweak cached for that id cannot have been cleared and the check is skipped. Wrappers whose Java side was weakened (and may have been handed back to JS since) keep it. Two cases broke that invariant and are closed here: - A finalizer batch that failed part way could leave objects weakened on the Java side with javaWeakened still false. The flag is now set before the call, so a failure leaves it conservative. - While JS constructs an object, an instance of the same class that Java creates inside the constructor takes the reserved id until registerConstructedInstance replaces it. A jweak cached in that window named the nested instance, so calls on the constructed object reached the nested one (and, once it was collected, relied on the jweak check). The cached entry is now dropped after registration.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
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),LongNumberinstances (no internal fields) and the.nullsentinel (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 ofV8GetPrivateValue/V8SetPrivateValuenow 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 bypassACCESS_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 avector<string>before converting its arguments. The tokens are now parsed once, stored on theCacheMethodInfobefore 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
newof 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::GetJavaObjectByJsObjectranValidateWeakGlobalRefCallback, which callsIsSameObject(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 hasjavaWeakened == 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:
Runtime.initInstancetookcurrentObjectIdwithout 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. perf: faster object lifecycle and JNI calls #2099 later gated the id by class and made it single-use.Invariant on main. If a linked state has
!javaWeakened, Java'sObjectTableholds that id's object strongly, and that object is the one the cached jweak names:ObjectTableis weakened only byprocessFinalizedBatch→weakenAndCheckAlive.javaWeakened.remove()path either clearslinkedor deletes the state.Two cases broke the invariant and are closed in the same commit:
WeakRefcould leave objects weak in Java withjavaWeakenedstill false. The flag is now set before the Java call.super(), before the outer'sinitInstance. That instance takes the reserved id untilregisterConstructedInstancereplaces 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.RegisterInstancenow drops the cached entry after registration. The newSelfNestingspec covers this. With B1 but without that eviction, the spec aborts withJNI 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:main(56c7b0b);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).
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:
Stable rows: the A/A gap is at most 2.4%, and every label's launches fall in a narrow band.
takeObjectby 24% andtakeObjectsby 36%.Math.maxcall by 19%.instance.noopby 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:The medians therefore reflect which cluster each launch fell into, not the code.
Related Pull Requests
MethodCache.cppandCallbackHandlers.cpptoo. This branch merges with it cleanly.Does your pull request have unit tests?
Yes, see Testing.
Testing
testObjectArgumentPath.js:long/char/double/floatcasts, typed nulls and wrappers through a warm overload cache;SelfNestingfixture.enableMultithreadedJavascripton, and 1799, 0 failures with it off.