This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
basic_string<>: operator+ - patch
- To: Benjamin Kosnik <bkoz@cygnus.com>
- Subject: basic_string<>: operator+ - patch
- From: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
- Date: Fri, 2 Jul 1999 10:22:03 +0200 (CEST)
- cc: libstdc++@sourceware.cygnus.com
- Reply-To: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Now since the reserve and _M_mutate member functions
are fixed we can optimize the operator+.
I updated my old patch.
Below the description, the atachment contains a patch.
The operator+ for basic_string<> is curently implemented in the way:
basic_string<> __str(__lhs);
__str.append(__rhs);
return __str;
In the cases where __lhs is a string it is OK (except if __lhs is unsharable).
In other cases (_CharT, _CharT*) it leads to a reallocation.
My proposal:
create an empty string -> call reserve -> append __lhs -> append __rhs.
1999-07-02 Ryszard Kabatek <kabatek@chemie.uni-halle.de>
* bits/basic_string.h:
Call reserve in operator+ to avoid reallocation.
Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 (2466) Fax. +49 3461 46 2129
Index: bits/basic_string.h
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/basic_string.h,v
retrieving revision 1.42
diff -c -2 -p -r1.42 basic_string.h
*** basic_string.h 1999/07/01 02:40:22 1.42
--- basic_string.h 1999/07/02 08:14:57
*************** namespace std {
*** 820,824 ****
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
__str.append(__rhs);
return __str;
--- 820,828 ----
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! typedef basic_string<_CharT, _Traits, _Alloc> __str_type;
! __str_type::size_type __len = _Traits::length(__lhs);
! __str_type __str;
! __str.reserve(__len + __rhs.size());
! __str.append(__lhs, __lhs + __len);
__str.append(__rhs);
return __str;
*************** namespace std {
*** 829,834 ****
operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str(__rhs);
! __str.insert(__str.begin(), __lhs);
return __str;
}
--- 833,840 ----
operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str;
! __str.reserve(1 + __rhs.size());
! __str.append(1, __lhs);
! __str.append(__rhs);
return __str;
}