Skip to content

Commit

Permalink
[Mono.Android] Remove more LINQ usage (#5443)
Browse files Browse the repository at this point in the history
Context: #5442

Apk size of BuildReleaseArm64False/net6 comparison:

    apkdiff --bs -e dll$ before.apk after.apk
    Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
      +         274 assemblies/Mono.Android.dll
        Type Android.Runtime.AndroidTypeManager
          -          42 Method System.Collections.Generic.IEnumerable`1<System.Type> GetTypesForSimpleReference (string)
          -          33 Method public void RegisterNativeMembers (Java.Interop.JniType, System.Type, string)
          +             Method System.Collections.Generic.IEnumerable`1<System.Type> <>n__0 (string)
          -             Type <>c__DisplayClass11_0
          -             Type <>c__DisplayClass11_1
          +             Type <GetTypesForSimpleReference>d__2
      -          67 assemblies/System.Private.CoreLib.dll
        Type System.Array
          -             Method public static void Fill (!!T[], !!T)
      -         424 assemblies/System.Linq.dll
        Type System.Linq.Enumerable
          -             Method public static System.Collections.Generic.IEnumerable`1<!!TResult> Repeat (!!TResult, int)
          -             Type RepeatIterator`1<TResult>
        Type System.Linq.ThrowHelper
          -             Method static void ThrowArgumentOutOfRangeException (System.Linq.ExceptionArgument)
    Summary:
      -         217 Assemblies -0.03% (of 782,054)
  • Loading branch information
radekdoulik authored Dec 18, 2020
1 parent e88cfbc commit 09baa52
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,12 @@ public AndroidTypeManager (bool jniAddNativeMethodRegistrationAttributePresent)

protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
{
foreach (var ti in base.GetTypesForSimpleReference (jniSimpleReference))
yield return ti;

var t = Java.Interop.TypeManager.GetJavaToManagedType (jniSimpleReference);
if (t == null)
return base.GetTypesForSimpleReference (jniSimpleReference);
return base.GetTypesForSimpleReference (jniSimpleReference)
.Concat (Enumerable.Repeat (t, 1));
if (t != null)
yield return t;
}

protected override string? GetSimpleReference (Type type)
Expand Down Expand Up @@ -395,7 +396,13 @@ public override void RegisterNativeMembers (JniType nativeClass, Type type, stri
Delegate callback;
if (toks [2] == "__export__") {
var mname = toks [0].Substring (2);
var minfo = type.GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Where (m => m.Name == mname && JavaNativeTypeManager.GetJniSignature (m) == toks [1]).FirstOrDefault ();
MethodInfo? minfo = null;
foreach (var mi in type.GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
if (mi.Name == mname && JavaNativeTypeManager.GetJniSignature (mi) == toks [1]) {
minfo = mi;
break;
}

if (minfo == null)
throw new InvalidOperationException (String.Format ("Specified managed method '{0}' was not found. Signature: {1}", mname, toks [1]));
callback = CreateDynamicCallback (minfo);
Expand Down

0 comments on commit 09baa52

Please sign in to comment.