From 0cb972d87802e5f544bf2a0589fe2175b4b5fade Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 22 Mar 2023 16:32:16 -0500 Subject: [PATCH] [Mono.Android] delay JNINativeWrapper.get_runtime_types() Reviewing `dotnet-trace` output, I noticed in a `dotnet new maui` app on a Pixel 5: 6.38ms mono.android!Android.Runtime.JNINativeWrapper.CreateDelegate(System.Delegate) 1.16ms mono.android!Android.Runtime.JNINativeWrapper.get_runtime_types() We spend ~1ms looking up `AndroidEnvironment.UnhandledException` and `AndroidRuntimeInternal.WaitForBridgeProcessing`. However, in this app *all* calls go to the "fast path" via `JNINativeWrapper.CreateBuiltInDelegate()`. We don't *need* to look up these methods in this app at all. Delay the call to `get_runtime_types()` to only when it is needed via the "slow path". Will help us a small amount in .NET 8. --- src/Mono.Android/Android.Runtime/JNINativeWrapper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index d7b27e855d8..fd37a69647c 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -34,8 +34,6 @@ public static Delegate CreateDelegate (Delegate dlg) if (dlg.Method == null) throw new ArgumentException (); - get_runtime_types (); - var delegateType = dlg.GetType (); var result = CreateBuiltInDelegate (dlg, delegateType); if (result != null) @@ -45,6 +43,8 @@ public static Delegate CreateDelegate (Delegate dlg) RuntimeNativeMethods.monodroid_log (LogLevel.Debug, LogCategories.Assembly, $"Falling back to System.Reflection.Emit for delegate type '{delegateType}': {dlg.Method}"); } + get_runtime_types (); + var ret_type = dlg.Method.ReturnType; var parameters = dlg.Method.GetParameters (); var param_types = new Type [parameters.Length];