This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch/RFC] Improve (maybe ;) basic_stringbuf::overflow


Hi all,

this is an idea I got (also perhaps inspired by Nathan).

It tests ok and definitely spares a copy :)

However, I'm not sure the standard guarantees that
swap exchanges also the capacities: I rely on that
property of v3 basic_string implementation.

In case in fact it doesn't, would be ok to just add a
comment about that??

Paolo.

/////////
2003-05-01  Paolo Carlini  <pcarlini@unitus.it>

	* include/bits/sstream.tcc (overflow): Instead of calling
	str(), then _M_string.reserve, thus copying the contents
	of the current buffer two times, just copy the buffer in
	a temporary, then use the 'swap trick'.
diff -prN libstdc++-v3-orig/include/bits/sstream.tcc libstdc++-v3/include/bits/sstream.tcc
*** libstdc++-v3-orig/include/bits/sstream.tcc	Wed Apr 30 14:26:32 2003
--- libstdc++-v3/include/bits/sstream.tcc	Thu May  1 22:05:43 2003
*************** namespace std
*** 100,111 ****
        // Order these tests done in is unspecified by the standard.
        if (!__testput)
  	{
! 	  // Force-allocate, re-sync.
! 	  _M_string = this->str();
  	  // 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.
! 	  _M_string.reserve(__len);
  	  _M_really_sync(const_cast<char_type*>(_M_string.data()),
  			 this->_M_in_cur - this->_M_in_beg, 
  			 this->_M_out_cur - this->_M_out_beg);
--- 100,113 ----
        // Order these tests done in is unspecified by the standard.
        if (!__testput)
  	{
! 	  __string_type __temp;
  	  // 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.	  
! 	  __temp.reserve(__len);
! 	  __temp.assign(_M_string.data(),
! 			this->_M_out_end - this->_M_out_beg);
! 	  _M_string.swap(__temp);
  	  _M_really_sync(const_cast<char_type*>(_M_string.data()),
  			 this->_M_in_cur - this->_M_in_beg, 
  			 this->_M_out_cur - this->_M_out_beg);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]