Hello everyone.
I'm proud to release the result of a Facebook-sponsored study on the feasibility of
using the RPython toolchain to produce a PHP interpreter. The rules were
simple: two months; one person; get as close to PHP as possible, implementing
enough warts and corner cases to be reasonably sure that it answers hard
problems in the PHP language. The outcome is called Hippy VM and implements
most of the PHP 1.0 language (functions, arrays, ints, floats and strings).
This should be considered an alpha release.
The resulting interpreter is obviously incomplete – it does not support all
modern PHP constructs (classes are completely unimplemented), builtin functions,
grammar productions, web server integration, builtin libraries
etc., etc.. It's just complete enough for me to reasonably be able to
say that – given some engineering effort – it's possible to provide a rock-solid
and fast PHP VM using PyPy technologies.
The result is available in a Bitbucket repo and is released under the MIT
license.
PHP deviations
The project's biggest deviation from the PHP specification is probably
that GC is no longer reference counting. That means that the object finalizer, when
implemented, will not be called directly at the moment of object death, but
at some later point. There are possible future developments to alleviate that
problem, by providing "refcounted" objects when leaving the current scope.
Research has to be done in order to achieve that.
Assessment
The RPython toolchain seems to be a cost-effective choice for writing
dynamic language VMs. It both provides a fast JIT and gives you
access to low-level primitives when you need them. A good example is
in the directory hippy/rpython which contains the implementation
of an ordered dictionary. An ordered dictionary is not a primitive
that RPython provides – it's not necessary for the goal of
implementing Python. Now, implementing it on top of a normal dictionary
is possible, but inefficient. RPython provides a way to work
directly at a lower level, if you desire to do so.
Things that require improvements in RPython:
- Lack of mutable strings on the RPython level ended up being a problem.
I ended up using lists of characters; which are efficient, but inconvenient,
since they don't support any string methods.
- Frame handling is too conservative and too Python-specific, especially around
the calls. It's possible to implement less general, but simpler and faster
frame handling implementation in RPython.
Status of the implementation
Don't use it! It's a research prototype intended to assess the feasibility
of using RPython to create dynamic language VMs. The most notable
feature that's missing is reasonable error reporting. That said, I'm
confident it implements enough of the PHP language to prove that the full
implementation will present the same performance characteristics.
Benchmarks
The benchmarks are a selection of computer language shootout benchmarks, as well
as cache_get_scb, which is a part of old Facebook code. All benchmarks other
than this one (which is not open source, but definitely the most interesting :( ) are
available in the bench directory. The Python program to run them is called
runner.py and is in the same directory. It runs them 10 times, cutting off the first
3 runs (to ignore the JIT warm-up time) and averaging the rest. As you can see
the standard deviation is fairly minimal for all interpreters and runs; if
it's omitted it means it's below 0.5%.
The benchmarks were not selected for their ease of optimization – the optimizations
in the interpreter were written specifically for this set of benchmarks. No special JIT
optimizations were added, and barring what's mentioned below a vanilla PyPy 1.9 checkout
was used for compilation.
So, how fast will my website run if this is completed?
The truth is that I lack the benchmarks to be able to answer that right now. The core
of the PHP language is implemented up to the point where I'm confident
that the performance will not change as we get more of the PHP going.
How do I run it?
Get a PyPy checkout, apply the diff if you want to squeeze out the last
bits of performance and run pypy-checkout/pypy/bin/rpython targethippy.py to
get an executable that resembles a PHP interpreter. You can also directly run
python targethippy.py file.php, but this will be about 2000x slower.
RPython modifications
There was a modification that I did to the PyPy source code; the diff
is available. It's trivial, and should simply be made optional in the
RPython JIT generator, but it was easier just to do it, given the very constrained time
frame.
- gen_store_back_in_virtualizable was disabled. This feature is
necessary for Python frames but not for PHP frames. PHP frames
do not have to be kept alive after we exit a function.
Future
Hippy is a cool prototype that presents a very interesting path towards a fast
PHP VM. However, at the moment I have too many other open source commitments
to take on the task of completing it in my spare time. I do think that this project
has a lot of potential, but I will not commit to any further development at
this time. If you send pull requests I'll try to review them. I'm also open
to having further development on this project funded, so if you're interested
in this project and the potential of a fast PHP interpreter, please get in
touch.
Cheers,
fijal
EDIT: Fixed the path to the rpython binary