Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java.Interop.Tools.Cecil] avoid File.Exists() check in DirectoryAssemblyResolver #1065

Merged

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Dec 7, 2022

dotnet-trace of a dotnet new maui app:

dotnet trace collect --format speedscope -- C:\src\xamarin-android\bin\Release\dotnet\dotnet.exe build -bl --no-restore bar.csproj

Shows some interesting time spent in:

179.53ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Load(class System.String,bool)
  7.89ms system.private.corelib.il!System.IO.File.Exists(class System.String)
171.63ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.ReadAssembly(class System.String)
 24.18ms system.private.corelib.il!System.IO.File.Exists(class System.String)

For LoadAssembly(), the common case is that the files always exist, and the rare case they would be missing. Instead of calling File.Exists() on every assembly, we can handle FileNotFoundException and return null appropriately.

For ReadAssembly() we can reorder the check for symbol files:

bool haveDebugSymbols = loadDebugSymbols &&
    (File.Exists (Path.ChangeExtension (file, ".pdb")) ||
    File.Exists (file + ".mdb"));

So we check for .pdb files first, which should more likely exist in modern projects. We could drop .mdb file checks at some point, when this code isn't shared with classic Xamarin.Android.

Testing the changes afterward:

167.57ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Load(class System.String,bool)
141.56ms xamarin.android.cecil!Mono.Cecil.AssemblyDefinition.ReadAssembly(class System.String,class Mono.Cecil.ReaderParameters)

There appears to be some variance in the timing, but my rough estimate is this saves about 15-20ms on incremental builds of dotnet new maui projects. Larger projects could potentially save more.

…ssemblyResolver`

`dotnet-trace` of a `dotnet new maui` app:

    dotnet trace collect --format speedscope -- C:\src\xamarin-android\bin\Release\dotnet\dotnet.exe build -bl --no-restore bar.csproj

Shows some interesting time spent in:

    179.53ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Load(class System.String,bool)
      7.89ms system.private.corelib.il!System.IO.File.Exists(class System.String)
    171.63ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.ReadAssembly(class System.String)
     24.18ms system.private.corelib.il!System.IO.File.Exists(class System.String)

For `LoadAssembly()`, the common case is that the files always exist,
and the rare case they would be missing. Instead of calling
`File.Exists()` on every assembly, we can handle
`FileNotFoundException` and `return null` appropriately.

For `ReadAssembly()` we can reorder the check for symbol files:

    bool haveDebugSymbols = loadDebugSymbols &&
        (File.Exists (Path.ChangeExtension (file, ".pdb")) ||
        File.Exists (file + ".mdb"));

So we check for `.pdb` files first, which should more likely exist in
modern projects. We could drop `.mdb` file checks at some point, when
this code isn't shared with classic Xamarin.Android.

Testing the changes afterward:

    167.57ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Load(class System.String,bool)
    141.56ms xamarin.android.cecil!Mono.Cecil.AssemblyDefinition.ReadAssembly(class System.String,class Mono.Cecil.ReaderParameters)

There appears to be some variance in the timing, but my rough estimate
is this saves about 15-20ms on incremental builds of `dotnet new maui`
projects. Larger projects could potentially save more.
@jonathanpeppers jonathanpeppers marked this pull request as draft December 8, 2022 20:54
@jonathanpeppers jonathanpeppers marked this pull request as ready for review December 8, 2022 22:15
@jonpryor jonpryor merged commit c2daa9f into xamarin:main Dec 8, 2022
@jonathanpeppers jonathanpeppers deleted the DirectoryAssemblyResolverFileExists branch December 8, 2022 23:38
jonpryor pushed a commit to xamarin/xamarin-android that referenced this pull request Dec 12, 2022
Changes: xamarin/java.interop@3a9f770...149d70f

  * xamarin/java.interop@149d70fe: [generator] Refactor enum writing to use SourceWriters (xamarin/java.interop#1063)
  * xamarin/java.interop@c2daa9f0: [Java.Interop.Tools.Cecil] DirectoryAssemblyResolver & File.Exists() (xamarin/java.interop#1065)
  * xamarin/java.interop@8ab9d33a: [Java.Interop.Tools.TypeNameMappings] improve `ToJniNameFromAttributesForAndroid` (xamarin/java.interop#1064)
  * xamarin/java.interop@09f8da26: [JavaCallableWrappers] avoid `string.Format()` (xamarin/java.interop#1061)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants