This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[patch/rfa] Tiny but delicate change to string::_M_mutate
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Cc: Nathan Myers <ncm at cantrip dot org>
- Date: Fri, 15 Oct 2004 20:26:18 +0200
- Subject: [patch/rfa] Tiny but delicate change to string::_M_mutate
Hi everyone, hi Nathan,
I like a lot this tweak but I'm not going to apply it if Nathan is not
able to double check it: is very small but delicate.
Basically, when Nathan improved last year the _S_empty_rep treatment
(not ref-counted anymore) he added another case in the conditional
at the beginning of _M_mutate:
if (_M_rep() == &_S_empty_rep || __new_size > capacity() ...
Therefore, what change is that we reallocate even when __new_size ==
capacity(). However, that seems not necessary, since, in that case,
we have simply another empty string (for the empty_rep, capacity() == 0,
of course). If we remove the new case, nothing happens in _M_mutate,
besides eventually reconfirming the sharable state for the empty string
object, length == 0 and _Rep::_S_terminal in position zero, the default
contents, in other terms.
Nathan, can you spot any flaw in my reasoning? If the idea works we
obtain, not only a slightly simpler _M_mutate, but, more importantly,
we keep on using the empty_rep trick even after things like
string str;
str.append(0, 'x');
which currently leads to normal dynamically allocated memory.
(Of course regtested, on x86-linux, for now)
Paolo.
//////////////
2004-10-16 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.tcc (_M_mutate): Do not reallocate
unnecessarily when _M_rep() == &_S_empty_rep() and __new_size
== capacity(): in that case is ok to simply leave everything
unchanged.
diff -urN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc 2004-10-13 10:58:39.000000000 +0200
+++ libstdc++-v3/include/bits/basic_string.tcc 2004-10-15 19:35:18.000000000 +0200
@@ -393,12 +393,7 @@
const size_type __new_size = __old_size + __len2 - __len1;
const size_type __how_much = __old_size - __pos - __len1;
-#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
- if (_M_rep() == &_S_empty_rep()
- || _M_rep()->_M_is_shared() || __new_size > capacity())
-#else
if (_M_rep()->_M_is_shared() || __new_size > capacity())
-#endif
{
// Must reallocate.
const allocator_type __a = get_allocator();