forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[linker] move StripEmbeddedLibraries into the linker
Fixes: dotnet#1092 The `StripEmbeddedLibraries` MSBuild task can take 1-2 seconds, and it mainly removes `__AndroidLibraryProjects__.zip` from assemblies. If we moved this to happen during linking, it has various benefits: - The linker already has every assembly opened and loaded. - We know if the linker is going to `Skip`/`Delete` an assembly, so we can likewise skip it. - The linker writes all the assemblies out at the end, so we don't have a second "write" step. Changes to make this happen: - Removed the `StripEmbeddedLibraries` MSBuild task and related targets - Removed `$(_AndroidStripFlag)` from our targets, since it is no longer used - Created a new `StripEmbeddedLibraries` linker step that runs late during linking - Removed a `RemoveLibraryResourceZip` linker step, as it seemed to be duplicative. I timed before and after with the Xamarin.Forms test project: .\bin\Debug\bin\xabuild .\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj /p:Configuration=Release /t:Clean .\bin\Debug\bin\xabuild .\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj /p:Configuration=Release /t:Build /bl Before: 1233 ms StripEmbeddedLibraries 1 calls 14925 ms LinkAssemblies 1 calls After: 15437 ms LinkAssemblies 1 calls As expected, `LinkAssemblies` will be slightly slower. But since `StripEmbeddedLibraries` is not called at all, we have a net gain of around 700ms. Once this has been merged and working for `Release` builds, I plan to do some further research to find out if running the new `StripEmbeddedLibraries` linker step will help for `Debug` builds. It could be a net performance improvement, if the time taken to remove these files improves deployment and app startup times. Greatly expanded upon an existing test: - Made it a Xamarin.Forms project - Moved the `AndroidEnvironment` item to a referenced library project - Added an `$(AndroidLinkSkip)` option for a support library assembly - Made sure the `$(AndroidLinkSkip)` assembly is saved and stripped - Check and make sure `EmbeddedResource` items are stripped at the end Other changes: - Removed some assertions in tests looking for `_StripEmbeddedLibraries`, since it is removed now.
- Loading branch information
1 parent
95ca102
commit 679cec9
Showing
9 changed files
with
119 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveLibraryResourceZip.cs
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEmbeddedLibraries.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Mono.Cecil; | ||
using Mono.Linker; | ||
using Mono.Linker.Steps; | ||
using System; | ||
using System.Linq; | ||
using Xamarin.Android.Tasks; | ||
|
||
namespace MonoDroid.Tuner | ||
{ | ||
public class StripEmbeddedLibraries : BaseStep | ||
{ | ||
protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
{ | ||
if (!Annotations.HasAction (assembly)) | ||
return; | ||
var action = Annotations.GetAction (assembly); | ||
if (action == AssemblyAction.Skip || action == AssemblyAction.Delete) | ||
return; | ||
|
||
var fileName = assembly.Name.Name + ".dll"; | ||
if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (fileName)) | ||
return; | ||
|
||
bool assembly_modified = false; | ||
foreach (var mod in assembly.Modules) { | ||
foreach (var r in mod.Resources.ToArray ()) { | ||
if (ShouldStripResource (r)) { | ||
Context.LogMessage (" Stripped {0} from {1}", r.Name, fileName); | ||
mod.Resources.Remove (r); | ||
assembly_modified = true; | ||
} | ||
} | ||
} | ||
if (assembly_modified && action == AssemblyAction.Copy) { | ||
Annotations.SetAction (assembly, AssemblyAction.Save); | ||
} | ||
} | ||
|
||
bool ShouldStripResource (Resource r) | ||
{ | ||
if (!(r is EmbeddedResource)) | ||
return false; | ||
// embedded jars | ||
if (r.Name.EndsWith (".jar", StringComparison.InvariantCultureIgnoreCase)) | ||
return true; | ||
// embedded AndroidNativeLibrary archive | ||
if (r.Name == "__AndroidNativeLibraries__.zip") | ||
return true; | ||
// embedded AndroidResourceLibrary archive | ||
if (r.Name == "__AndroidLibraryProjects__.zip") | ||
return true; | ||
// embedded AndroidEnvironment item | ||
if (r.Name.StartsWith ("__AndroidEnvironment__", StringComparison.Ordinal)) | ||
return true; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 0 additions & 93 deletions
93
src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.