[Python-ideas] Do we need non-heap types any more? (Was: Implicit submodule imports)

Sturla Molden sturla.molden at gmail.com
Sun Sep 28 19:13:06 CEST 2014


Sturla Molden <sturla.molden at gmail.com>
wrote:

> With branch prediction on a modern CPU an "if unlikely()" can probably push
> it down to inpunity. Both the Linux kernel and Cython does this liberally.

Just for reference, the definition of these macros in Cython and Linux are:

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

Typical usecases are

    fd = open(...);
    if (unlikely(fd < 0)) {
        /* handle unlikely error */
    }

or

    ptr = malloc(...);
    if (unlikely(!ptr)) {
        /* handle unlikely error */
    }    

If the conditionals fail, these checks have exactly zero impact on the
run-time with a processor that supports branch prediction. Microsoft
compilers don't know about __builtin_expect, but GCC, Clang and Intel
compilers know what to do with it. 

Sturla



More information about the Python-ideas mailing list