Skip to content

Commit

Permalink
[Java.Interop.Tools.Cecil] DirectoryAssemblyResolver & File.Exists() (#…
Browse files Browse the repository at this point in the history
…1065)

`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 `DirectoryAssemblyResolver.Load()`, 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/or `DirectoryNotFoundException` and
`return null` appropriately.

For `DirectoryAssemblyResolver.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.
  • Loading branch information
jonathanpeppers committed Dec 8, 2022
1 parent 8ab9d33 commit c2daa9f
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ public bool AddToCache (AssemblyDefinition assembly)

public virtual AssemblyDefinition? Load (string fileName, bool forceLoad = false)
{
if (!File.Exists (fileName))
return null;

AssemblyDefinition? assembly = null;
var name = Path.GetFileNameWithoutExtension (fileName);
if (!forceLoad && cache.TryGetValue (name, out assembly))
return assembly;

try {
assembly = ReadAssembly (fileName);
} catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) {
// These are ok, we can return null
return null;
} catch (Exception e) {
Diagnostic.Error (9, e, Localization.Resources.CecilResolver_XA0009, fileName);
}
Expand All @@ -144,8 +144,8 @@ public bool AddToCache (AssemblyDefinition assembly)
protected virtual AssemblyDefinition ReadAssembly (string file)
{
bool haveDebugSymbols = loadDebugSymbols &&
(File.Exists (file + ".mdb") ||
File.Exists (Path.ChangeExtension (file, ".pdb")));
(File.Exists (Path.ChangeExtension (file, ".pdb")) ||
File.Exists (file + ".mdb"));
var reader_parameters = new ReaderParameters () {
ApplyWindowsRuntimeProjections = loadReaderParameters.ApplyWindowsRuntimeProjections,
AssemblyResolver = this,
Expand Down

0 comments on commit c2daa9f

Please sign in to comment.