2009-02-26 Zoltan Varga <vargaz@gmail.com>
authorZoltan Varga <vargaz@gmail.com>
Thu, 26 Feb 2009 16:32:52 +0000 (16:32 -0000)
committerZoltan Varga <vargaz@gmail.com>
Thu, 26 Feb 2009 16:32:52 +0000 (16:32 -0000)
* String.cs (FormatHelper): If all the arguments are strings, compute an
initial length for the stringbuilder to avoid reallocations.

svn path=/trunk/mcs/; revision=128114

mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/String.cs

index 992283e8e5eea29497f62bdd683e5fc6e001b92e..dcb417455146a31e65cfaac663baceb22ec7ff0b 100644 (file)
@@ -1,5 +1,8 @@
 2009-02-26  Zoltan Varga  <vargaz@gmail.com>
 
+       * String.cs (FormatHelper): If all the arguments are strings, compute an
+       initial length for the stringbuilder to avoid reallocations.
+
        * MonoType.cs (FullName): Cache the result of this in a field, as
        computing it is expensive.
 
index c88ae12c91d9074c672b514332baefd8f3aa93f1..b1b1ae56804fa482b099812b85808e25c856dbea 100644 (file)
@@ -1853,18 +1853,35 @@ namespace System
        
                public static string Format (IFormatProvider provider, string format, params object[] args)
                {
-                       StringBuilder b = new StringBuilder ();
-                       FormatHelper (b, provider, format, args);
+                       StringBuilder b = FormatHelper (null, provider, format, args);
                        return b.ToString ();
                }
                
-               internal static void FormatHelper (StringBuilder result, IFormatProvider provider, string format, params object[] args)
+               internal static StringBuilder FormatHelper (StringBuilder result, IFormatProvider provider, string format, params object[] args)
                {
                        if (format == null)
                                throw new ArgumentNullException ("format");
                        if (args == null)
                                throw new ArgumentNullException ("args");
 
+                       if (result == null) {
+                               /* Try to approximate the size of result to avoid reallocations */
+                               int i, len;
+
+                               len = 0;
+                               for (i = 0; i < args.Length; ++i) {
+                                       string s = args [i] as string;
+                                       if (s != null)
+                                               len += s.length;
+                                       else
+                                               break;
+                               }
+                               if (i == args.Length)
+                                       result = new StringBuilder (len + format.length);
+                               else
+                                       result = new StringBuilder ();
+                       }
+
                        int ptr = 0;
                        int start = ptr;
                        while (ptr < format.length) {
@@ -1939,6 +1956,8 @@ namespace System
 
                        if (start < format.length)
                                result.Append (format, start, format.Length - start);
+
+                       return result;
                }
 
                public unsafe static String Copy (String str)