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]

ctype optimization work


Hi, folks.

I've been trying a few things to speedup ctype<char>::narrow() and
have had some success but not as much as I was hoping.  I'm posting my
results so far to get input.

The single char form is easier to speed up.  I tried a few things:

1) return table lookup to get a best case scenario (impossible of
   course)

2) completely filling a table then using it

3) filling cells on demand (clear winner)

4) baseline


      char 
      narrow(char_type __c, char __dfault) const
      {
	// 2.66s
// 	return _M_narrow_table[__c];

	// 6.62s
	// _M_narrow_init fills the table and sets _M_narrow_ok
//   	if (__builtin_expect(!_M_narrow_ok, false)) { _M_narrow_init(); }
// 	return _M_narrow_table[__c];

	// 4.83s
	if (_M_narrow_table[__c]) return _M_narrow_table[__c];
	char __t = do_narrow(__c, __dfault);
	if (__t != __dfault) _M_narrow_table[__c] = __t;
	return __t;

	// 7.43s
	return do_narrow(__c, __dfault);
      }


For the array form, I had less success.  Here I had to check that al
cells of the table are valid before using it.

1) Table lookup was horribly slow, even though it doesn't hit the
   virtual function.

2) By assuming that _M_narrow_init sets a flag if memcpy is valid
   (only for the base ctyp<char> class), I could move the memcpy into
   narrow() from do_narrow() for minor speedup.


      const char_type*
      narrow(const char_type* __lo, const char_type* __hi,
	      char __dfault, char *__to) const
      {
	// 6.25s
	if (_M_narrow_ok==1)
	  {
	    memcpy(__to, __lo, __hi - __lo);
	    return __hi;
	  }
	if (!_M_narrow_ok)
	  _M_narrow_init();
	this->do_narrow(__lo, __hi, __dfault, __to);
	return __hi;
      
	// 35.17s HUH!?!
// 	if (!_M_narrow_ok) { _M_narrow_init(); }
// 	if (_M_narrow_ok == 1)
// 	  while (__lo != __hi) *__to++ = _M_narrow_table[*__lo++];
// 	else
// 	  this->do_narrow(__lo, __hi, __dfault, __to);
// 	return __hi;

	// 6.63s
// 	return this->do_narrow(__lo, __hi, __dfault, __to);
      }


Any thoughts?

Jerry Quinn


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