Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] FixAbstractMethodsStep performance (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jonathanpeppers authored Feb 7, 2024
1 parent dbefbad commit 526172b
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ bool MightNeedFix (TypeDefinition type)
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache);
}

static bool CompareTypes (TypeReference iType, TypeReference tType)
bool CompareTypes (TypeReference iType, TypeReference tType)
{
if (iType.IsGenericParameter)
return true;
Expand All @@ -164,11 +164,11 @@ static bool CompareTypes (TypeReference iType, TypeReference tType)
if (iType.Namespace != tType.Namespace)
return false;

TypeDefinition iTypeDef = iType.Resolve ();
TypeDefinition iTypeDef = cache.Resolve (iType);
if (iTypeDef == null)
return false;

TypeDefinition tTypeDef = tType.Resolve ();
TypeDefinition tTypeDef = cache.Resolve (tType);
if (tTypeDef == null)
return false;

Expand Down Expand Up @@ -198,7 +198,7 @@ bool IsInOverrides (MethodDefinition iMethod, MethodDefinition tMethod)
return false;

foreach (var o in tMethod.Overrides)
if (o != null && iMethod == o.Resolve ())
if (o != null && iMethod.Name == o.Name && iMethod == cache.Resolve (o))
return true;

return false;
Expand Down Expand Up @@ -252,7 +252,7 @@ bool FixAbstractMethods (TypeDefinition type)

foreach (var ifaceInfo in type.Interfaces) {
var iface = ifaceInfo.InterfaceType;
var ifaceDef = iface.Resolve ();
var ifaceDef = cache.Resolve (iface);
if (ifaceDef == null) {
LogMessage ($"Unable to unresolve interface: {iface.FullName}");
continue;
Expand Down

0 comments on commit 526172b

Please sign in to comment.