Skip to content

[Java.Interop] Cache peer activation constructors - #12354

Merged
jonathanpeppers merged 2 commits into
dotnet:mainfrom
jonathanpeppers:jonathanpeppers-cache-peer-constructors
Aug 12, 2026
Merged

jonathanpeppers merged 2 commits into
dotnet:mainfrom
jonathanpeppers:jonathanpeppers-cache-peer-constructors

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Description

Cache reflection-based peer activation constructor resolution once per managed Type in TypeManager.CreateProxy.

The cache:

  • prefers the (IntPtr, JniHandleOwnership) constructor
  • falls back to (ref JniObjectReference, JniObjectReferenceOptions)
  • records missing constructors
  • uses a dedicated System.Threading.Lock
  • keeps peer instances, JNI references, and invocation argument arrays per activation

Add focused reflection activation tests for XA preference, Java.Interop fallback, and missing-constructor caching.

Fixes #7479

Validation

  • make all CONFIGURATION=Debug
  • Pixel 5, LLVM-IR typemap, CoreCLR: ReflectionCreateProxy tests passed (3/3)
  • Release MAUI/CoreCLR startup, 10 ActivityTaskManager Displayed runs after a 5-second warmup:
    • before: 1115.0 ms average
    • after: 1104.9 ms average
    • delta: -10.1 ms (-0.91%)

The startup result is indicative and within observed run-to-run variance.

Cache reflection-based XA, Java.Interop, and missing constructor resolution once per managed type while preserving XA-first activation semantics.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 06d344d6-8ee8-4076-9517-b1b5822a56be
Copilot AI lite review requested due to automatic review settings August 11, 2026 23:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the reflection-based peer activation path in Java.Interop.TypeManager.CreateProxy by caching activation constructor resolution per managed Type, addressing the performance concern in #7479.

Changes:

  • Add a Type → activation-constructor cache (including “missing constructor” entries) for TypeManager.CreateProxy, preferring (IntPtr, JniHandleOwnership) and falling back to (ref JniObjectReference, JniObjectReferenceOptions).
  • Add focused on-device tests to validate XA preference, Java.Interop fallback behavior, and missing-constructor caching via reflection.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs Adds reflection-driven tests/helpers to validate constructor caching behavior in TypeManager.CreateProxy.
src/Mono.Android/Java.Interop/TypeManager.cs Implements a cached resolution path for activation constructors to avoid repeated reflection.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 06d344d6-8ee8-4076-9517-b1b5822a56be
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Aug 12, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

@jonathanpeppers

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #12354

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — 0 errors, 0 warnings, 1 non-blocking suggestion. Constructor preference, Java.Interop fallback, missing-constructor behavior, and JNI ownership semantics are preserved and covered by focused tests. CI remains in progress; all completed checks observed are passing.

Generated by Android PR Reviewer for #12354 · gpt56 · 104 AIC · ⌖ 20.2 AIC · ⊞ 25.6K
Comment /review to run again

Comment thread src/Mono.Android/Java.Interop/TypeManager.cs

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor caching preserves activation resolution, concurrency, and JNI lifetime semantics, with the exact-head checks green.

@jonathanpeppers
jonathanpeppers merged commit feeba90 into dotnet:main Aug 12, 2026
44 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeManager.CreateProxy does not cache constructor reflection

3 participants