为什么 Python 优化了 "if 0" 而不是 "if None"?
Why does Python optimize out "if 0", but not "if None"?
为什么如果你编译像
这样的条件表达式
def f():
if None:
print(222)
if 0:
print(333)
使用数字的分支得到优化,但使用 None
的分支却没有?示例:
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
在哪些情况下 if 0
和 if None
会表现不同?
我的猜测:这是一个疏忽,因为 None
只是 python-2.x.
中的一个特例名称(或全局名称)
如果你看一下 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 ... */
}
您可能会注意到 None
加载了 LOAD_GLOBAL
或 LOAD_NAME
,然后被 LOAD_CONST
替换。
但是:在它被替换后 break
s,所以它不能进入 LOAD_CONST
的情况,如果常量,块将被替换为 NOP
不是 True
。
在 python-3.x 中,优化器不需要对名称(或全局)None
进行特殊处理,因为它总是加载 LOAD_CONST
和 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 ... */
}
LOAD_NAME
和 LOAD_GLOBAL
不再有特殊情况,所以 if None
(而且 if False
- False
也成为 python-3.x) 将进入 LOAD_CONST
案例,然后替换为 NOP
.
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.
我发现了一种在 CPython 2.7 中重写 None
的方法,尽管它涉及一个肮脏的技巧并且可以类似地对文字进行。即,我替换了代码对象的 co_consts
字段中的常量条目 #0:
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()
输出:
None is false
None is true
为什么如果你编译像
这样的条件表达式def f():
if None:
print(222)
if 0:
print(333)
使用数字的分支得到优化,但使用 None
的分支却没有?示例:
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
在哪些情况下 if 0
和 if None
会表现不同?
我的猜测:这是一个疏忽,因为 None
只是 python-2.x.
如果你看一下 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 ... */
}
您可能会注意到 None
加载了 LOAD_GLOBAL
或 LOAD_NAME
,然后被 LOAD_CONST
替换。
但是:在它被替换后 break
s,所以它不能进入 LOAD_CONST
的情况,如果常量,块将被替换为 NOP
不是 True
。
在 python-3.x 中,优化器不需要对名称(或全局)None
进行特殊处理,因为它总是加载 LOAD_CONST
和 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 ... */
}
LOAD_NAME
和 LOAD_GLOBAL
不再有特殊情况,所以 if None
(而且 if False
- False
也成为 python-3.x) 将进入 LOAD_CONST
案例,然后替换为 NOP
.
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.
我发现了一种在 CPython 2.7 中重写 None
的方法,尽管它涉及一个肮脏的技巧并且可以类似地对文字进行。即,我替换了代码对象的 co_consts
字段中的常量条目 #0:
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()
输出:
None is false
None is true