parse-time optimizations

Thomas Wouters thomas at xs4all.net
Tue May 29 04:30:09 EDT 2001


On Tue, May 29, 2001 at 05:08:47AM +0000, jcm wrote:

[ jcm wants to optimize constant expressions away ]

> This set of optimizations, which are very easy to identify at
> parse-time, would be nice to see added to Python.  There are some
> things to pay attention to (like arithmetic overflow), but still
> fairly simple to implement, as far as optimizations go.  And yes, I
> have.

> Yeah I know: "so go implement it yourself in Python too".  Wishful
> thinking is about as far as I expect I'll get.  If I ever stuck my
> hands in the Python parser, there are a couple of things I'd want to
> change (like not generating the full concrete syntax tree--parser
> module be damned!).  I should stop now or I might actually convince
> myself to go look at the Python source.

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): 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.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list