Skip to content

Commit

Permalink
Add simple optimization by trying last successful strategy first
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Feb 27, 2024
1 parent 03e5224 commit 579c85b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ public override Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyNa
{
string candidate = Path.Combine(runtimeDir, assemblyName.Name + ".dll");
if (File.Exists(candidate))
{
log.Debug($"{RuntimeName} Resolved to {candidate}");
return LoadContext.LoadFromAssemblyPath(candidate);
}
}

log.Debug($"{RuntimeName} Failed!");
return null;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/TestCentric.Agent.Core/Resolvers/ResolutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#if NETCOREAPP3_1_OR_GREATER

using System;
using System.Reflection;
using System.Runtime.Loader;
using TestCentric.Engine.Internal;
Expand All @@ -13,6 +14,8 @@ namespace TestCentric.Engine.Internal
{
public abstract class ResolutionStrategy
{
private int _totalCalls;

internal TestAssemblyLoadContext LoadContext { get; }
internal string TestAssemblyPath { get; }

Expand All @@ -25,11 +28,21 @@ internal ResolutionStrategy(TestAssemblyResolver resolver)

public bool TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName, out Assembly loadedAssembly)
{
_totalCalls++;

loadedAssembly = TryLoadAssembly(context, assemblyName);
return loadedAssembly != null;
}

public abstract Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName);

public void WriteReport()
{
Console.WriteLine();
Console.WriteLine(GetType().Name);
Console.WriteLine();
Console.WriteLine($"Total Calls: {_totalCalls}");
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace TestCentric.Engine.Internal
{
internal class RuntimeLibraryResolutionStrategy : ResolutionStrategy
{
private static readonly Logger log = InternalTrace.GetLogger(nameof(AdditionalRuntimesResolutionStrategy));
private static readonly Logger log = InternalTrace.GetLogger(nameof(RuntimeLibraryResolutionStrategy));

private List<CompilationLibrary> _libraries = new List<CompilationLibrary>();

Expand Down Expand Up @@ -54,10 +54,14 @@ public override Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyNa
foreach (var assemblyPath in assemblies)
{
if (assemblyName.Name == Path.GetFileNameWithoutExtension(assemblyPath))
{
log.Debug($"Resolved to {assemblyPath}");
return LoadContext.LoadFromAssemblyPath(assemblyPath);
}
}
}

log.Debug("Failed!");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ internal sealed class TestAssemblyLoadContext : AssemblyLoadContext

internal readonly string _testAssemblyPath;
private readonly string _basePath;
private readonly TestAssemblyResolver _resolver;

public TestAssemblyResolver Resolver { get; }

public TestAssemblyLoadContext(string testAssemblyPath)
{
_testAssemblyPath = testAssemblyPath;
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
_basePath = Path.GetDirectoryName(testAssemblyPath);

Resolver = new TestAssemblyResolver(this, testAssemblyPath);
}

protected override Assembly Load(AssemblyName name)
Expand Down
18 changes: 17 additions & 1 deletion src/TestCentric.Agent.Core/Resolvers/TestAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -57,9 +58,24 @@ private Assembly OnResolving(AssemblyLoadContext context, AssemblyName assemblyN
{
log.Info($"Resolving {assemblyName}");

foreach(var strategy in ResolverStrategies)
for (int index = 0; index < ResolverStrategies.Count; index++)
{
var strategy = ResolverStrategies[index];

if (strategy.TryLoadAssembly(context, assemblyName, out var loadedAssembly))
{
if (index > 0)
{
// Simplistic approach to favoring the strategy that succeeds
// by moving it to the start of the list. This is safe because
// we are about to return from the method, exiting the loop.
ResolverStrategies.RemoveAt(index);
ResolverStrategies.Insert(0, strategy);
}

return loadedAssembly;
}
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public override Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyNa
{
var fileName = Path.GetFileNameWithoutExtension(assemblyPath);
if (string.Equals(fileName, assemblyName.Name, StringComparison.InvariantCultureIgnoreCase) && File.Exists(assemblyPath))
{
log.Debug($"Resolved to {assemblyPath}");
return context.LoadFromAssemblyPath(assemblyPath);
}
}

log.Debug("Failed!");
return null;
}
}
Expand Down

0 comments on commit 579c85b

Please sign in to comment.