Special-case 1 module per assembly in Assembly.GetTypes/DefinedTypes (dotnet/coreclr...
authorStephen Toub <stoub@microsoft.com>
Tue, 26 Feb 2019 03:17:19 +0000 (22:17 -0500)
committerGitHub <noreply@github.com>
Tue, 26 Feb 2019 03:17:19 +0000 (22:17 -0500)
It's very common for an assembly to have only one module, in which case we can avoid unnecessary allocations and copies in Assembly.GetTypes() and Assembly.DefinedTypes.

Commit migrated from https://github.com/dotnet/coreclr/commit/8509bbd72ed937a626fc436715b962d3f86fa380

src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs
src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs

index 3cc4b86..d670031 100644 (file)
@@ -175,9 +175,13 @@ namespace System.Reflection
         {
             get
             {
-                List<RuntimeType> rtTypes = new List<RuntimeType>();
-
                 RuntimeModule[] modules = GetModulesInternal(true, false);
+                if (modules.Length == 1)
+                {
+                    return modules[0].GetDefinedTypes();
+                }
+
+                List<RuntimeType> rtTypes = new List<RuntimeType>();
 
                 for (int i = 0; i < modules.Length; i++)
                 {
index bc8b4cd..8abaf89 100644 (file)
@@ -37,6 +37,10 @@ namespace System.Reflection
         public virtual Type[] GetTypes()
         {
             Module[] m = GetModules(false);
+            if (m.Length == 1)
+            {
+                return m[0].GetTypes();
+            }
 
             int finalLength = 0;
             Type[][] moduleTypes = new Type[m.Length][];