Just-in-Time Compiler (JIT)

The first experiments with just-in-time compilation for PHP were made by Dmitry Stogov in 2011. When work on PHP 7 was begun in earnest in 2014, these JIT experiments showed an impressive tenfold increase in performance for synthetic benchmarks. Results for real-world applications such as Wordpress where underwhelming to say the least, though.

Fast forward six years. PHP developers care a lot more about type safety and modern PHP code contains more and more type declarations. This explicit information about types, combined with information from type inference, allow not only more sophisticated bytecode optimization techniques in OpCache (see the “Bytecode Caching” section), but also make just-in-time compilation more relevant. In the meantime, tools for building just-in-time compilers have evolved quite a bit, making it a lot easier to implement a maintainable just-in-time compiler for PHP.

Native code is generated directly from PHP bytecode as well as its Static Single Assignment (SSA) representation generated by OpCache. The just-in-time compiler of PHP 8 is implemented using DynASM, a dynamic assembler for code generation engines that was originally created for LuaJIT, a just-in-time compiler for the Lua programming language. But even with this tooling, just-in-time compilation increases the level of complexity in the PHP runtime’s own codebase and introduces the risk of an entirely new family of bugs, thus increasing the cost of development and maintenance for everyone working on PHP. It will be interesting to see whether the benefits of just-in-time compilation will outweigh these risks.