Skip to content

Commit 3b24a2c

Browse files
jonathanpeppersjonpryor
authored andcommitted
[Java.Interop] apply AggressiveInlining where profiler shows calls (#541)
Context: https://github.com/xamarin/xamarin-android/blob/master/Documentation/guides/profiling.md#profiling-managed-code I noticed the following when profiling startup in the Blank Xamarin.Forms app template: Method call summary Total(ms) Self(ms) Calls Method name 2 2 6470 Java.Interop.JniObjectReference:get_Handle () 6 4 4043 Java.Interop.JniObjectReference:get_IsValid () 1 1 3881 Java.Interop.JniEnvironmentInfo:get_Runtime () 12 3 3084 Java.Interop.JniEnvironment:get_CurrentInfo () 5 3 3084 Java.Interop.JniEnvironmentInfo:get_IsValid () 9 1 1634 Java.Interop.JniEnvironment:get_EnvironmentPointer () 4 0 797 Java.Interop.JniEnvironment:get_Runtime () 2 0 672 Java.Interop.JniType:AssertValid () None of these calls seem to be slow in themselves, but Xamarin.Forms tends to make *many* managed-to-java transitions throughout its startup. I found I could just sprinkle: [MethodImpl (MethodImplOptions.AggressiveInlining)] and I was able to see a performance improvement! A `Debug` build of the app: * Before (Debug): 12-16 10:23:08.289 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +919ms 12-16 10:23:12.126 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +802ms 12-16 10:23:16.087 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +805ms 12-16 10:23:20.053 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +806ms 12-16 10:23:24.019 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +811ms 12-16 10:23:27.985 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +815ms 12-16 10:23:31.934 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +803ms 12-16 10:23:35.901 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +816ms 12-16 10:23:39.865 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +807ms 12-16 10:23:43.797 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +806ms * Average(ms): 819 * Std Err(ms): 11.2111056051082 * Std Dev(ms): 35.4526288008222 * After (Debug): 12-16 10:25:11.825 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +816ms 12-16 10:25:15.758 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +808ms 12-16 10:25:19.692 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +805ms 12-16 10:25:23.656 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +805ms 12-16 10:25:27.605 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +797ms 12-16 10:25:31.574 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +803ms 12-16 10:25:35.555 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +806ms 12-16 10:25:39.490 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +802ms 12-16 10:25:43.437 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +808ms 12-16 10:25:47.383 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +813ms * Average(ms): 806.3 * Std Err(ms): 1.71302202100395 * Std Dev(ms): 5.41705126839727 A `Release` build of the app: * Before (Release): 12-16 10:21:23.643 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +574ms 12-16 10:21:27.576 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +557ms 12-16 10:21:31.558 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +567ms 12-16 10:21:35.557 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +557ms 12-16 10:21:39.522 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +572ms 12-16 10:21:43.471 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +555ms 12-16 10:21:47.452 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +558ms 12-16 10:21:51.388 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +553ms 12-16 10:21:55.335 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +560ms 12-16 10:21:59.267 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +554ms * Average(ms): 560.7 * Std Err(ms): 2.39467000742157 * Std Dev(ms): 7.57261146794449 * After (Release): 12-16 10:26:52.042 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +551ms 12-16 10:26:56.023 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +547ms 12-16 10:27:00.038 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +550ms 12-16 10:27:04.004 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +533ms 12-16 10:27:07.953 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +540ms 12-16 10:27:11.917 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +552ms 12-16 10:27:15.869 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +540ms 12-16 10:27:19.832 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +534ms 12-16 10:27:23.815 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +543ms 12-16 10:27:27.765 1490 1517 I ActivityTaskManager: Displayed com.xamarin.forms.helloforms/crc6450e568c951913723.MainActivity: +538ms * Average(ms): 542.8 * Std Err(ms): 2.19494368442058 * Std Dev(ms): 6.94102137857086 I would say this is a ~17ms improvement to a Xamarin.Forms app's startup? These numbers were taken running on a Pixel 3 XL and a Blank Xamarin.Forms app template using Xamarin.Forms/master. The project and script used is in this PR: * <xamarin/Xamarin.Forms#8867>
1 parent f3553f4 commit 3b24a2c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/Java.Interop/Java.Interop/JniEnvironment.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67
using System.Threading;
78

@@ -12,6 +13,7 @@ public static partial class JniEnvironment {
1213
internal static readonly ThreadLocal<JniEnvironmentInfo> Info = new ThreadLocal<JniEnvironmentInfo> (() => new JniEnvironmentInfo (), trackAllValues: true);
1314

1415
internal static JniEnvironmentInfo CurrentInfo {
16+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
1517
get {
1618
var e = Info.Value;
1719
if (!e.IsValid)
@@ -21,22 +23,27 @@ internal static JniEnvironmentInfo CurrentInfo {
2123
}
2224

2325
public static JniRuntime Runtime {
26+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
2427
get {return CurrentInfo.Runtime;}
2528
}
2629

2730
public static IntPtr EnvironmentPointer {
31+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
2832
get {return CurrentInfo.EnvironmentPointer;}
2933
}
3034

3135
public static JniVersion JniVersion {
36+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
3237
get {return (JniVersion) Versions.GetVersion ();}
3338
}
3439

3540
public static int LocalReferenceCount {
41+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
3642
get {return CurrentInfo.LocalReferenceCount;}
3743
}
3844

3945
public static bool WithinNewObjectScope {
46+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
4047
get {return CurrentInfo.WithinNewObjectScope;}
4148
internal set {CurrentInfo.WithinNewObjectScope = value;}
4249
}

src/Java.Interop/Java.Interop/JniObjectReference.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
#if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES && FEATURE_JNIOBJECTREFERENCE_INTPTRS
@@ -24,6 +25,7 @@ internal JniReferenceSafeHandle SafeHandle {
2425
get {return gcHandle.IsAllocated ? ((JniReferenceSafeHandle) gcHandle.Target) : JniReferenceSafeHandle.Null;}
2526
}
2627
public IntPtr Handle {
28+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
2729
get {
2830
var h = SafeHandle;
2931
return h == null
@@ -32,7 +34,11 @@ public IntPtr Handle {
3234
}
3335
}
3436
#elif FEATURE_JNIOBJECTREFERENCE_INTPTRS
35-
public IntPtr Handle {get; private set;}
37+
public IntPtr Handle {
38+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
39+
get;
40+
private set;
41+
}
3642
#endif
3743

3844
uint referenceInfo;
@@ -47,6 +53,7 @@ internal JniObjectReferenceFlags Flags {
4753
}
4854

4955
public bool IsValid {
56+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
5057
get {
5158
#if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
5259
return SafeHandle != null && !SafeHandle.IsInvalid && !SafeHandle.IsClosed;

src/Java.Interop/Java.Interop/JniType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Runtime.CompilerServices;
56
using System.Threading;
67

78
using Java.Interop;
@@ -78,6 +79,7 @@ void RegisterWithRuntime ()
7879
registered = true;
7980
}
8081

82+
[MethodImpl (MethodImplOptions.AggressiveInlining)]
8183
void AssertValid ()
8284
{
8385
if (!PeerReference.IsValid)

0 commit comments

Comments
 (0)