parse-time optimizations

Michael Hudson mwh at python.net
Tue May 29 13:49:46 EDT 2001


Thomas Wouters <thomas at xs4all.net> writes:

> No need to look at the source. You can write the optimizations in
> Python.  All you need is the marshal module, the dis module and some
> creative disk work (to make the code attributes effectively writable
> -- M.'s bytecodehacks might help here, I haven't checked

bytecodehacks has a "rationalize" module, mostly written by Corran
Webster, that does constant folding, and some jump-jump optimizations
(nad maybe some other stuff, I haven't looked at it in ages).

Thing is, if you're writing code that benefits from constant folding -
WHY?  If your code contains things like:

    "a" + "b"

then you have bigger problems than Python's lack of optimizations...

> ): keep track of which vrbls are constants (start by looking for
> 'LOAD_CONST' opcodes) and replace them where necessary. Don't forget
> to keep a jump-offset table when you collapse instructions, and
> adjust all jumps correctly, or you'll end up jumping into outer
> space.

This is why you want to use bytecodehacks - they do all this for you.

Of course they don't work properly for Python 2.1...

Cheers,
M.

-- 
  ZAPHOD:  Listen three eyes, don't try to outwierd me, I get stranger
           things than you free with my breakfast cereal.
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 7



More information about the Python-list mailing list