Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 May 2004 14:47:31 +0100
From:      "Jonathan Wakely" <jwakely@mintel.com>
To:        FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Subject:   Re: misc/66941: Unacceptable stringstream performance
Message-ID:  <80256E9A.004BF36B.00@notes03.mintel.co.uk>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]

This is the patch we've been using:

(See attached file: sstream.patch)

[-- Attachment #2 --]
--- /usr/include/g++/sstream.orig	Thu Feb 12 05:52:21 2004
+++ /usr/include/g++/sstream	Thu May 20 10:28:03 2004
@@ -45,7 +45,7 @@
     explicit
     stringbuf(int which=ios::in|ios::out)
       : streambuf(), mode(static_cast<ios::open_mode>(which)),
-	stream(NULL), stream_len(0)
+	stream(NULL), stream_len(0), stream_capacity(0)
     {
       stringbuf_init();
     }
@@ -53,7 +53,7 @@
     explicit
     stringbuf(const string &str, int which=ios::in|ios::out)
       : streambuf(), mode(static_cast<ios::open_mode>(which)),
-	stream(NULL), stream_len(0)
+	stream(NULL), stream_len(0), stream_capacity(0)
     {
       if (mode & (ios::in|ios::out))
 	{
@@ -83,8 +83,8 @@
     str(const string& str)
     {
       delete[] stream;
-      stream_len = str.size();
-      stream = new char_type[stream_len];
+      stream_capacity = stream_len = str.size();
+      stream = new char_type[stream_capacity];
       str.copy(stream, stream_len);
       stringbuf_init();
     }
@@ -105,12 +105,15 @@
 	{
 	  if (c != EOF)
 	    {
-	      streamsize old_stream_len = stream_len;
+	      if (stream_len >= stream_capacity)
+		{
+		  stream_capacity = 2 * stream_len + 100;
+		  char_type* new_stream = new char_type[stream_capacity];
+		  memcpy(new_stream, stream, stream_len);
+		  delete[] stream;
+		  stream = new_stream;
+		}
 	      stream_len += 1;
-	      char_type* new_stream = new char_type[stream_len];
-	      memcpy(new_stream, stream, old_stream_len);
-	      delete[] stream;
-	      stream = new_stream;
 	      stringbuf_sync(gptr()-eback(), pptr()-pbase());
 	      sputc(c);
 	      res = c;
@@ -225,6 +228,7 @@
     ios::open_mode	mode;
     char_type*		stream;
     streamsize		stream_len;
+    streamsize		stream_capacity;
   };
 
   class istringstream : public istream {

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?80256E9A.004BF36B.00>