[v3] char_traits error checking
Ulrich Drepper
drepper@redhat.com
Tue Mar 12 11:45:00 GMT 2002
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 `------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 232 bytes
Desc: This is a digitally signed message part
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20020312/d894b3bc/attachment.sig>
More information about the Libstdc++
mailing list