[Mono-bugs] [Bug 49009][Wis] New - StringBuilder allocates twice as much memory as necessary

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sun, 28 Sep 2003 23:19:57 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by bmaurer@users.sf.net.

http://bugzilla.ximian.com/show_bug.cgi?id=49009

--- shadow/49009	2003-09-28 23:19:57.000000000 -0400
+++ shadow/49009.tmp.30706	2003-09-28 23:19:57.000000000 -0400
@@ -0,0 +1,48 @@
+Bug#: 49009
+Product: Mono/Class Libraries
+Version: unspecified
+OS: All
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: Unknown
+Priority: Wishlist
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: bmaurer@users.sf.net               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: StringBuilder allocates twice as much memory as necessary
+
+Description of Problem:
+Due to the way our StringBuilder is designed, we first allocate a char [],
+which is later passed on to the string .ctor. Thus, a new string is
+allocated. Idealy, however, we should introduct the concept of a "mutuable
+string" into the string class, for the sole purpose of use inside
+stringbuilder. We could then return this buffer, allocating another one
+only if the string is modified again. In the most common case (string
+builder is allocated, appened to, .ToString () is called, and the sb is
+never used again), we would half the memory used.
+
+Steps to reproduce the problem:
+
+for (int i = 0; i < 50000; i++) {
+   StringBuilder sb = new StringBuilder ();
+   sb.Append ("hello");
+   sb.Append (" world!");
+   sb.ToString ();
+}
+
+Actual Results:
+50000 char []s and 50000 strings are allocated
+
+Expected Results:
+50000 strings are allocated
+
+How often does this happen? 
+always
+
+Additional Information:
+both pnet and Microsoft implement StringBuilder in the way described.