From: Marek Safar Date: Wed, 6 Mar 2013 10:16:29 +0000 (+0100) Subject: Optimize Activator path used by new() constraint to run less code X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=8deca1782e2f0c51072acd5465870a1913bd7c45;p=mono.git Optimize Activator path used by new() constraint to run less code --- diff --git a/mcs/class/corlib/System.Reflection/MonoMethod.cs b/mcs/class/corlib/System.Reflection/MonoMethod.cs index 94cbd7658d0..f46c01a226d 100644 --- a/mcs/class/corlib/System.Reflection/MonoMethod.cs +++ b/mcs/class/corlib/System.Reflection/MonoMethod.cs @@ -540,7 +540,12 @@ namespace System.Reflection { throw new MemberAccessException (String.Format ("Cannot create an instance of {0} because it is an abstract class", DeclaringType)); } - Exception exc = null; + return InternalInvoke (obj, parameters); + } + + public object InternalInvoke (object obj, object[] parameters) + { + Exception exc; object o = null; try { @@ -555,7 +560,8 @@ namespace System.Reflection { if (exc != null) throw exc; - return (obj == null) ? o : null; + + return obj == null ? o : null; } [DebuggerHidden] diff --git a/mcs/class/corlib/System/Activator.cs b/mcs/class/corlib/System/Activator.cs index 9e999435e17..1828f310bc0 100644 --- a/mcs/class/corlib/System/Activator.cs +++ b/mcs/class/corlib/System/Activator.cs @@ -4,10 +4,12 @@ // Authors: // Nick Drochak II (ndrochak@gol.com) // Gonzalo Paniagua (gonzalo@ximian.com) +// Marek Safar (marek.safar@gmail.com) // // (C) 2001 Nick Drochak II // (c) 2002 Ximian, Inc. (http://www.ximian.com) // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com) +// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -325,7 +327,7 @@ namespace System type.FullName + ".")); } - return ctor.Invoke (null); + return ctor.InternalInvoke (null, null); } private static void CheckType (Type type) diff --git a/mcs/class/corlib/System/MonoType.cs b/mcs/class/corlib/System/MonoType.cs index fa18e2dd4d1..31736e78172 100644 --- a/mcs/class/corlib/System/MonoType.cs +++ b/mcs/class/corlib/System/MonoType.cs @@ -6,9 +6,11 @@ // Paolo Molaro (lupus@ximian.com) // Patrik Torstensson (patrik.torstensson@labs2.com) // Gonzalo Paniagua (gonzalo@ximian.com) +// Marek Safar (marek.safar@gmail.com) // // (c) 2001-2003 Ximian, Inc. // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com) +// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -44,12 +46,12 @@ namespace System [StructLayout (LayoutKind.Sequential)] internal class MonoTypeInfo { public string full_name; - public ConstructorInfo default_ctor; + public MonoCMethod default_ctor; } [Serializable] [StructLayout (LayoutKind.Sequential)] - internal class MonoType : Type, ISerializable + sealed class MonoType : Type, ISerializable { [NonSerialized] MonoTypeInfo type_info; @@ -68,15 +70,24 @@ namespace System [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern TypeAttributes get_attributes (Type type); - internal ConstructorInfo GetDefaultConstructor () { - ConstructorInfo ctor = null; + public MonoCMethod GetDefaultConstructor () + { + MonoCMethod ctor = null; if (type_info == null) type_info = new MonoTypeInfo (); - if ((ctor = type_info.default_ctor) == null) { - const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic; - - ctor = type_info.default_ctor = GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null); + else + ctor = type_info.default_ctor; + + if (ctor == null) { + var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); + + for (int i = 0; i < ctors.Length; ++i) { + if (ctors [0].GetParametersCount () == 0) { + type_info.default_ctor = ctor = (MonoCMethod) ctors [i]; + break; + } + } } return ctor;