Optimize Activator path used by new() constraint to run less code
authorMarek Safar <marek.safar@gmail.com>
Wed, 6 Mar 2013 10:16:29 +0000 (11:16 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 6 Mar 2013 10:18:04 +0000 (11:18 +0100)
mcs/class/corlib/System.Reflection/MonoMethod.cs
mcs/class/corlib/System/Activator.cs
mcs/class/corlib/System/MonoType.cs

index 94cbd7658d099963374aee3587f299a4037bcb58..f46c01a226d083de05a8f7cb9a741f2511e56347 100644 (file)
@@ -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]
index 9e999435e17c11c64b5b4fc759b66bf946d0e922..1828f310bc05503cba23a10c42baea68cf2620c3 100644 (file)
@@ -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)
index fa18e2dd4d15de85ac74736db87dc427f976dfb5..31736e781725c0cf7e237380b753dfe1bae5b78a 100644 (file)
@@ -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;