[Python-Dev] lnotab and the AST optimizer

Antoine Pitrou solipsis at pitrou.net
Thu Jul 24 17:19:04 CEST 2008


Hi,

> I'm making some good progress with the AST optimizer, and now the main 
> thing standing in my way is lnotab. Currently lnotab expects bytecode 
> sequencing to be roughly in-sync with the order of the source file and a 
> few things that the optimizer does (e.g. swapping the bodies of an 
> if/else after removing negation such that "if not X: A; else: B" becomes 
> "if X: B; else A") breaks this assumption. This will result in either an 
> assertion failure or incorrect line numbers being reported.

In http://bugs.python.org/issue2459 ("speedup for / while / if with better
bytecode") I had the same problem and decided to change the lnotab format so
that line number increments are signed bytes rather than unsigned. The proposed
patch contains many other changes, but with a bit of perseverance you may be
able to extract the lnotab-related modifications... ;)

This modification will allow many more types of control flow transformations
than the current scheme does.


By the way:
> swapping the bodies of an 
> if/else after removing negation such that "if not X: A; else: B" becomes 
> "if X: B; else A")

Is this really an optimization? "if" and "if not" should use the same number of
opcodes (the former produces JUMP_IF_FALSE and the latter JUMP_IF_TRUE).

Regards

Antoine.




More information about the Python-Dev mailing list