Skip to content

Commit 30501c1

Browse files
[Xamarin.Android.Build.Tasks] lazily populate Resource lookup
Fixes: #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!
1 parent 8c24b8f commit 30501c1

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ protected override void LoadDesigner ()
6868
return;
6969
}
7070
lookup = BuildResourceDesignerPropertyLookup (designerType);
71-
return;
7271
}
7372

7473
internal override bool ProcessAssemblyDesigner (AssemblyDefinition assembly)
@@ -114,9 +113,10 @@ Dictionary<string, MethodDefinition> BuildResourceDesignerPropertyLookup (TypeDe
114113
foreach (PropertyDefinition property in definition.Properties)
115114
{
116115
string key = $"{definition.Name}::{property.Name}";
117-
if (!output.ContainsKey (key)) {
118-
LogMessage ($" Adding {key}");
119-
output.Add(key, property.GetMethod);
116+
if (output.ContainsKey (key)) {
117+
LogMessage ($" Found duplicate {key}");
118+
} else {
119+
output.Add (key, property.GetMethod);
120120
}
121121
}
122122
}
@@ -125,6 +125,10 @@ Dictionary<string, MethodDefinition> BuildResourceDesignerPropertyLookup (TypeDe
125125

126126
protected override void FixBody (MethodBody body, TypeDefinition designer)
127127
{
128+
// This is expected to be null for the first call, in <LinkAssembliesNoShrink/>
129+
if (lookup == null)
130+
LoadDesigner ();
131+
128132
// replace
129133
// IL_0068: ldsfld int32 Xamarin.Forms.Platform.Android.Resource/Layout::Toolbar
130134
// with

src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ public override bool RunTask ()
6767
var fixAbstractMethodsStep = new FixAbstractMethodsStep (resolver, cache, Log);
6868
var addKeepAliveStep = new AddKeepAlivesStep (resolver, cache, Log, UsingAndroidNETSdk);
6969
var fixLegacyResourceDesignerStep = new FixLegacyResourceDesignerStep (resolver, cache, Log);
70-
if (UseDesignerAssembly)
71-
fixLegacyResourceDesignerStep.Load ();
7270
for (int i = 0; i < SourceFiles.Length; i++) {
7371
var source = SourceFiles [i];
7472
var destination = DestinationFiles [i];
@@ -140,10 +138,6 @@ public FixLegacyResourceDesignerStep (DirectoryAssemblyResolver resolver, TypeDe
140138
this.logger = logger;
141139
}
142140

143-
public void Load () {
144-
LoadDesigner ();
145-
}
146-
147141
public override void LogMessage (string message)
148142
{
149143
logger.LogDebugMessage ("{0}", message);

0 commit comments

Comments
 (0)