[Python-Dev] Re: Code Generation Idea Was: Bytecode idea
Ludovic Aubry
Ludovic.Aubry@logilab.fr
Thu, 27 Feb 2003 16:02:46 +0100
On Wed, Feb 26, 2003 at 09:55:50PM +0100, M.-A. Lemburg wrote:
> Ludovic Aubry wrote:
> >I have a piece of code that does (almost) exactly that, except at
> >runtime.
> >It's purpose is to optimize access to globals used as constants by
> >replacing the LOAD_GLOBAL opcode by a LOAD_CONST.
> >It does that by creating a new code object for the function you provide
> >and using a list of symbols to consider constant.
> >
> > [code]
>
> I suppose this kind of code could be put to some good use
> if we ever get some of the recently discussed function modifiers
> into Python:
>
> def myFunction(a,b) [bindglobals]:
> for i in range(len(a)):
> b[i] = math.sin(a[i]) * math.cos(2)
>
> Hmm, now we'd only need a way to describe "this function has no
> side-effects and behaves like a mathematical function (same inputs
> map to same outputs)"... then we could also optimize cos(2)
> into the constants area :-)
>
With a [bindglobals] modifier we need a way to specify which objects we
want to bind.
We can pass a list or dict as with my example or we can have the
container of the definition tell us if a symbol should be a constant.
Actually my first implementation of this code was using a __consts__
variable (like Jeff's __solid__) to tell the bind function what
symbol it could replace.
I patched PyFunction_New to create a new code object at the time the
function object was created, thus binding or not was decided by
looking for the __consts__ variable in the globals.
There was no need for a [bindglobals] but people not knowing I patched
the interpreter were a little bit confused ;)
--
Ludovic Aubry LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org