Over 1.5 years ago, I introduced PHPPHP to the world. It was the first implementation of the PHP language written in PHP itself. But PHPPHP suffered from a few problems which relegated it to toy status (such as performance). Today, I get to introduce you to another implementation of PHP, written in PHP. But this one is no toy. This one… This one is fun…
I’d like to introduce you to The Recki Compiler Toolkit (Recki-CT).
It’s a Compiler written in PHP that targets a subset of the PHP language (a less dynamic subset). This means that it does not support things like references or variable-variables. It also does not support global variables (at all, not even super-globals). What’s the point of that you ask?
Well, if you’re able to write code that’s static enough, then we can reason about it. We can analyze it. We can optimize it. And we can statically compile it to machine code.
Yes, that’s right. Recki-CT compiles PHP down to machine code.
But it’s fundamentally different from other approaches at doing so. HHVM and HippyVM both use Just In Time Compilation to compile PHP. That means that the compilation happens at run-time. Which means that the compilation process must be fast.
Recki-CT on the other hand uses Ahead-of-Time Compilation (or more precisely, lets you cache an intermediary which can be compiled at run-time). This means that more aggressive optimizations can be applied. And it means that more efficient code can be generated. And it means that code produced by Recki-CT can be run along side (inside technically) another engine.
Right now, the only engine that’s supported is PHP. However there’s nothing stopping porting the JitFu extension to HHVM, which would then enable running compiled code inside of HHVM. And that’s what’s different about this approach. It isn’t all-or-nothing. It’s incremental. You can port individual functions to Recki-CT, while the rest of the application still works 100% on the engine that it’s currently running on!!!
Well, that’s a very difficult question to answer. Based on trivial benchmarks, it’s blindingly fast. It can execute a recursive Fibonacci generator in about the same amount of time as C code can (without GCC’s optimizations turned on that is). But this is something that HHVM can already do well. Can Recki-CT outperform HHVM?
Let’s find out. Using a simple test script, based off of PHP’s own
bench.php, we can compare the relative performance differences of a number of PHP implementations. The numbers are normalized to the fastest implementation (which becomes 1.0). So a result of
45 means it’s 45 times slower than the fastest implementation. And note that parsing (and any compilation that happens prior to calling the function) is not included in the numbers.
The Actual Test Script Used (which is based off of PHP’s internal bench.php). It runs each test 5 times, and throws away the slowest
two runs. Then averages the fastest three to get the final answer. So this should remove the overhead of JIT compilers, and just focus on the last bits of performance that we can squeeze out.
|php 5.5||Recki-CT||hhvm 3.2||hippy-c||qb|
Winner, Within Factor Of 2, Within Factor Of 10, > 10 times slower
Note that the Fail indications for Recki-CT are not true failures, but instead compile errors due to using not-implemented features (in this case, arrays).
But these benchmarks aren’t real. They show off best-case use-cases. They don’t show off how well it’ll work for your application. Only testing your application directly can determine that.
Well, there’s documentation to answer exactly that question!
I also relied very heavily upon a few other open source projects, without which I wouldn’t have been able to do this. I’d like to thank:
- Nikita Popov for his amazing work on PHP-Parser which this project uses heavily.
- Sam Boyer for his awesome graph library Gliph which is a foundation for this project.
Other projects were used, and there are too many individuals to thank each one. Many helped without knowing what they were helping with (in fact, most). So to everyone else, thank you!!! :-D
You can always open bugs, and help document! And if you’re feeling really adventurous, then you could try submitting some code (due note that all code contributions are subject to signing a CLA). Take on a bug! Take on a new optimization! Add a new compiler back-end! Have fun!
Check out the Introduction and FAQ. And if you still have one, reach out to me!
Have fun, and let me know what you think of the project!!!