re PR libstdc++/16401 (ostringstream in gcc 3.4.x very slow for big data)

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.

From-SVN: r84235
This commit is contained in:
Paolo Carlini 2004-07-07 21:48:01 +00:00 committed by Paolo Carlini
parent f4f58d9486
commit 9f9000d177
4 changed files with 137 additions and 10 deletions

View File

@ -1,3 +1,11 @@
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.
2004-07-06 Anssi Hannula <anssi.hannula@mbnet.fi>
PR libstdc++/15928

View File

@ -83,23 +83,25 @@ namespace std
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 __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 && __len > _M_string.max_size(), false))
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)
{
// 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.
// 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());

View File

@ -0,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(unsigned len)
{
string ret;
for (unsigned i = 0; i < len; ++i)
ret.push_back('a' + rand() % 26);
return ret;
}
void
test01(unsigned 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;
}

View File

@ -0,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;
}