[Python-Dev] Code Generation Idea Was: Bytecode idea

Raymond Hettinger python@rcn.com
Tue, 25 Feb 2003 23:05:38 -0500


There is a piece of low hanging fruit in "while True" constructs.
If the code generator can recognize it, then three op-codes 
can be shaved off of every pass (LOAD_GLOBAL,
JUMP_IF_FALSE, and POP_TOP).


Raymond Hettinger



---------------------------------------------------------
>>> def f():
 while True:
  print "hello"

>>> dis.dis(f)
  2           0 SETUP_LOOP              17 (to 20)
        >>    3 LOAD_GLOBAL              0 (True)
              6 JUMP_IF_FALSE            9 (to 18)
              9 POP_TOP             

  3          10 LOAD_CONST               1 ('hello')
             13 PRINT_ITEM          
             14 PRINT_NEWLINE       
             15 JUMP_ABSOLUTE            3
--------------------------------------------------------
It would be great if lines 3, 6, and 9 were not
generated or if line 15 was JUMP_ABSOLUTE 10.