Special-case 1 module per assembly in Assembly.GetTypes/DefinedTypes (#22825)
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.

src/System.Private.CoreLib/shared/System/Reflection/Assembly.cs
src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

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][];
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++)
                 {