Skip to content

Commit b57d770

Browse files
jonathanpeppersjonpryor
authored andcommitted
[jcwgen] add public method for GetDestinationPath (#402)
Context: dotnet/android#2505 (comment) Xamarin.Android is working to reduce the number of temporary files which are generated during the build, in order to improve build time. We would like `JavaCallableWrapperGenerator` to assist in that effort. Currently we are doing: // jti is a JavaCallableWrapperGenerator instance jti.Generate (outputPath); // Then later in our MSBuild task foreach (var file in Directory.GetFiles (temp, "*", SearchOption.AllDirectories)) { ... MonoAndroidHelper.CopyIfChanged (file, dest); } This is slower than it could be: 1. Writes to a bunch of temp files. 2. Recurses the directory where all these temp files exist. 3. Does a hash comparison of the contents of the source and destination. 4. Copies the file if needed. 5. Deletes all the temp files. Instead, we want to instead do: using (var memoryStream = new MemoryStream ()) using (var writer = new StreamWriter (memoryStream)) { jti.Generate (writer); writer.Flush (); var path = jti.GetDestinationPath (outputPath); MonoAndroidHelper.CopyIfStreamChanged (memoryStream, path); } This is more streamlined: 1. Generate the file in-memory. 2. Do a hash comparison of the in-memory file vs. the destination. 3. Write to the file if needed. The change we need in Java.Interop here is to add the `JavaCallableWrapperGenerator.GetDestinationPath()` method. Further changes downstream in xamarin-android will allow us reap the benefits. I also improved the code a bit, no longer using `string.Split()`. We can do a simple `string.Replace()` from `.` to `Path.DirectorySeparatorChar`, which allocates fewer strings.
1 parent 85be94f commit b57d770

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,15 +856,18 @@ void WriteInstrumentationOnCreate (TextWriter sw, Action<TextWriter> extra)
856856

857857
StreamWriter OpenStream (string outputPath)
858858
{
859-
string path = outputPath;
860-
foreach (string dir in package.Split ('.'))
861-
path = Path.Combine (path, dir);
862-
863-
if (!Directory.Exists (path))
864-
Directory.CreateDirectory (path);
859+
string destination = GetDestinationPath (outputPath);
860+
Directory.CreateDirectory (Path.GetDirectoryName (destination));
861+
return new StreamWriter (new FileStream (destination, FileMode.Create, FileAccess.Write));
862+
}
865863

866-
path = Path.Combine (path, name + ".java");
867-
return new StreamWriter (new FileStream (path, FileMode.Create, FileAccess.Write));
864+
/// <summary>
865+
/// Returns a destination file path based on the package name of this Java type
866+
/// </summary>
867+
public string GetDestinationPath (string outputPath)
868+
{
869+
var dir = package.Replace ('.', Path.DirectorySeparatorChar);
870+
return Path.Combine (outputPath, dir, name + ".java");
868871
}
869872
}
870873
}

0 commit comments

Comments
 (0)