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] Improve performance-wise __pad::_S_pad


Hi!

To tell you the truth, I'm quite proud of this evening! ;)

In short:
 1- 4 less locals.
 2- Removed the __builtin_alloca.
 3- Avoid one traits::copy over two: now __oldlen chars are
    copied vs __newlen before, that is now we copy the bare
    minimum number of chars.
 4- Shorter and not less clean code, IMHO.

Indeed, the patch is quite invasive, and, whereas I have regression
tested it (x86-linux, as usual) and carefully checked it on paper,
please double check it! Thanks!

Paolo.

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

	* include/bits/locale_facets.tcc (__pad<>::_S_pad):
	Improve performance-wise: avoid one traits::copy, avoid
	the __builtin_alloca, streamline.
diff -prN libstdc++-v3-1/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
*** libstdc++-v3-1/include/bits/locale_facets.tcc	Sun Oct  5 16:50:47 2003
--- libstdc++-v3/include/bits/locale_facets.tcc	Sun Oct  5 19:14:07 2003
*************** namespace std
*** 2207,2229 ****
  				   const streamsize __newlen, 
  				   const streamsize __oldlen, const bool __num)
      {
!       size_t __plen = static_cast<size_t>(__newlen - __oldlen);
!       _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
! 							     * __plen));
!       _Traits::assign(__pads, __plen, __fill); 
  
-       _CharT* __beg;
-       _CharT* __end;
        size_t __mod = 0;
-       size_t __beglen; //either __plen or __oldlen
-       ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
- 
        if (__adjust == ios_base::left)
  	{
  	  // Padding last.
! 	  __beg = const_cast<_CharT*>(__olds);
! 	  __beglen = __oldlen;
! 	  __end = __pads;
  	}
        else if (__adjust == ios_base::internal && __num)
  	{
--- 2207,2222 ----
  				   const streamsize __newlen, 
  				   const streamsize __oldlen, const bool __num)
      {
!       const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
!       const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
  
        size_t __mod = 0;
        if (__adjust == ios_base::left)
  	{
  	  // Padding last.
! 	  _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
! 	  _Traits::assign(__news + __oldlen, __plen, __fill);
! 	  return;
  	}
        else if (__adjust == ios_base::internal && __num)
  	{
*************** namespace std
*** 2254,2273 ****
  	      ++__news;
  	    }
  	  // else Padding first.
- 	  
- 	  __beg = __pads;
- 	  __beglen = __plen;
- 	  __end = const_cast<_CharT*>(__olds + __mod);
- 	}
-       else
- 	{
- 	  // Padding first.
- 	  __beg = __pads;
- 	  __beglen = __plen;
- 	  __end = const_cast<_CharT*>(__olds);
  	}
!       _Traits::copy(__news, __beg, __beglen);
!       _Traits::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
      }
  
    template<typename _CharT>
--- 2247,2256 ----
  	      ++__news;
  	    }
  	  // else Padding first.
  	}
!       _Traits::assign(__news, __plen, __fill);
!       _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
! 		    __oldlen - __mod);
      }
  
    template<typename _CharT>

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