Skip to content

[TrimmableTypeMap] Use JniNativeMethod for RegisterNatives bootstrap - #11002

Merged
simonrozsival merged 5 commits into
mainfrom
dev/simonrozsival/trimmable-typemap-uco-register-natives
Mar 27, 2026
Merged

[TrimmableTypeMap] Use JniNativeMethod for RegisterNatives bootstrap#11002
simonrozsival merged 5 commits into
mainfrom
dev/simonrozsival/trimmable-typemap-uco-register-natives

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Mar 23, 2026

Copy link
Copy Markdown
Member

Summary

Refactor the mono.android.Runtime.registerNatives bootstrap registration in TrimmableTypeMap to use zero-allocation JNI interop:

  • Use JniNativeMethod struct with fixed UTF-8 string literals instead of JniNativeMethodRegistration + delegate wrapper
  • Use [UnmanagedCallersOnly] on OnRegisterNatives callback (no Delegate allocation, no GC handle)
  • Use JniType UTF-8 constructor ("mono/android/Runtime"u8) to avoid string-to-UTF8 conversion at runtime
  • Remove RegisterMethod helper (already removed in a separate PR)

Changes

File Change
TrimmableTypeMap.cs RegisterNatives(): use JniNativeMethod + fixed UTF-8 literals + [UnmanagedCallersOnly] callback; remove old delegate-based registration and RegisterMethod helper

Copilot AI review requested due to automatic review settings March 23, 2026 18:40

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

Replace the Marshal.GetDelegateForFunctionPointer-based bootstrap registration for mono.android.Runtime.registerNatives with a zero-allocation registration path using JniNativeMethod and the new RegisterNatives(ReadOnlySpan<JniNativeMethod>) overload.

Changes:

  • Update RegisterNatives() to use fixed UTF-8 strings + stackalloc JniNativeMethod instead of allocating a delegate wrapper.
  • Switch to the span-based JniEnvironment.Types.RegisterNatives overload.
  • Bump external/Java.Interop submodule to a revision that provides JniNativeMethod and the span overload.

Reviewed changes

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

File Description
src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs Reworks bootstrap RegisterNatives() to use stackalloc + span-based JNI registration.
external/Java.Interop Updates submodule commit to pick up the new JniNativeMethod API surface.

Comment thread src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs Outdated
@simonrozsival simonrozsival added copilot `copilot-cli` or other AIs were used to author this trimmable-type-map labels Mar 23, 2026

@simonrozsival simonrozsival left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 AI Review Summary

Verdict: ✅ LGTM

Found 0 issues.

Clean, minimal change: replaces Marshal.GetDelegateForFunctionPointer + JniNativeMethodRegistration with JniNativeMethod stackalloc + ReadOnlySpan<JniNativeMethod> for zero-allocation JNI native method registration. JniType("..."u8) avoids string→UTF-8 conversion.

👍 Good use of fixed + stackalloc for the blittable JNI struct — matches the pattern used by generated proxy RegisterNatives methods. Submodule bump is justified (picks up JniNativeMethod struct and span overload from dotnet/java-interop#1393).


Review generated by android-reviewer from review guidelines.

@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-build-pipeline branch 3 times, most recently from d489bff to de48eba Compare March 24, 2026 21:54
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-uco-register-natives branch 2 times, most recently from 2641a42 to 971433c Compare March 25, 2026 10:07
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-build-pipeline branch 2 times, most recently from ba34b83 to a3fdf27 Compare March 25, 2026 12:30
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-uco-register-natives branch from 971433c to 82a8542 Compare March 25, 2026 12:32
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-build-pipeline branch from 6f42b4d to 384a5a8 Compare March 25, 2026 16:16
simonrozsival and others added 3 commits March 26, 2026 16:40
Replace Marshal.GetDelegateForFunctionPointer wrapper with
JniNativeMethod stackalloc + ReadOnlySpan<JniNativeMethod> for the
mono.android.Runtime.registerNatives bootstrap registration.

No delegate allocation, no GC handle — same pattern as the generated
proxy RegisterNatives methods.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Avoids string-to-UTF8 conversion at runtime. The ReadOnlySpan<byte>
overload was added in dotnet/java-interop#1393.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-uco-register-natives branch from 82a8542 to de9b013 Compare March 26, 2026 15:43
@simonrozsival
simonrozsival changed the base branch from dev/simonrozsival/trimmable-typemap-build-pipeline to main March 26, 2026 15:47
simonrozsival and others added 2 commits March 26, 2026 16:50
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/trimmable-typemap-uco-register-natives branch from 53564cc to cd77f0f Compare March 26, 2026 16:31

@simonrozsival simonrozsival left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 AI Review Summary

Verdict: ✅ LGTM

Found 0 issues.

Clean, focused refactoring of the RegisterNatives bootstrap in TrimmableTypeMap:

  • 👍 Zero-allocation path: JniNativeMethod struct + [UnmanagedCallersOnly] replaces the delegate-based JniNativeMethodRegistration — eliminates Delegate allocation and GC handle.
  • 👍 UTF-8 literals ("mono/android/Runtime"u8) avoid runtime string-to-UTF8 conversion.
  • 👍 fixed scope correctly pins the UTF-8 byte pointers for the lifetime of the RegisterNatives JNI call.
  • 👍 Collection expression [method] is clean and idiomatic for the single-element ReadOnlySpan<JniNativeMethod>.

CI is still pending — all Azure DevOps checks are in progress. Verify CI passes before merging.


Review generated by android-reviewer from review guidelines.

@simonrozsival
simonrozsival removed the request for review from grendello March 26, 2026 16:40
@simonrozsival
simonrozsival merged commit 26e67aa into main Mar 27, 2026
6 checks passed
@simonrozsival
simonrozsival deleted the dev/simonrozsival/trimmable-typemap-uco-register-natives branch March 27, 2026 09:58
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

copilot `copilot-cli` or other AIs were used to author this trimmable-type-map

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants