[TrimmableTypeMap] Use JniNativeMethod for RegisterNatives bootstrap - #11002
Conversation
There was a problem hiding this comment.
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 usefixedUTF-8 strings +stackallocJniNativeMethodinstead of allocating a delegate wrapper. - Switch to the span-based
JniEnvironment.Types.RegisterNativesoverload. - Bump
external/Java.Interopsubmodule to a revision that providesJniNativeMethodand 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. |
simonrozsival
left a comment
There was a problem hiding this comment.
🤖 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.
d489bff to
de48eba
Compare
2641a42 to
971433c
Compare
ba34b83 to
a3fdf27
Compare
971433c to
82a8542
Compare
6f42b4d to
384a5a8
Compare
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>
82a8542 to
de9b013
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
53564cc to
cd77f0f
Compare
simonrozsival
left a comment
There was a problem hiding this comment.
🤖 AI Review Summary
Verdict: ✅ LGTM
Found 0 issues.
Clean, focused refactoring of the RegisterNatives bootstrap in TrimmableTypeMap:
- 👍 Zero-allocation path:
JniNativeMethodstruct +[UnmanagedCallersOnly]replaces the delegate-basedJniNativeMethodRegistration— eliminatesDelegateallocation and GC handle. - 👍 UTF-8 literals (
"mono/android/Runtime"u8) avoid runtime string-to-UTF8 conversion. - 👍
fixedscope correctly pins the UTF-8 byte pointers for the lifetime of theRegisterNativesJNI call. - 👍 Collection expression
[method]is clean and idiomatic for the single-elementReadOnlySpan<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.
Summary
Refactor the
mono.android.Runtime.registerNativesbootstrap registration inTrimmableTypeMapto use zero-allocation JNI interop:JniNativeMethodstruct withfixedUTF-8 string literals instead ofJniNativeMethodRegistration+ delegate wrapper[UnmanagedCallersOnly]onOnRegisterNativescallback (noDelegateallocation, no GC handle)JniTypeUTF-8 constructor ("mono/android/Runtime"u8) to avoid string-to-UTF8 conversion at runtimeRegisterMethodhelper (already removed in a separate PR)Changes
TrimmableTypeMap.csRegisterNatives(): useJniNativeMethod+fixedUTF-8 literals +[UnmanagedCallersOnly]callback; remove old delegate-based registration andRegisterMethodhelper