Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python optimize out "if 0", but not "if None"?

Why is it that if you compile a conditional expression like

def f():
    if None:
        print(222)
    if 0:
        print(333)

the branches that use numbers get optimized out, but those that use None don't? Example:

 3        0 LOAD_CONST               0 (None)
          3 POP_JUMP_IF_FALSE       14

 4        6 LOAD_CONST               1 (222)
          9 PRINT_ITEM          
         10 PRINT_NEWLINE       
         11 JUMP_FORWARD             0 (to 14)

 5  >>   14 LOAD_CONST               0 (None)
         17 RETURN_VALUE        

In which scenarios could if 0 and if None behave differently?

like image 655
user541686 Avatar asked Jun 06 '17 12:06

user541686


2 Answers

My guess: It's an oversight that happened because None is just a special-cased name (or global) in python-2.x.

If you take a look at the bytecode-optimizer code in python-2.x:

switch (opcode) {

   /* ... More cases ... */

        /* Replace LOAD_GLOBAL/LOAD_NAME None
           with LOAD_CONST None */
    case LOAD_NAME:
    case LOAD_GLOBAL:
        j = GETARG(codestr, i);
        name = PyString_AsString(PyTuple_GET_ITEM(names, j));
        if (name == NULL  ||  strcmp(name, "None") != 0)
            continue;
        for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
            if (PyList_GET_ITEM(consts, j) == Py_None)
                break;
        }
        if (j == PyList_GET_SIZE(consts)) {
            if (PyList_Append(consts, Py_None) == -1)
                goto exitError;
        }
        assert(PyList_GET_ITEM(consts, j) == Py_None);
        codestr[i] = LOAD_CONST;
        SETARG(codestr, i, j);
        cumlc = lastlc + 1;
        break;      /* Here it breaks, so it can't fall through into the next case */

        /* Skip over LOAD_CONST trueconst
           POP_JUMP_IF_FALSE xx. This improves
           "while 1" performance. */
    case LOAD_CONST:
        cumlc = lastlc + 1;
        j = GETARG(codestr, i);
        if (codestr[i+3] != POP_JUMP_IF_FALSE  ||
            !ISBASICBLOCK(blocks,i,6)  ||
            !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
            continue;
        memset(codestr+i, NOP, 6);
        cumlc = 0;
        break;

   /* ... More cases ... */

}

You may notice that None is loaded with LOAD_GLOBAL or LOAD_NAME and then replaced by LOAD_CONST.

However: After it is replaced it breaks, so it can't go into the LOAD_CONST case in which the block would be replaced with a NOP if the constant isn't True.


In python-3.x the optimizer doesn't need to special case the name (or global) None because it's always loaded with LOAD_CONST and the bytecode-optimizer reads:

switch (opcode) {

   /* ... More cases ... */

        /* Skip over LOAD_CONST trueconst
           POP_JUMP_IF_FALSE xx.  This improves
           "while 1" performance.  */
    case LOAD_CONST:
        CONST_STACK_PUSH_OP(i);
        if (nextop != POP_JUMP_IF_FALSE  ||
            !ISBASICBLOCK(blocks, op_start, i + 1)  ||
            !PyObject_IsTrue(PyList_GET_ITEM(consts, get_arg(codestr, i))))
            break;
        fill_nops(codestr, op_start, nexti + 1);
        CONST_STACK_POP(1);
        break;

   /* ... More cases ... */

}

There's no special case for LOAD_NAME and LOAD_GLOBAL anymore so if None (but also if False - False was also made a constant in python-3.x) will go into the LOAD_CONST case and then replaced by a NOP.

like image 98
MSeifert Avatar answered Nov 03 '22 00:11

MSeifert


Disclaimer: This is not really an answer, but just a report of my succeeded attempt to override None in CPython 2.7 despite the protection by the compiler.

I found a way of overriding None in CPython 2.7, though it involves a dirty trick and could similarly be done to literals. Namely, I replace the constant entry #0 in the co_consts field of a code object:

def makeNoneTrueIn(func):
    c = func.__code__
    func.__code__ = type(c)(c.co_argcount,
                            c.co_nlocals,
                            c.co_stacksize,
                            c.co_flags,
                            c.co_code,
                            (True, ) + c.co_consts[1:],
                            c.co_names,
                            c.co_varnames,
                            c.co_filename,
                            c.co_name,
                            c.co_firstlineno,
                            c.co_lnotab,
                            c.co_freevars,
                            c.co_cellvars)


def foo():
    if None:
        print "None is true"
    else:
        print "None is false"

foo()
makeNoneTrueIn(foo)
foo()

Output:

None is false
None is true
like image 39
Leon Avatar answered Nov 03 '22 00:11

Leon