Delay expensive inflator construction for type parameter constraints
authorMarek Safar <marek.safar@gmail.com>
Tue, 26 Oct 2010 15:45:15 +0000 (16:45 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 27 Oct 2010 07:40:43 +0000 (08:40 +0100)
mcs/mcs/generic.cs

index 87d045a3d2d8a8e301c914ce7f4aa3061eeb1b75..9001269408b8a23e487e0ece24594ed3707a686f 100644 (file)
@@ -923,11 +923,17 @@ namespace Mono.CSharp {
                        }
 
                        return true;
+               }\r
+\r
+               public static TypeParameterSpec[] InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec[] tparams)\r
+               {\r
+                       return InflateConstraints (tparams, l => l, inflator);\r
                }
 
-               public static TypeParameterSpec[] InflateConstraints (TypeParameterInflator inflator, TypeParameterSpec[] tparams)
+               public static TypeParameterSpec[] InflateConstraints<T> (TypeParameterSpec[] tparams, Func<T, TypeParameterInflator> inflatorFactory, T arg)
                {
-                       TypeParameterSpec[] constraints = null;
+                       TypeParameterSpec[] constraints = null;\r
+                       TypeParameterInflator? inflator = null;
 
                        for (int i = 0; i < tparams.Length; ++i) {
                                var tp = tparams[i];
@@ -935,9 +941,15 @@ namespace Mono.CSharp {
                                        if (constraints == null) {
                                                constraints = new TypeParameterSpec[tparams.Length];
                                                Array.Copy (tparams, constraints, constraints.Length);
-                                       }
+                                       }\r
+\r
+                                       //\r
+                                       // Using a factory to avoid possibly expensive inflator build up\r
+                                       //\r
+                                       if (inflator == null)\r
+                                               inflator = inflatorFactory (arg);
 
-                                       constraints[i] = (TypeParameterSpec) constraints[i].InflateMember (inflator);
+                                       constraints[i] = (TypeParameterSpec) constraints[i].InflateMember (inflator.Value);
                                }
                        }
 
@@ -1294,9 +1306,8 @@ namespace Mono.CSharp {
                //
                public TypeParameterSpec[] Constraints {
                        get {
-                               if (constraints == null) {
-                                       var inflator = CreateLocalInflator ();
-                                       constraints = TypeParameterSpec.InflateConstraints (inflator, MemberDefinition.TypeParameters);
+                               if (constraints == null) {\r
+                                       constraints = TypeParameterSpec.InflateConstraints (MemberDefinition.TypeParameters, l => l.CreateLocalInflator (), this);
                                }
 
                                return constraints;