Skip to content

Commit 526172b

Browse files
[Xamarin.Android.Build.Tasks] FixAbstractMethodsStep performance (#8650)
Context: #8421 Working a bit on build performance, I tested: * `dotnet new maui` * `dotnet build -bl` The `.binlog` shows: LinkAssembliesNoShrink 3.797s Attaching `dotnet-trace` as mentioned on: https://github.com/xamarin/xamarin-android/blob/2f192386e8072f8e0ecaf0de2fe48654f3ade423/Documentation/guides/tracing.md#how-to-dotnet-trace-our-build I see time broken down such as: FixAbstractMethods: 37% AssemblyDefinition.Write: 27% ProcessAssemblyDesigner: 20% CopyIfChanged: 13% DirectoryAssemblyResolver.GetAssembly: 4.4% This made me focus in on `FixAbstractMethodsStep` and make the following changes: * All calls for `TypeReference.Resolve()` and `MethodReference.Resolve()` should go through the `TypeDefinitionCache` to avoid repeated lookups. * `IsInOverrides()` can compare the `MethodReference.Name` before calling `Resolve()`. It could resolve many unnecessary methods otherwise. After these changes, I instead see from `dotnet-trace`: --1.45s (3.7%) xamarin.android.build.tasks!MonoDroid.Tuner.FixAbstractMethodsStep.FixAbstractMethods() ++949.70ms (2.5%) xamarin.android.build.tasks!MonoDroid.Tuner.FixAbstractMethodsStep.FixAbstractMethods() Time is now broken down differently, such as: AssemblyDefinition.Write: 31% FixAbstractMethods: 28% ProcessAssemblyDesigner: 23% CopyIfChanged: 12% DirectoryAssemblyResolver.GetAssembly: 4.8% In an overall `.binlog` (without `dotnet-trace` attached): --LinkAssembliesNoShrink 3.797s ++LinkAssembliesNoShrink 3.105s This saved ~700ms on initial build of a new MAUI project.
1 parent dbefbad commit 526172b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ bool MightNeedFix (TypeDefinition type)
141141
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache);
142142
}
143143

144-
static bool CompareTypes (TypeReference iType, TypeReference tType)
144+
bool CompareTypes (TypeReference iType, TypeReference tType)
145145
{
146146
if (iType.IsGenericParameter)
147147
return true;
@@ -164,11 +164,11 @@ static bool CompareTypes (TypeReference iType, TypeReference tType)
164164
if (iType.Namespace != tType.Namespace)
165165
return false;
166166

167-
TypeDefinition iTypeDef = iType.Resolve ();
167+
TypeDefinition iTypeDef = cache.Resolve (iType);
168168
if (iTypeDef == null)
169169
return false;
170170

171-
TypeDefinition tTypeDef = tType.Resolve ();
171+
TypeDefinition tTypeDef = cache.Resolve (tType);
172172
if (tTypeDef == null)
173173
return false;
174174

@@ -198,7 +198,7 @@ bool IsInOverrides (MethodDefinition iMethod, MethodDefinition tMethod)
198198
return false;
199199

200200
foreach (var o in tMethod.Overrides)
201-
if (o != null && iMethod == o.Resolve ())
201+
if (o != null && iMethod.Name == o.Name && iMethod == cache.Resolve (o))
202202
return true;
203203

204204
return false;
@@ -252,7 +252,7 @@ bool FixAbstractMethods (TypeDefinition type)
252252

253253
foreach (var ifaceInfo in type.Interfaces) {
254254
var iface = ifaceInfo.InterfaceType;
255-
var ifaceDef = iface.Resolve ();
255+
var ifaceDef = cache.Resolve (iface);
256256
if (ifaceDef == null) {
257257
LogMessage ($"Unable to unresolve interface: {iface.FullName}");
258258
continue;

0 commit comments

Comments
 (0)