[Patch] Speed-up do_is(ctype_base::space, wchar_t)

Paolo Carlini pcarlini@suse.de
Sun Jul 17 09:24:00 GMT 2005


Hi,

this is something I noticed while working on 22515: for wchar_t,
important istream functions (sentry, operator>>(string/CharT*), ws) are
spending *a lot* of time in ctype::do_is(mask, CharT) due to the
is(ctype_base::space, CharT) calls (*). As an example, reading back from
file ten millions of newline separated integers (from 0 to 9999999) via

  std::wifstream in("input_test");
  std::wstring j;

  for (int i = 0; i < 10000000; ++i)
    in >> j;
  return 0;

leads to a profile with do_is on top, with 34% of the total time!
Indeed, for each call, we are looping over all the possible elementary
bit masks with:

      for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
      if (__m & _M_bit[__bitcur]
          && __iswctype_l(__c, _M_wmask[__bitcur], _M_c_locale_ctype))
        {
          __ret = true;
          break;
        }

which takes a lot of time (+ the cost of the virtual function call, not
present for plain char). But, on GNU systems (i.e., in the GNU locale
model) we *know* that the encoding of ctype_base::space is _M_bit[5] and
we can check first whether __m is exactly _M_bit[5].

For the example above, do_is drops to the third place with 14% of the
total time, which changes:

mainline
--
8.440u 0.090s 0:08.56 99.6%     0+0k 0+0io 213pf+0w

mainline + patch
---
6.600u 0.090s 0:06.71 99.7%     0+0k 0+0io 213pf+0w

Actually we already have in the performance testsuite a testcase which
exactly tracks this performance component, is_wchar_t.cc. The C++ case
changes:

mainline
--
2.930u 0.000s 0:02.93 100.0%    0+0k 0+0io 177pf+0w

mainline + patch
--
1.760u 0.000s 0:01.76 100.0%    0+0k 0+0io 177pf+0w

I'll keep on investigating this issue to see whether we can do something
more general within the current ABI but I'd like to commit first this
improvement.

Paolo.

(*) Actually, in our whole implementation ctype_base::space is the only
used mask, besides a single occurrence of ctype_base::upper in time_get.

////////////////
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CL_do_is
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20050717/e911e4ac/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch_do_is
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20050717/e911e4ac/attachment-0001.ksh>


More information about the Libstdc++ mailing list