From: Stephen Toub Date: Tue, 26 Feb 2019 03:17:19 +0000 (-0500) Subject: Special-case 1 module per assembly in Assembly.GetTypes/DefinedTypes (dotnet/coreclr... X-Git-Tag: submit/tizen/20210909.063632~11030^2~2385 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d96c86ab3f96fba01789a649bafdf8e2992b9e29;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Special-case 1 module per assembly in Assembly.GetTypes/DefinedTypes (dotnet/coreclr#22825) 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 --- diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index 3cc4b86..d670031 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -175,9 +175,13 @@ namespace System.Reflection { get { - List rtTypes = new List(); - RuntimeModule[] modules = GetModulesInternal(true, false); + if (modules.Length == 1) + { + return modules[0].GetDefinedTypes(); + } + + List rtTypes = new List(); for (int i = 0; i < modules.Length; i++) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index bc8b4cd..8abaf89 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -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][];