Skip to content

perf: faster object arguments and receiver lookups - #2145

Open
edusperoni wants to merge 4 commits into
mainfrom
perf/object-arg-path
Open

edusperoni wants to merge 4 commits into
mainfrom
perf/object-arg-path

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

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. perf: faster object lifecycle and JNI calls #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

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.

…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.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The runtime now caches parsed JNI signatures, changes how it classifies Java wrappers during argument conversion, and updates Java object cache handling during weakening and extended-class registration. New tests cover repeated calls, constructor overloads, wrapper lifecycle cases, and nested extended-class construction.

Changes: Java Object Argument Handling

Layer / File(s) Summary
Parsed signatures for method and constructor calls
test-app/runtime/src/main/cpp/MethodCache.h, test-app/runtime/src/main/cpp/MethodCache.cpp, test-app/runtime/src/main/cpp/JsArgConverter.h, test-app/runtime/src/main/cpp/JsArgConverter.cpp, test-app/runtime/src/main/cpp/CallbackHandlers.h, test-app/runtime/src/main/cpp/CallbackHandlers.cpp, test-app/app/src/main/assets/app/mainpage.js
Method and constructor cache entries now store parsed JNI signatures. Argument converters accept and reuse parsed signatures, and callback paths pass the available signature metadata. The app test sequence loads the new test suite.
Java-wrapper argument conversion
test-app/runtime/src/main/cpp/MethodCache.cpp, test-app/runtime/src/main/cpp/JsArgConverter.cpp, test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp, test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp, test-app/runtime/src/main/cpp/MetadataNode.cpp, test-app/runtime/src/main/cpp/NumericCasts.h, test-app/app/src/main/assets/app/tests/testObjectArgumentPath.js
Conversion skips numeric-cast and null-sentinel lookups for objects with runtime wrapper metadata. Private-value access uses the current V8 context when available. Tests cover wrapper round-trips, released wrappers, casts, typed nulls, and warmed calls.
Java object cache and extended-class registration
test-app/runtime/src/main/cpp/LRUCache.h, test-app/runtime/src/main/cpp/ObjectManager.h, test-app/runtime/src/main/cpp/ObjectManager.cpp, test-app/runtime/src/main/cpp/MetadataNode.h, test-app/runtime/src/main/cpp/MetadataNode.cpp, test-app/runtime/src/main/cpp/CallbackHandlers.h, test-app/runtime/src/main/cpp/CallbackHandlers.cpp, test-app/app/src/main/java/com/tns/tests/SelfNesting.java, test-app/app/src/main/assets/app/tests/testObjectArgumentPath.js
Object lookup now supports disabling cache validation when Java holds the object strongly. Weakening state is set before Java batch calls. Extended-class registration reuses a resolved class and clears the cached Java object for the registered ID. Tests cover wrapper weakening, repeated construction, and nested construction.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Suggested reviewers: nathanwalker

Merge Risk: Minimal · up to 766e9

This PR speeds up passing objects from JavaScript to Java and looking up the Java object behind a wrapper. No concrete defect was found, and new tests cover repeated calls, constructor overloads, wrapper lifecycle and nested construction of extended classes. The remaining uncertainty is the usual one for runtime-level changes that depend on garbage-collection timing.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf: faster object arguments and receiver lookups" matches the main changes. The PR skips cast and null-marker reads for Java wrappers when converting object arguments, caches parsed JNI s…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "perf: faster object arguments and receiver lookups" matches the main changes. The PR skips cast and null-marker reads for Java wrappers when converting object arguments, caches parsed JNI signatures in MethodCache, caches resolved classes for extended-class construction, and skips jweak validity checks while Java holds the object strongly. The title names the object-argument and receiver-lookup work but does not mention the signature caching or the jweak change, so it covers the main theme without every detail, which is acceptable. It is a single, concise sentence with a conventional prefix and no file lists or vague terms.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 56c7b0b and 766e992.

Files selected for processing: 17

  • test-app/app/src/main/assets/app/mainpage.js
  • test-app/app/src/main/assets/app/tests/testObjectArgumentPath.js
  • test-app/app/src/main/java/com/tns/tests/SelfNesting.java
  • test-app/runtime/src/main/cpp/CallbackHandlers.cpp
  • test-app/runtime/src/main/cpp/CallbackHandlers.h
  • test-app/runtime/src/main/cpp/JsArgConverter.cpp
  • test-app/runtime/src/main/cpp/JsArgConverter.h
  • test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp
  • test-app/runtime/src/main/cpp/LRUCache.h
  • test-app/runtime/src/main/cpp/MetadataNode.cpp
  • test-app/runtime/src/main/cpp/MetadataNode.h
  • test-app/runtime/src/main/cpp/MethodCache.cpp
  • test-app/runtime/src/main/cpp/MethodCache.h
  • test-app/runtime/src/main/cpp/NumericCasts.h
  • test-app/runtime/src/main/cpp/ObjectManager.cpp
  • test-app/runtime/src/main/cpp/ObjectManager.h
  • test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

@edusperoni
edusperoni marked this pull request as ready for review November 21, 2026 20:33

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant