Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possibilities for fast dynamic code execution in Python

I have some code available in some form of AST and I would like to execute it.

I can think of several ways to do this, e.g.:

  • Just straight-forwardly interpret it.
  • Translate it into a Python AST (the ast module) and
    • Python-compile that or
    • Python-eval that.
  • Translate it into Python source code (e.g. a pure string) and
    • Python-compile that or
    • Python-eval that.
  • Translate it in some form of low level code and write a simple VM in Python which runs that.

I guess I would get the fasted execution by translating it into a Python AST, compile that and run that. Esp. when using PyPy, I might even get improvements by PyPys JIT compiling optimizations (I hope I do, do I?).

Can you think of other possibilities? Can you give suggestions on what might be the best way?

like image 384
Albert Avatar asked Nov 14 '22 20:11

Albert


1 Answers

Another possibility: translate to Cython code, write out to a file, compile with Cython then a C compiler with optimization turned on, load the resulting module and execute it.

If the code has type annotations that can be translated to Cython/C types, this can be blazing fast. Watch out, though, as Cython is in beta and still a bit rough around the edges. Also, this solution only works for CPython.

like image 134
Fred Foo Avatar answered Dec 17 '22 17:12

Fred Foo