From 2625783ea2e860424709c99ce241e138b9b87989 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Tue, 17 Oct 2023 16:33:33 +0000 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Small refactoring to `AddKeepAlives` method (#8423) The `method.Parameters.Count == 0` check should be faster than LINQ's `Any` call (`CustomAttributes.Any`). So moved it first. Moved some variables near their usage, particularly, `GetILProcessor` which may allocate unnecessarily if `GC.KeepAlive(...)` call was already found. It's not a big of problem though as it's not very common. --- .../Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs index d11fd13bb2c..7263864219e 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs @@ -86,18 +86,13 @@ bool AddKeepAlives (TypeDefinition type) { bool changed = false; foreach (MethodDefinition method in type.Methods) { - if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute")) + if (method.Parameters.Count == 0) continue; - if (method.Parameters.Count == 0) + if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute")) continue; - var processor = method.Body.GetILProcessor (); - var module = method.DeclaringType.Module; var instructions = method.Body.Instructions; - var end = instructions.Last (); - if (end.Previous.OpCode == OpCodes.Endfinally) - end = end.Previous; var found = false; for (int off = Math.Max (0, instructions.Count - 6); off < instructions.Count; off++) { @@ -111,6 +106,12 @@ bool AddKeepAlives (TypeDefinition type) if (found) continue; + var processor = method.Body.GetILProcessor (); + var module = method.DeclaringType.Module; + var end = instructions.Last (); + if (end.Previous.OpCode == OpCodes.Endfinally) + end = end.Previous; + for (int i = 0; i < method.Parameters.Count; i++) { if (method.Parameters [i].ParameterType.IsValueType || method.Parameters [i].ParameterType.FullName == "System.String") continue;