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]

Re: [v3] char_traits error checking


On Tue, 2002-03-12 at 11:02, Benjamin Kosnik wrote:

> *** 139,144 ****
> --- 139,148 ----
>         {
>   	size_type __dnew = static_cast<size_type>(distance(__beg, __end));
>   
> + 	// NB: Not required, but considered best practice.
> + 	if (__beg == _InIter(0))
> + 	  __throw_logic_error("attempt to create string with null value");
> + 

You might want to consider to start using __builtin_expect.  Not just
here but it is a prime example.  While in program code it is often
possible to use profile guided optimization of the code paths this isn't
possible in libraries.  But the implementor still knows details about
the probability of tests.

In the above test the beg == _InIter(0) test is almost never true.  And
even if it would be, the fast path is to not throw an exception. 
Therefore the if () expressions should be written like this:

  if (__builtin_expect (__beg == _InIter(0), 0))

This tells the compiler to expect that the condition is almost always
wrong.  The block reordering path (enabled with -O2 in gcc3) will then
move the code out of the common code path to improve the icache
usability and adjust the generated code to help the processor make the
right branch prediction.

The improvements of each single use is probably small but it adds up. 
glibc has __builtin_expect all over the place.  It's an acceptable
requirement that all code should take advantage of this feature whenever
it is useful.  Checks for errors are always a prime candidate but there
are other boundary conditions as well.

-- 
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------

Attachment: signature.asc
Description: This is a digitally signed message part


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