thautwarm / diojit

Fully compatible CPython jit compiler. Optimising Dynamic, Interpreted, and Object-oriented(DIO) programs.
BSD 2-Clause "Simplified" License
117 stars 2 forks source link

efficient representation for closures #4

Closed thautwarm closed 3 years ago

thautwarm commented 3 years ago

so far it's hard to make closures efficient, because each free variable has to have top type. in lack of escape analysis to give better assumptions for the type of a free variable. this is not the core of dynjit though it contributes a lot to performance when the programs use high order functions.

thautwarm commented 3 years ago

WIP:

import jit

@jit.parametric_struct
class JitClosure:
    free : object
    fptr : object
    def __init__(self, free, fptr):
        self.free = free
        self.fptr = fptr
    def __call__(self, *args):
         return self.fptr(*self.free, *args)

# don't write
def f(x):
   return lambda : x

# instead, use
def f(x):
    JitClosure((1, ), lambda c: c)

# don't write
def f(x, y):
    return lambda x, y, z: x + y + z

# instead, use
def f(x):
    return JitClosure((1, 2), lambda x, y, z: x + y + z)
thautwarm commented 3 years ago

simple escape analysis can be done, say, if a variable is initialized in only one source code point.

thautwarm commented 3 years ago

closed for the favour of #8