Skip to content

Commit c2daa9f

Browse files
[Java.Interop.Tools.Cecil] DirectoryAssemblyResolver & File.Exists() (#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.
1 parent 8ab9d33 commit c2daa9f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ public bool AddToCache (AssemblyDefinition assembly)
124124

125125
public virtual AssemblyDefinition? Load (string fileName, bool forceLoad = false)
126126
{
127-
if (!File.Exists (fileName))
128-
return null;
129-
130127
AssemblyDefinition? assembly = null;
131128
var name = Path.GetFileNameWithoutExtension (fileName);
132129
if (!forceLoad && cache.TryGetValue (name, out assembly))
133130
return assembly;
134131

135132
try {
136133
assembly = ReadAssembly (fileName);
134+
} catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) {
135+
// These are ok, we can return null
136+
return null;
137137
} catch (Exception e) {
138138
Diagnostic.Error (9, e, Localization.Resources.CecilResolver_XA0009, fileName);
139139
}
@@ -144,8 +144,8 @@ public bool AddToCache (AssemblyDefinition assembly)
144144
protected virtual AssemblyDefinition ReadAssembly (string file)
145145
{
146146
bool haveDebugSymbols = loadDebugSymbols &&
147-
(File.Exists (file + ".mdb") ||
148-
File.Exists (Path.ChangeExtension (file, ".pdb")));
147+
(File.Exists (Path.ChangeExtension (file, ".pdb")) ||
148+
File.Exists (file + ".mdb"));
149149
var reader_parameters = new ReaderParameters () {
150150
ApplyWindowsRuntimeProjections = loadReaderParameters.ApplyWindowsRuntimeProjections,
151151
AssemblyResolver = this,

0 commit comments

Comments
 (0)