Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] perf improvements for LlvmIrGenerator (#…
Browse files Browse the repository at this point in the history
…7626)

Reviewing `dotnet trace` output for a `dotnet new maui` app:

    dotnet trace collect --format speedscope -- .\bin\Release\dotnet\dotnet.exe build -bl --no-restore foo.csproj

21% of the time in `<GenerateJavaStubs/>` is spent in:

    249.60ms xamarin.android.build.tasks!Xamarin.Android.Tasks.LLVMIR.LlvmIrComposer.Write(value class Xamarin.Android.Tools.Andro...

Drilling down, some of the hot spots are:

    68.64ms xamarin.android.build.tasks!Xamarin.Android.Tasks.LLVMIR.LlvmIrGenerator.WriteString(class System.String,class System.St
    40.39ms xamarin.android.build.tasks!Xamarin.Android.Tasks.LLVMIR.LlvmIrGenerator.WritePointer(class Xamarin.Android.Tasks.LLVMIR

Which seem to be places

The types of things I solved so far:

* I audited every `$""` to see if we could call `TextWriter.Write()`
  or `StringBuilder.Append()` multiple times to avoid creating a
  string.

* Use `ArrayPool` in combination with `Encoding.GetBytes()`.

* Use `char` overloads, where we were using a 1-length `string`.

Hopefully, I've changed no behavior at all here, and we've just saved
a lot of `string` and `byte[]` allocations.

After these changes go in, we can probably profile and find other
areas to improve.

Testing the same `dotnet new maui` app, the overall time is now:

    190.97ms xamarin.android.build.tasks!Xamarin.Android.Tasks.LLVMIR.LlvmIrComposer.Write(value class Xamarin.Android.Tools.AndroidT

I think this will save ~60ms on incremental builds of the .NET MAUI
project template. Larger projects with more Java subclasses could save
even more.

Other changes:

* Added comments where `$""` was more readable before

* Fix new usage of System.Buffers in tests

Classic tests were failing with `unable to load assembly
System.Buffers.dll` on Windows, because they used the
`GeneratePackageManagerJava` MSBuild task directly from C# in a .NET
framework test project.

To fix this, I brought over an assembly-binding redirect we already
had in xabuild's `App.config`.
  • Loading branch information
jonathanpeppers authored Dec 13, 2022
1 parent 43118f9 commit 0f7a150
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="4.1.4.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
Expand Down
Loading

0 comments on commit 0f7a150

Please sign in to comment.