Issue Details (XML | Word | Printable)

Key: STDCXX-491
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Martin Sebor
Reporter: Mark Brown
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
C++ Standard Library

string::push_back() slow

Created: 22/Jul/07 08:26 PM   Updated: 07/Sep/07 02:34 PM
Return to search
Component/s: 21. Strings
Affects Version/s: 4.1.3
Fix Version/s: 4.2.0

Time Tracking:
Not Specified

File Attachments: None
Image Attachments:

1. push_back.png
(9 kB)
Environment: gcc 4.1.2, Linux/x86_64
Issue Links:
Reference
 

Resolved: 06/Aug/07 03:27 AM
Resolution Date: 06/Aug/07 03:27 AM


 Description  « Hide
According to my timings string::push_back() in stdcxx 4.1.3 is more than twice as slow than the same function in gcc 4.1.2 on Linux x86_64:

$ time ./push_back-stdcxx 100000000

real 0m2.175s
user 0m2.004s
sys 0m0.172s

$ time ./push_back-gcc 100000000

real 0m0.924s
user 0m0.760s
sys 0m0.164s

#include <cassert>
#include <cstdlib>
#include <string>

int main (int argc, char *argv[])
{
const int N = argc < 2 ? 1 : std::atoi (argv [1]);

std::string str;

for (int i = 0; i < N; ++i)
str.push_back ('x');

assert (str.size () == std::size_t (N));
}

Comparing the generated assembly, the gcc push_back() is mostly inline but the stdcxx push_back() is not:

stdcxx:
_Z8pushbackRSsc:
.LFB449:
movq (%rdi), %rax
movl %esi, %edx
movl $1, %ecx
movsbl %dl,%r8d
xorl %edx, %edx
movq -8(%rax), %rsi
jmp _ZNSs7replaceEmmmc

gcc:
_Z8pushbackRSsc:
.LFB904:
movq %rbp, -16(%rsp)
.LCFI0:
movq %r12, -8(%rsp)
.LCFI1:
movq %rdi, %rbp
movq %rbx, -24(%rsp)
.LCFI2:
subq $24, %rsp
.LCFI3:
movq (%rdi), %rax
movl %esi, %r12d
subq $24, %rax
movq (%rax), %rbx
addq $1, %rbx
cmpq 8(%rax), %rbx
ja .L2
movl 16(%rax), %eax
testl %eax, %eax
jg .L2
.L4:
movq (%rbp), %rdx
movq -24(%rdx), %rax
movb %r12b, (%rdx,%rax)
movq (%rbp), %rax
subq $24, %rax
movl $0, 16(%rax)
movq %rbx, (%rax)
movb $0, 24(%rax,%rbx)
movq (%rsp), %rbx
movq 8(%rsp), %rbp
movq 16(%rsp), %r12
addq $24, %rsp
ret
.p2align 4,,7
.L2:
movq %rbx, %rsi
movq %rbp, %rdi
call _ZNSs7reserveEm
jmp .L4



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #562601 Fri Aug 03 22:06:18 UTC 2007 sebor 2007-08-03 Mark Brown <mark.g.brown@gmail.com>
Farid Zaripov <faridz@apache.org>

STDCXX-491
* string (push_back): Moved definition outside the basic_string class
template and optimized so as to call append() only when reallocation
is necessary.
Files Changed
MODIFY /incubator/stdcxx/trunk/include/string