0

I have a computer generated AST in python. I interpret this AST 2000 times, but this is slow. Moreover, this AST changes during runtime, and it's not known before compile time. I can codegen my AST to a valid python string, but I don't know how to execute it. To give a conceptual overview, this is what I have now:

def exec(AST):
   {local variables}
   {interpret AST over local variables 2000 times}

I would like:

def exec(AST):
  {local variables}
  new_fn = make_lambda(to_python_string(AST))
  {iterate new_fn over local variables 2000 times}

Note: different calls to exec will have totally new unknown AST that was constructed at runtime. My current bottleneck is in the AST interpretation, and my hope is that dynamic function generation will make it faster.

2
  • "new_fn = make_lambda(to_python_string(AST))" You answered your own question, writ e a function that returns a function and use this in the loop. Commented Jul 31, 2020 at 21:25
  • How do I write a function from a string during runtime? Commented Jul 31, 2020 at 21:25

2 Answers 2

1

So python3.8 does have an exec command that takes a string, and executes it. There are some caveats if your generated function calls local variables.

This works:

def exec(AST):
  {local variables}
  exec("def new_fn():\n" + to_python_string(AST), globals())
  {iterate new_fn over local variables 2000 times}

This is blazingly faster than interpreting the AST.

Sign up to request clarification or add additional context in comments.

Comments

0

This works:

def fun1():
    return 5


def fun2():
    return 7


def make_lambda(x):
    if x == 1:
        return fun1
    else:
        return fun2

print(make_lambda(1)())
print(make_lambda(2)())

1 Comment

This isn't what I'm looking for. Here fun1 and fun2 are known at compile time. In my case, I have the AST for fun1/fun2, which can be codegened into a valid string of python code. More over, this AST changes during execution. In my python file, I don't know what fun1 and fun2 look like, it's constructed during runtime, and want to execute it. Right now I interpret the AST which is slow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.