libstdc++/4960: std::string slow to append individual characters

martin@v.loewis.de martin@v.loewis.de
Sat Nov 17 05:22:00 GMT 2001


>Number:         4960
>Category:       libstdc++
>Synopsis:       std::string slow to append individual characters
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 27 15:06:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Martin v. Loewis
>Release:        3.1 20010813 (experimental)
>Organization:
HUB
>Environment:
System: Linux mira 2.4.10-4GB #1 Tue Sep 25 12:33:54 GMT 2001 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /usr/src/egcs/configure --prefix=/usr/local/newabi --enable-shared --enable-threads --enable-languages=c,c++,java
>Description:
In gcc 2.95, adding one character after another to an std::string resulted in infrequent
memory allocations, as the implementation would reserve sufficient memory. In libstdc++ v3,
each append invocation will cause reallocation (and copying of the existing string) once
the reserve is exhausted.
>How-To-Repeat:
Compile and benchmark the following program with both gcc 2.95 and gcc 3 on the same machine.
On a 900MHz Pentium III, the 2.95 compilation result needs 0.01s, the gcc 3 result needs 4.0s.

#include <string>
#include <stdio.h>

int main()
{
	std::string s;
	int i;
	for(i=0;i<100000;i++){
		//printf("%d %d\n",i,s.capacity());
		s+=" ";
	}
}
>Fix:
	Add a reserve larger than the current size in std::string::append.
        The strategy of libstdc++ 2 (using the nearest power of 2) seems reasonable.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list