This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 28df18987604c1685ac3d231737b0d4e2c30efae Author: Shad Storhaug <[email protected]> AuthorDate: Fri Sep 11 13:53:36 2020 +0700 PERFORMANCE: Lucene.Net.Support.AssemblyUtils: restructured to use IEnumerable<T> for deferred execution --- .../Util/LuceneTestCase.cs | 3 +- src/Lucene.Net/Support/AssemblyUtils.cs | 8 ++--- src/Lucene.Net/Util/SPIClassIterator.cs | 39 ++++++++++++---------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs index 06e03c5..d64b235 100644 --- a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs +++ b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs @@ -1269,8 +1269,7 @@ namespace Lucene.Net.Util return testClass; // 2nd attempt - try scanning the referenced assemblies to see if we can find the class by fullname - var referencedAssemblies = AssemblyUtils.GetReferencedAssemblies(); - testClass = referencedAssemblies.SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName)).FirstOrDefault(); + testClass = AssemblyUtils.GetReferencedAssemblies().SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName)).FirstOrDefault(); if (testClass != null) return testClass; #endif diff --git a/src/Lucene.Net/Support/AssemblyUtils.cs b/src/Lucene.Net/Support/AssemblyUtils.cs index e77b616..5beaab2 100644 --- a/src/Lucene.Net/Support/AssemblyUtils.cs +++ b/src/Lucene.Net/Support/AssemblyUtils.cs @@ -39,7 +39,7 @@ namespace Lucene.Net.Support /// Microsoft assemblies here. /// </summary> /// <returns></returns> - public static IList<Assembly> GetReferencedAssemblies() + public static IEnumerable<Assembly> GetReferencedAssemblies() { // .NET Port Hack: We do a 2-level deep check here because if the assembly you're // hoping would be loaded hasn't been loaded yet into the app domain, @@ -55,9 +55,9 @@ namespace Lucene.Net.Support .Distinct(); var assembliesLoaded = LoadAssemblyFromName(assemblyNames); #endif - assembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x)).ToArray(); + var nonFrameworkAssembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x)); - var referencedAssemblies = assembliesLoaded + var referencedAssemblies = nonFrameworkAssembliesLoaded .SelectMany(assembly => { return assembly @@ -68,7 +68,7 @@ namespace Lucene.Net.Support .Where(x => x != null) .Distinct(); - return assembliesLoaded.Concat(referencedAssemblies).Distinct().ToList(); + return nonFrameworkAssembliesLoaded.Concat(referencedAssemblies).Distinct(); } #if !FEATURE_APPDOMAIN_GETASSEMBLIES diff --git a/src/Lucene.Net/Util/SPIClassIterator.cs b/src/Lucene.Net/Util/SPIClassIterator.cs index f6c7e52..5439a51 100644 --- a/src/Lucene.Net/Util/SPIClassIterator.cs +++ b/src/Lucene.Net/Util/SPIClassIterator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using JCG = J2N.Collections.Generic; namespace Lucene.Net.Util @@ -32,29 +33,33 @@ namespace Lucene.Net.Util /// </summary> public class SPIClassIterator<S> : IEnumerable<Type> { - private static JCG.HashSet<Type> types = LoadTypes(); + private static readonly JCG.HashSet<Type> types = LoadTypes(); private static JCG.HashSet<Type> LoadTypes() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) { - types = new JCG.HashSet<Type>(); + var types = new JCG.HashSet<Type>(); var assembliesToExamine = Support.AssemblyUtils.GetReferencedAssemblies(); - // LUCENENET HACK: - // Tests such as TestImpersonation.cs expect that the assemblies - // are probed in a certain order. NamedSPILoader, lines 68 - 75 adds - // the first item it sees with that name. So if you have multiple - // codecs, it may not add the right one, depending on the order of - // the assemblies that were examined. - // This results in many test failures if Types from Lucene.Net.Codecs - // are examined and added to NamedSPILoader first before - // Lucene.Net.TestFramework. - var testFrameworkAssembly = assembliesToExamine.FirstOrDefault(x => string.Equals(x.GetName().Name, "Lucene.Net.TestFramework", StringComparison.Ordinal)); - if (testFrameworkAssembly != null) - { - assembliesToExamine.Remove(testFrameworkAssembly); - assembliesToExamine.Insert(0, testFrameworkAssembly); - } + // LUCENENET NOTE: The following hack is not required because we are using abstract factories + // and pure DI to ensure the order of the codecs are always correct during testing. + + //// LUCENENET HACK: + //// Tests such as TestImpersonation.cs expect that the assemblies + //// are probed in a certain order. NamedSPILoader, lines 68 - 75 adds + //// the first item it sees with that name. So if you have multiple + //// codecs, it may not add the right one, depending on the order of + //// the assemblies that were examined. + //// This results in many test failures if Types from Lucene.Net.Codecs + //// are examined and added to NamedSPILoader first before + //// Lucene.Net.TestFramework. + //var testFrameworkAssembly = assembliesToExamine.FirstOrDefault(x => string.Equals(x.GetName().Name, "Lucene.Net.TestFramework", StringComparison.Ordinal)); + //if (testFrameworkAssembly != null) + //{ + // //assembliesToExamine.Remove(testFrameworkAssembly); + // //assembliesToExamine.Insert(0, testFrameworkAssembly); + // assembliesToExamine = new Assembly[] { testFrameworkAssembly }.Concat(assembliesToExamine.Where(a => !testFrameworkAssembly.Equals(a))); + //} foreach (var assembly in assembliesToExamine) {
