Skip to content

Commit 2625783

Browse files
authored
[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.
1 parent 03018e0 commit 2625783

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,13 @@ bool AddKeepAlives (TypeDefinition type)
8686
{
8787
bool changed = false;
8888
foreach (MethodDefinition method in type.Methods) {
89-
if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute"))
89+
if (method.Parameters.Count == 0)
9090
continue;
9191

92-
if (method.Parameters.Count == 0)
92+
if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute"))
9393
continue;
9494

95-
var processor = method.Body.GetILProcessor ();
96-
var module = method.DeclaringType.Module;
9795
var instructions = method.Body.Instructions;
98-
var end = instructions.Last ();
99-
if (end.Previous.OpCode == OpCodes.Endfinally)
100-
end = end.Previous;
10196

10297
var found = false;
10398
for (int off = Math.Max (0, instructions.Count - 6); off < instructions.Count; off++) {
@@ -111,6 +106,12 @@ bool AddKeepAlives (TypeDefinition type)
111106
if (found)
112107
continue;
113108

109+
var processor = method.Body.GetILProcessor ();
110+
var module = method.DeclaringType.Module;
111+
var end = instructions.Last ();
112+
if (end.Previous.OpCode == OpCodes.Endfinally)
113+
end = end.Previous;
114+
114115
for (int i = 0; i < method.Parameters.Count; i++) {
115116
if (method.Parameters [i].ParameterType.IsValueType || method.Parameters [i].ParameterType.FullName == "System.String")
116117
continue;

0 commit comments

Comments
 (0)