Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] lazily populate Resource lookup
Browse files Browse the repository at this point in the history
Fixes: dotnet#7684

Comparing .NET 7 to main, I noticed:

    .NET 7
    Task LinkAssembliesNoShrink 4ms
    main/.NET 8
    Task LinkAssembliesNoShrink 101ms

Under `dotnet trace` a lot of the time was spent in:

    94.74ms MonoDroid.Tuner.FixLegacyResourceDesignerStep.LoadDesigner()

Reviewing the code, I think we can "lazily" call the `LoadDesigner()`
method. It creates a `Dictionary` that isn't used until the
`FixBody()` method.

I also updated one log message to only log duplicates, as it was
logging hundreds of lines:

    if (output.ContainsKey (key)) {
        LogMessage ($"          Found duplicate {key}");
    } else {
        output.Add (key, property.GetMethod);
    }

Which also showed up in `dotnet trace`:

    25.58ms Microsoft.Android.Build.Tasks.MSBuildExtensions.LogDebugMessage()

With these changes, I instead get:

    Task LinkAssembliesNoShrink 5ms

Which is probably ~the same performance as before or plenty good enough!
  • Loading branch information
jonathanpeppers committed Jan 11, 2023
1 parent 8c24b8f commit 30501c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ protected override void LoadDesigner ()
return;
}
lookup = BuildResourceDesignerPropertyLookup (designerType);
return;
}

internal override bool ProcessAssemblyDesigner (AssemblyDefinition assembly)
Expand Down Expand Up @@ -114,9 +113,10 @@ Dictionary<string, MethodDefinition> BuildResourceDesignerPropertyLookup (TypeDe
foreach (PropertyDefinition property in definition.Properties)
{
string key = $"{definition.Name}::{property.Name}";
if (!output.ContainsKey (key)) {
LogMessage ($" Adding {key}");
output.Add(key, property.GetMethod);
if (output.ContainsKey (key)) {
LogMessage ($" Found duplicate {key}");
} else {
output.Add (key, property.GetMethod);
}
}
}
Expand All @@ -125,6 +125,10 @@ Dictionary<string, MethodDefinition> BuildResourceDesignerPropertyLookup (TypeDe

protected override void FixBody (MethodBody body, TypeDefinition designer)
{
// This is expected to be null for the first call, in <LinkAssembliesNoShrink/>
if (lookup == null)
LoadDesigner ();

// replace
// IL_0068: ldsfld int32 Xamarin.Forms.Platform.Android.Resource/Layout::Toolbar
// with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public override bool RunTask ()
var fixAbstractMethodsStep = new FixAbstractMethodsStep (resolver, cache, Log);
var addKeepAliveStep = new AddKeepAlivesStep (resolver, cache, Log, UsingAndroidNETSdk);
var fixLegacyResourceDesignerStep = new FixLegacyResourceDesignerStep (resolver, cache, Log);
if (UseDesignerAssembly)
fixLegacyResourceDesignerStep.Load ();
for (int i = 0; i < SourceFiles.Length; i++) {
var source = SourceFiles [i];
var destination = DestinationFiles [i];
Expand Down Expand Up @@ -140,10 +138,6 @@ public FixLegacyResourceDesignerStep (DirectoryAssemblyResolver resolver, TypeDe
this.logger = logger;
}

public void Load () {
LoadDesigner ();
}

public override void LogMessage (string message)
{
logger.LogDebugMessage ("{0}", message);
Expand Down

0 comments on commit 30501c1

Please sign in to comment.