pointless musings on performance

Antoine Pitrou solipsis at pitrou.net
Tue Nov 24 10:11:29 EST 2009


Hello,

Le Tue, 24 Nov 2009 14:41:19 +0100, mk a écrit :
> 
> As Rob pointed out (thanks):
> 
> 11          31 LOAD_FAST                0 (nonevar)
>               34 JUMP_IF_FALSE            4 (to 41)
> 
> I'm no good at py compiler or implementation internals and so I have no
> idea what bytecode "JUMP_IF_FALSE" is actually doing.

It tries to evaluate the op of the stack (here nonevar) in a boolean 
context (which theoretically involves calling __nonzero__ on the type) 
and then jumps if the result is False (rather than True).

You are totally right that it does /more/ than "is not None", but since 
it is executed as a single opcode rather than a sequence of several 
opcodes, the additional work it has to do is compensated (in this case) 
by the smaller overhead in bytecode interpretation.

As someone pointed out, the Python interpreter could grow CISC-like 
opcodes so as to collapse "is not None" (or generically "is not 
<constant>") into a single JUMP_IF_IS_NOT_CONST opcode. Actually, it is 
the kind of optimizations wpython does (http://code.google.com/p/
wpython/).

Regards

Antoine.




More information about the Python-list mailing list