From e09375d6bdd1e97d586b7d5017c132de6b9a2a30 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sun, 3 Nov 2019 18:28:32 +0100 Subject: [PATCH] [netcore] Improve default constructor lookup, (mono/mono#17666) * [netcore] Improve default constructor lookup, cache when no default constructor is found (common for struct) * Address PR feedback Commit migrated from https://github.com/mono/mono/commit/8131518bb12a059ffc71fa6c6030b103b5c6d35b --- .../src/System/RuntimeType.Mono.cs | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs index faa2a31..0f58b5d 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs @@ -1863,6 +1863,7 @@ namespace System // ,+*&*[]\ in the identifier portions of the names // have been escaped with a leading backslash (\) public string full_name; + public bool default_ctor_cached; public RuntimeConstructorInfo default_ctor; } @@ -1875,18 +1876,21 @@ namespace System internal RuntimeConstructorInfo GetDefaultConstructor () { var cache = Cache; - RuntimeConstructorInfo ctor = cache.default_ctor; + RuntimeConstructorInfo ctor = null; - if (ctor == null) { - var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); + if (Volatile.Read (ref cache.default_ctor_cached)) + return cache.default_ctor; - for (int i = 0; i < ctors.Length; ++i) { - if (ctors [i].GetParametersCount () == 0) { - cache.default_ctor = ctor = (RuntimeConstructorInfo) ctors [i]; - break; - } - } - } + var ctors = GetConstructorCandidates ( + null, + BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, CallingConventions.Any, + Array.Empty (), false); + + if (ctors.Count == 1) + cache.default_ctor = ctor = (RuntimeConstructorInfo) ctors [0]; + + // Note down even if we found no constructors + Volatile.Write (ref cache.default_ctor_cached, true); return ctor; } -- 2.7.4