Re: [Python-Dev] AST optimizer implemented in Python

2012-08-14 Thread Kristján Valur Jónsson
-Original Message- I moved the script to a new dedicated project on Bitbucket: https://bitbucket.org/haypo/astoptimizer Join the project if you want to help me to build a better optimizer! It now works on Python 2.5-3.3. I had the idea (perhaps not an original one) that

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-14 Thread Victor Stinner
2012/8/14 Kristján Valur Jónsson krist...@ccpgames.com: I moved the script to a new dedicated project on Bitbucket: https://bitbucket.org/haypo/astoptimizer Join the project if you want to help me to build a better optimizer! It now works on Python 2.5-3.3. I had the idea (perhaps not an

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-14 Thread Kristján Valur Jónsson
-Original Message- From: Victor Stinner [mailto:victor.stin...@gmail.com] Sent: 14. ágúst 2012 13:32 To: Kristján Valur Jónsson Cc: Python Dev Subject: Re: [Python-Dev] AST optimizer implemented in Python The problem is, there exists only bytecode disassembler, no corresponding

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-14 Thread Hrvoje Niksic
On 08/14/2012 03:32 PM, Victor Stinner wrote: I had the idea (perhaps not an original one) that peephole optimization would be much better done in python than in C. The C code is clunky and unwieldly, wheras python would be much better suited, being able to use nifty regexes and the like.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-13 Thread Mark Shannon
Brett Cannon wrote: On Sat, Aug 11, 2012 at 8:16 PM, Eric Snow ericsnowcurren...@gmail.com mailto:ericsnowcurren...@gmail.com wrote: On Sat, Aug 11, 2012 at 6:03 PM, Brett Cannon br...@python.org mailto:br...@python.org wrote: It would also be very easy to expand

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-13 Thread Guido van Rossum
Not so fast. If you make this a language feature you force all Python implementations to support an identical AST API. That's a big step. Not that AST manipulation isn't cool -- but I'd like to warn against over-enthusiasm that might backfire on the language (or its community) as a whole.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-13 Thread Terry Reedy
On 8/13/2012 10:45 AM, Guido van Rossum wrote: Not so fast. If you make this a language feature you force all Python implementations to support an identical AST API. That's a big step. I have been wondering about this. One could think from the manuals that we are there already. From the

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-13 Thread Nick Coghlan
Implementations are currently required to *have* an AST (or else declare non compliance with that particular flag to compile). They're definitely not required to have the *same* AST, thus all AST manipulation, like bytecode manipulation, is necessarily implementation dependent. We don't even

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Stefan Behnel
Victor Stinner, 11.08.2012 20:30: I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py Since you're about to do pretty much the same

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Stefan Behnel
Martin v. Löwis, 11.08.2012 23:27: +1 We should add some form of setastoptimizer API in 3.4. Please start a PEP for this. It would be nice to include the ability to properly cache the ast optimizer output so that it does not have to run every time (in pyc files or similar, etc) but can

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Stefan Behnel
Stefan Behnel, 12.08.2012 08:00: Victor Stinner, 11.08.2012 20:30: I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py Since you're

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread PJ Eby
On Sat, Aug 11, 2012 at 8:03 PM, Brett Cannon br...@python.org wrote: It would also be very easy to expand importlib.abc.SourceLoader to add a method which is called with source and returns the bytecode to be written out which people could override with AST optimizations before sending the

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread martin
I'm not sure if this is directly related or not, but making this mechanism support custom compilation for new filename suffixes would be nice, especially for various e.g. HTML/XML templating systems that compile to Python or bytecode. Specifically, having a way to add a new source suffix (e.g.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Victor Stinner
I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. I done more research. I found the AST optimizer of PyPy, which implements basic optimizations:

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Meador Inge
On Sat, Aug 11, 2012 at 1:30 PM, Victor Stinner victor.stin...@gmail.com wrote: Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Eric Snow
On Aug 12, 2012 12:56 PM, PJ Eby p...@telecommunity.com wrote: I'm not sure if this is directly related or not, but making this mechanism support custom compilation for new filename suffixes would be nice, especially for various e.g. HTML/XML templating systems that compile to Python or

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Brett Cannon
On Sun, Aug 12, 2012 at 6:07 PM, Eric Snow ericsnowcurren...@gmail.comwrote: On Aug 12, 2012 12:56 PM, PJ Eby p...@telecommunity.com wrote: I'm not sure if this is directly related or not, but making this mechanism support custom compilation for new filename suffixes would be nice,

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-12 Thread Nick Coghlan
On Mon, Aug 13, 2012 at 7:05 AM, Meador Inge mead...@gmail.com wrote: This is an interesting project and I would happily volunteer to help flesh out the details of a prototype and working on a PEP. Also, if there are possible AST improvements that would help in 3.4+, that option *is* on the

[Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Victor Stinner
Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py To test its peephole optimizations (by checking manually its final bytecode), I

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Gregory P. Smith
On Sat, Aug 11, 2012 at 11:30 AM, Victor Stinner victor.stin...@gmail.comwrote: Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Nam Nguyen
On Sat, Aug 11, 2012 at 11:47 AM, Gregory P. Smith g...@krypto.org wrote: On Sat, Aug 11, 2012 at 11:30 AM, Victor Stinner victor.stin...@gmail.com wrote: Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Martin v. Löwis
+1 We should add some form of setastoptimizer API in 3.4. Please start a PEP for this. It would be nice to include the ability to properly cache the ast optimizer output so that it does not have to run every time (in pyc files or similar, etc) but can verify that it is the specific ast

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Ned Batchelder
On 8/11/2012 2:30 PM, Victor Stinner wrote: Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py To test its peephole optimizations

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Eric Snow
On Sat, Aug 11, 2012 at 3:27 PM, Martin v. Löwis mar...@v.loewis.de wrote: I think you misunderstood. What gps is concerned about (IIUC) that some people add ast optimizers in some run of Python, but other AST optimizers in a different run. Then, if you use a Python byte code file, you should

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Chris Angelico
On Sun, Aug 12, 2012 at 4:30 AM, Victor Stinner victor.stin...@gmail.com wrote: I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project. Very nice idea! Other idea to improve this optimizer: - move

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Brett Cannon
On Sat, Aug 11, 2012 at 2:30 PM, Victor Stinner victor.stin...@gmail.comwrote: Hi, I started to implement an AST optimizer in Python. It's easy to create a new AST tree, so I'm surprised that I didn't find any existing project.

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Eric Snow
On Sat, Aug 11, 2012 at 6:03 PM, Brett Cannon br...@python.org wrote: It would also be very easy to expand importlib.abc.SourceLoader to add a method which is called with source and returns the bytecode to be written out Yes, please. Not having to hack around this would be nice. which

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Brett Cannon
On Sat, Aug 11, 2012 at 8:16 PM, Eric Snow ericsnowcurren...@gmail.comwrote: On Sat, Aug 11, 2012 at 6:03 PM, Brett Cannon br...@python.org wrote: It would also be very easy to expand importlib.abc.SourceLoader to add a method which is called with source and returns the bytecode to be

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Victor Stinner
Other idea to improve this optimizer: - move invariant out of loops. Example: x=[]; for i in range(10): x.append(i) = x=[]; x_append=x.append; for i in range(10): x_append(i). Require to infer the type of variables. But this is risky. It's theoretically possible for x.append to replace

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Chris Angelico
On Sun, Aug 12, 2012 at 11:17 AM, Victor Stinner victor.stin...@gmail.com wrote: The idea would be to allow the developer to specify explicitly what he wants to optimize. I'm using a configuration class with a list of what can be optimized (ex: len(int)), but it can be changed to something

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Stefan Behnel
Chris Angelico, 12.08.2012 01:22: Other idea to improve this optimizer: - move invariant out of loops. Example: x=[]; for i in range(10): x.append(i) = x=[]; x_append=x.append; for i in range(10): x_append(i). Require to infer the type of variables. But this is risky. It's theoretically

Re: [Python-Dev] AST optimizer implemented in Python

2012-08-11 Thread Stefan Behnel
Stefan Behnel, 12.08.2012 06:42: Chris Angelico, 12.08.2012 01:22: Other idea to improve this optimizer: - move invariant out of loops. Example: x=[]; for i in range(10): x.append(i) = x=[]; x_append=x.append; for i in range(10): x_append(i). Require to infer the type of variables. But