Skip to content

Commit 1e12463

Browse files
[Mono.Android] JNIEnv.GetObjectArray() can use Array.Empty<T>() (#6769)
Context: https://github.com/microsoft/dotnet-podcasts Context: #6766 While working on #6766 and reviewing `dotnet trace` output, I found time was being spent in `JNIEnv.GetObjectArray()`: 17.09ms Mono.Android!Java.Interop.TypeManager.n_Activate(intptr,intptr,intptr,intptr,intptr,intptr) … 4.96ms Mono.Android!Android.Runtime.JNIEnv.GetObjectArray In this case, it is taking an array of parameters and calling a constructor. A lot of the time we are calling empty constructors! Reviewing `JNIEnv.GetObjectArray()` we could add a check if the array is of length 0 and return `Array.Empty<object>()`. I also found one other place doing `new Type[0]` we can fix while we're at it. After the change, I instead get: 2.56ms Mono.Android!Android.Runtime.JNIEnv.GetObjectArray I'm not able to see a noticeable difference in `dotnet new maui`, likely as this is such as small improvement.
1 parent c80dfff commit 1e12463

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,8 @@ static int _GetArrayLength (IntPtr array_ptr)
13251325
return null;
13261326

13271327
int cnt = _GetArrayLength (array_ptr);
1328+
if (cnt == 0)
1329+
return Array.Empty<object> ();
13281330

13291331
var converter = GetConverter (NativeArrayElementToManaged, null, array_ptr);
13301332

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ internal static bool ActivationEnabled {
122122
static Type[] GetParameterTypes (string? signature)
123123
{
124124
if (String.IsNullOrEmpty (signature))
125-
return new Type[0];
125+
return Array.Empty<Type> ();
126126
string[] typenames = signature!.Split (':');
127127
Type[] result = new Type [typenames.Length];
128128
for (int i = 0; i < typenames.Length; i++)

0 commit comments

Comments
 (0)