[pypy-dev] Need some pointers on my interpreter

Timothy Baldridge tbaldridge at gmail.com
Mon Aug 13 17:08:55 CEST 2012


I'm in the process of writing a interpreter in pypy and I'd like some
pointers on how to optimize my inner loop for better jit optimization.

My interpreter code is here:

https://github.com/halgari/cljvm/blob/master/system/interpreter.py

And the wrapper/unwrapper bits are here:

https://github.com/halgari/cljvm/blob/master/system/objspace.py

My test program is here:

https://github.com/halgari/cljvm/blob/master/tests/benchmark/basic_math.py

In this example the interpreter is getting "max" from the command line,
then counting from 0 to "max" via using a tail call. This looks something
like the following in Python:

def f(cur, max):
  if cur == max:
    return cur
  else:
    f(cur + 1, max)

Since this interpreter supports tail calls, the above code should basically
be jitted down to a simple for loop, or even better, just returning "max".

Here is my jitlog:

https://gist.github.com/3341474


A few notes about the VM:

Almost everything is immutable. That is, there are no local variables.
Locals are created via creating an anonymous function and passing variables
as arguments.

There are several "stacks" in the VM:

_call_stack - normal stack of functions
_arg_stack - the arguments of the functions in _call_stack
_stack - the main data stack. Temp variables are stored here
_ip_stack - the current instruction pointer location of each function in
_call_stack


>From what I can tell from the jitlog, it looks like there's a ton of code
being generated by the TAIL_CALL opcode. It seems the JIT doesn't realize
that the int values going into a function are the same ones coming out of
the previous iteration of the loop. In other words, there's no reason to
box the arguments to the TAIL_CALL into W_Ints, or to put them into an
array, since the next iteration of the loop will just take them out again.

Any suggestions of what I could do better here?

Thank you so much for your help, I've been playing with PyPy for years, and
it's exciting to finally understand this code enough that I can at least
make a change and see my benchmarks improve slightly. PyPy is an awesome
project.

Timothy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20120813/716e034e/attachment.html>


More information about the pypy-dev mailing list