This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Fix libstdc++/16401
- From: Paolo Carlini <pcarlini at suse dot de>
- To: "'gcc-patches at gcc dot gnu dot org'" <gcc-patches at gcc dot gnu dot org>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Wed, 07 Jul 2004 14:10:28 +0200
- Subject: [v3] Fix libstdc++/16401
Hi,
the below fixes a serious performance regression in 3.4/mainline: when
I tweaked Nathan's rewrite of stringbuf::overflow to spare a copy, I
forgot, my bad, that now __tmp was a *new* string and I couldn't rely
on it growing automatically exponentially from the old _M_string size.
Tested x86-linux.
Paolo.
//////////////
2004-07-07 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/16401
* include/bits/sstream.tcc (overflow): When reallocating _M_string
use an exponential grow policy.
* testsuite/27_io/basic_stringbuf/overflow/char/1.cc: New.
* testsuite/performance/27_io/stringbuf_overflow.cc: New.
diff -prN libstdc++-v3-orig/include/bits/sstream.tcc libstdc++-v3/include/bits/sstream.tcc
*** libstdc++-v3-orig/include/bits/sstream.tcc Sun Feb 8 05:46:41 2004
--- libstdc++-v3/include/bits/sstream.tcc Wed Jul 7 13:07:50 2004
*************** namespace std
*** 83,105 ****
if (__builtin_expect(__testeof, false))
return traits_type::not_eof(__c);
! // NB: Start ostringstream buffers at 512 chars. This is an
! // experimental value (pronounced "arbitrary" in some of the
! // hipper english-speaking countries), and can be changed to
! // suit particular needs.
! const __size_type __len = std::max(__size_type(_M_string.capacity() + 1),
! __size_type(512));
const bool __testput = this->pptr() < this->epptr();
! if (__builtin_expect(!__testput && __len > _M_string.max_size(), false))
return traits_type::eof();
// Try to append __c into output sequence in one of two ways.
// Order these tests done in is unspecified by the standard.
if (!__testput)
{
! // In virtue of DR 169 (TC) we are allowed to grow more than
! // one char. That's easy to implement thanks to the exponential
! // growth policy builtin into basic_string.
__string_type __tmp;
__tmp.reserve(__len);
__tmp.assign(_M_string.data(), this->epptr() - this->pbase());
--- 83,107 ----
if (__builtin_expect(__testeof, false))
return traits_type::not_eof(__c);
! const __size_type __capacity = _M_string.capacity();
! const __size_type __max_size = _M_string.max_size();
const bool __testput = this->pptr() < this->epptr();
! if (__builtin_expect(!__testput && __capacity == __max_size, false))
return traits_type::eof();
// Try to append __c into output sequence in one of two ways.
// Order these tests done in is unspecified by the standard.
if (!__testput)
{
! // NB: Start ostringstream buffers at 512 chars. This is an
! // experimental value (pronounced "arbitrary" in some of the
! // hipper english-speaking countries), and can be changed to
! // suit particular needs.
! // Then, in virtue of DR 169 (TC) we are allowed to grow more
! // than one char.
! const __size_type __opt_len = std::max(__size_type(2 * __capacity),
! __size_type(512));
! const __size_type __len = std::min(__opt_len, __max_size);
__string_type __tmp;
__tmp.reserve(__len);
__tmp.assign(_M_string.data(), this->epptr() - this->pbase());
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/overflow/char/1.cc libstdc++-v3/testsuite/27_io/basic_stringbuf/overflow/char/1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/overflow/char/1.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_stringbuf/overflow/char/1.cc Wed Jul 7 13:31:03 2004
***************
*** 0 ****
--- 1,57 ----
+ // 2004-07-07 Paolo Carlini <pcarlini@suse.de>
+
+ // Copyright (C) 2004 Free Software Foundation, Inc.
+ //
+ // This file is part of the GNU ISO C++ Library. This library is free
+ // software; you can redistribute it and/or modify it under the
+ // terms of the GNU General Public License as published by the
+ // Free Software Foundation; either version 2, or (at your option)
+ // any later version.
+
+ // This library is distributed in the hope that it will be useful,
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ // GNU General Public License for more details.
+
+ // You should have received a copy of the GNU General Public License along
+ // with this library; see the file COPYING. If not, write to the Free
+ // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ // USA.
+
+ // 27.7.1.3 basic_stringbuf overridden virtual functions.
+
+ #include <sstream>
+ #include <cstdlib>
+ #include <testsuite_hooks.h>
+
+ using namespace std;
+
+ string
+ data(long len)
+ {
+ string ret;
+ for (long i = 0; i < len; ++i)
+ ret.push_back('a' + rand() % 26);
+ return ret;
+ }
+
+ void
+ test01(int iter)
+ {
+ bool test __attribute__((unused)) = true;
+
+ for (unsigned n = 1; n <= iter; n *= 10)
+ {
+ const string str = data(n);
+ stringbuf sstr;
+ for (unsigned i = 0; i < n; ++i)
+ sstr.sputc(str[i]);
+ VERIFY( str == sstr.str() );
+ }
+ }
+
+ int main()
+ {
+ test01(10000000);
+ return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/performance/27_io/stringbuf_overflow.cc libstdc++-v3/testsuite/performance/27_io/stringbuf_overflow.cc
*** libstdc++-v3-orig/testsuite/performance/27_io/stringbuf_overflow.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/performance/27_io/stringbuf_overflow.cc Wed Jul 7 13:05:10 2004
***************
*** 0 ****
--- 1,60 ----
+ // Copyright (C) 2004 Free Software Foundation, Inc.
+ //
+ // This file is part of the GNU ISO C++ Library. This library is free
+ // software; you can redistribute it and/or modify it under the
+ // terms of the GNU General Public License as published by the
+ // Free Software Foundation; either version 2, or (at your option)
+ // any later version.
+
+ // This library is distributed in the hope that it will be useful,
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ // GNU General Public License for more details.
+
+ // You should have received a copy of the GNU General Public License along
+ // with this library; see the file COPYING. If not, write to the Free
+ // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ // USA.
+
+ // As a special exception, you may use this file as part of a free software
+ // library without restriction. Specifically, if other files instantiate
+ // templates or use macros or inline functions from this file, or you compile
+ // this file and link it with other files to produce an executable, this
+ // file does not by itself cause the resulting executable to be covered by
+ // the GNU General Public License. This exception does not however
+ // invalidate any other reasons why the executable file might be covered by
+ // the GNU General Public License.
+
+ #include <sstream>
+ #include <testsuite_performance.h>
+
+ // libstdc++/16401 ostringstream in gcc 3.4.x very slow for big data
+ void test01()
+ {
+ using namespace std;
+ using namespace __gnu_test;
+
+ time_counter time;
+ resource_counter resource;
+
+ for(unsigned n = 10000; n <= 10000000; n *= 10)
+ {
+ ostringstream oss;
+ oss << "size = " << n;
+
+ ostringstream str;
+ start_counters(time, resource);
+ for(unsigned i = 0; i < n; ++i)
+ str << 'a';
+ stop_counters(time, resource);
+
+ report_performance(__FILE__, oss.str(), time, resource);
+ clear_counters(time, resource);
+ }
+ }
+
+ int main()
+ {
+ test01();
+ return 0;
+ }