From e08a9a3fd57f8f20f58476c62b3ddd2dda1c26aa Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Tue, 18 Jan 2022 09:05:26 +0100 Subject: [PATCH] cache types resolution --- .../Utilities/MetadataReaderHelper.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.TestPlatform.Common/Utilities/MetadataReaderHelper.cs b/src/Microsoft.TestPlatform.Common/Utilities/MetadataReaderHelper.cs index ce4aa9945c..a71e56906b 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/MetadataReaderHelper.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/MetadataReaderHelper.cs @@ -42,16 +42,19 @@ public TestExtensionTypesV2Attribute(string extensionType, string extensionIdent internal class MetadataReaderExtensionsHelper { private const string TestExtensionTypesAttributeV2 = "Microsoft.VisualStudio.TestPlatform.TestExtensionTypesV2Attribute"; - private static readonly ConcurrentDictionary assemblyCache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary assemblyCache = new ConcurrentDictionary(); private static readonly Type[] emptyTypeArray = new Type[0]; public Type[] DiscoverTestExtensionTypesV2Attribute(Assembly loadedAssembly, string assemblyFilePath) + => assemblyCache.GetOrAdd(assemblyFilePath, DiscoverTestExtensionTypesV2AttributeInternal(loadedAssembly, assemblyFilePath)); + + private Type[] DiscoverTestExtensionTypesV2AttributeInternal(Assembly loadedAssembly, string assemblyFilePath) { EqtTrace.Verbose($"MetadataReaderExtensionsHelper: Discovering extensions inside assembly '{loadedAssembly.FullName}' file path '{assemblyFilePath}'"); #if !NETSTANDARD1_3 - // Cache assembly, in VS scenario vstest.console is not unloaded so we don't want to load same asm more times. - Assembly assemblyToAnalyze = assemblyCache.GetOrAdd(assemblyFilePath, Assembly.LoadFile(assemblyFilePath)); + // We don't cache the load because this method is used by DiscoverTestExtensionTypesV2Attribute that caches the outcome Type[] + Assembly assemblyToAnalyze = Assembly.LoadFile(assemblyFilePath); #else Assembly assemblyToAnalyze = loadedAssembly; #endif