Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] ResolveLibraryProjectImports temp files
Browse files Browse the repository at this point in the history
When reviewing `ResolveLibraryProjectImports`, I noticed a few things
we could improve:

1. We are using `GetResourceData` to get the entire contents of a file
  as a `byte[]` and then writing to disk.
2. We are writing temporary zip files to disk, reading them to extract,
  then deleting them.

The first thing I did was take advantage of the `GetResourceStream`
method, so we could just do `stream.CopyTo(file)`. This prevents us
from allocating big `byte[]` the size of each file.

Next, I took advantage of `ZipArchive.Open (stream)` and
`GetResourceStream` to avoid the need for creating temporary zip
files. We can unzip directly from the `Stream` in memory.

~~ Results ~~

When timing these changes, I used the Xamarin.Forms integration
project in this repo:

    Before
    1. 1793 ms  ResolveLibraryProjectImports               1 calls
    2. 1780 ms  ResolveLibraryProjectImports               1 calls
    3. 1770 ms  ResolveLibraryProjectImports               1 calls
    After
    1. 1750 ms  ResolveLibraryProjectImports               1 calls
    2. 1740 ms  ResolveLibraryProjectImports               1 calls
    3. 1702 ms  ResolveLibraryProjectImports               1 calls

I would say the improvements here are around 50ms.
  • Loading branch information
jonathanpeppers committed Dec 14, 2018
1 parent b0aaaf2 commit aeda0e2
Showing 1 changed file with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ void Extract (
Directory.CreateDirectory (outDirForDll);
var finfo = new FileInfo (Path.Combine (outDirForDll, envtxt.Name));
if (!finfo.Exists || finfo.LastWriteTimeUtc > assemblyLastWrite) {
using (var stream = envtxt.GetResourceStream ())
using (var fs = finfo.Create ()) {
var data = envtxt.GetResourceData ();
fs.Write (data, 0, data.Length);
stream.CopyTo (fs);
}
updated = true;
}
Expand All @@ -283,24 +283,18 @@ void Extract (
var outjarFile = Path.Combine (importsDir, resjar.Name);
var fi = new FileInfo (outjarFile);
if (!fi.Exists || fi.LastWriteTimeUtc > assemblyLastWrite) {
var data = resjar.GetResourceData ();
using (var stream = resjar.GetResourceStream ())
using (var outfs = File.Create (outjarFile))
outfs.Write (data, 0, data.Length);
stream.CopyTo (outfs);
updated = true;
}
}

var libzip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidNativeLibraries__.zip") as EmbeddedResource;
if (libzip != null) {
if (!Directory.Exists (outDirForDll))
Directory.CreateDirectory (outDirForDll);
var finfo = new FileInfo (Path.Combine (outDirForDll, libzip.Name));
using (var fs = finfo.Create ()) {
var data = libzip.GetResourceData ();
fs.Write (data, 0, data.Length);
}
List<string> files = new List<string> ();
using (var zip = MonoAndroidHelper.ReadZipFile (finfo.FullName)) {
using (var stream = libzip.GetResourceStream ())
using (var zip = Xamarin.Tools.Zip.ZipArchive.Open (stream)) {
try {
updated |= Files.ExtractAll (zip, nativeimportsDir, modifyCallback: (entryFullName) => {
files.Add (Path.GetFullPath (Path.Combine (nativeimportsDir, entryFullName)));
Expand All @@ -318,24 +312,15 @@ void Extract (
return;
}
}

finfo.Delete ();
}

// embedded AndroidResourceLibrary archive
var reszip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidLibraryProjects__.zip") as EmbeddedResource;
if (reszip != null) {
if (!Directory.Exists (outDirForDll))
Directory.CreateDirectory (outDirForDll);
var finfo = new FileInfo (Path.Combine (outDirForDll, reszip.Name));
using (var fs = finfo.Create ()) {
var data = reszip.GetResourceData ();
fs.Write (data, 0, data.Length);
}

// temporarily extracted directory will look like:
// __library_projects__/[dllname]/[library_project_imports | jlibs]/bin
using (var zip = MonoAndroidHelper.ReadZipFile (finfo.FullName)) {
using (var stream = reszip.GetResourceStream ())
using (var zip = Xamarin.Tools.Zip.ZipArchive.Open (stream)) {
try {
updated |= Files.ExtractAll (zip, importsDir, modifyCallback: (entryFullName) => {
return entryFullName
Expand Down Expand Up @@ -374,8 +359,6 @@ void Extract (
}
if (Directory.Exists (assemblyDir))
resolvedAssetDirectories.Add (assemblyDir);

finfo.Delete ();
}
}

Expand Down

0 comments on commit aeda0e2

Please sign in to comment.