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

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 19 Jan 2004 13:28:31 -0500 (EST)


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 vargaz@freemail.hu.

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

--- shadow/49009	2004-01-19 13:28:31.000000000 -0500
+++ shadow/49009.tmp.7855	2004-01-19 13:28:31.000000000 -0500
@@ -0,0 +1,51 @@
+Bug#: 49009
+Product: Mono/Class Libraries
+Version: unspecified
+OS: All
+OS Details: 
+Status: RESOLVED   
+Resolution: FIXED
+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.
+
+------- Additional Comments From vargaz@freemail.hu  2004-01-19 13:28 -------
+Fixed by totte's changes.