[Python-Dev] Accessing globals without dict lookup (original) (raw)

Neil Schemenauer nas@python.ca
Tue, 12 Feb 2002 13:27:24 -0800


Tim Peters wrote:

if (normal) do normal stuff else do exceptional stuff

Most dumb compilers on platforms that care use a "forward branches probably aren't taken, backward branches probably are" heuristic for setting branch-prediction hints in the machine code; and on platforms that don't care it's usually faster to fall through than to change the program counter anyway.

I seem to remember someone saying that GCC generated better code for:

    if (exceptional) {
        do exceptional things
        break / return / goto
    }
    do normal things

Is GCC in the dumb category? Also, the Linux is starting to use this set of macros more often:

/* Somewhere in the middle of the GCC 2.96 development cycle, we
 * implemented a mechanism by which the user can annotate likely
 * branch directions and expect the blocks to be reordered
 * appropriately.  Define __builtin_expect to nothing for earlier
 * compilers.  */

#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
#define __builtin_expect(x, expected_value) (x)
#endif

#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)

For example:

if (likely(normal))
    do normal stuff
else do exceptional stuff

I don't have GCC >= 2.96 otherwise I would have tried sprinkling some of those macros in ceval and testing the effect.

Neil