From 30501c14b103ac7f645533b050ab60e234fb38f2 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 11 Jan 2023 15:29:02 -0600 Subject: [PATCH] [Xamarin.Android.Build.Tasks] lazily populate Resource lookup Fixes: https://github.com/xamarin/xamarin-android/issues/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! --- .../MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs | 12 ++++++++---- .../Tasks/LinkAssembliesNoShrink.cs | 6 ------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs index 8a88408c42d..1a3cf2d89ec 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs @@ -68,7 +68,6 @@ protected override void LoadDesigner () return; } lookup = BuildResourceDesignerPropertyLookup (designerType); - return; } internal override bool ProcessAssemblyDesigner (AssemblyDefinition assembly) @@ -114,9 +113,10 @@ Dictionary 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); } } } @@ -125,6 +125,10 @@ Dictionary BuildResourceDesignerPropertyLookup (TypeDe protected override void FixBody (MethodBody body, TypeDefinition designer) { + // This is expected to be null for the first call, in + if (lookup == null) + LoadDesigner (); + // replace // IL_0068: ldsfld int32 Xamarin.Forms.Platform.Android.Resource/Layout::Toolbar // with diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs index 5d4c65c8509..b560540e61a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs @@ -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]; @@ -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);