[Python-Dev] Release of astoptimizer 0.3

larudwer larudwer at freenet.de
Wed Sep 12 01:36:51 CEST 2012


Hello,

i am a longtime Reader of this list and this is the first time i a dare to 
speak up.
Apology in advance for any noise, silly comments and not posting to 
python-ideas ;).

Am 11.09.2012 12:41, schrieb Victor Stinner:
> I plan to implement other optimizations like unrolling loop or convert
> a loop to a list comprehension, see the TODO file.
>
> Don't hesitate to propose more optimizations if you have some ideas ;-)

Well how about implementing guards like in pypy?
They are not optimizations by themselves but should enable a whole lot of 
them.

I am not good at explaining so i've written this short example (python 
2.7.3).
I hope it makes clear what i mean.

Thank you for reading
larudwer

#
# Example for implementing an guard
# by freezing global and local dictionary
# it is not an optimization by itself
# but should open the possibility for them.
#

class WorldChangedException(Exception): pass
class WorldShadowedException(Exception): pass

class frozendict(dict):
    def __setitem__(self, key, value):
        if key in globals():
            if self == locals():
                raise WorldShadowedException(key)
            else:
                raise WorldChangedException(key)

def put(string):
    print string

g = frozendict({'put': put, 'foo': 2})
l = frozendict({'bar': 'work fast'})

#
# This is just an poor example.
# astoptimizer should be able to generate
# much better code
#
f = compile("""
put( "trying fast path")
put(bar)
put = 27
""", "<astoptimizer>", "exec")

def test():
    global put # Workaround for UnboundLocalError
    put("trying slow path")
    put = 27

#
# Guard: since the world is frozen, nothing can change.
# if the fast path succeeds we take the result, else
# we use the slow path.
#
try:
    exec(f, g, l)
except WorldChangedException, e:
    print "WorldChangedException Caught", e
    test()
except WorldShadowedException, e:
    print "WorldShadowedException Caught", e
    test()




More information about the Python-Dev mailing list