Facebook has introduced a new virtual machine for PHP called the HipHop Virtual Machine (HHVM) that it is sharing with the development community.
In 2010, Facebook came out with HipHop for PHP, a source code transformer, to help support the growing number of Facebook users. PHP developers found that tool useful, and Facebook will be making the new technology available as well. HHVM is 60 percent faster than the current HipHop interpreter with a 90 percent reduction in memory cost.
In a Dec. 9 blog post, Jason Evans, a software engineer on Facebook’s HipHop team, said, “While HipHop has helped us make significant gains in the performance of our code, its reliance on static compilation makes optimizing our code time consuming. We were also compelled to develop a separate PHP interpreter (HPHPI) that requires a lot of effort to maintain. So, early last year, we put together a small team to experiment with dynamic translation of PHP code into native machine code. What followed is a new PHP execution engine based on the HipHop language runtime that we call the HipHop Virtual Machine (HHVM). We’re excited to report that Facebook is now using HHVM as a faster replacement for the original HipHop interpreter (HPHPI), with plans to eventually use HHVM for all PHP execution.”
PHP is widely used in Web development. And Facebook uses HPHPI (and now HHVM) for day-to-day software development, but uses the HipHop compiler (HPHPC) to create optimized binaries that serve the Facebook Website, Evans said.
He added:
““HPHPC is in essence a traditional static compiler that converts PHP→AST→C++→x64. We have long been keenly aware of the limitations to static analysis imposed by such a dynamic language as PHP, not to mention the risks inherent in developing software with HPHPI and deploying with HPHPC. Our experiences with HPHPC led us to start experimenting with dynamic translation to native machine code, also known as just-in-time (JIT) compilation. A dynamic translator can observe data types as the program executes, and generate type-specialized machine code.”Unfortunately we didn’t have a clean model of PHP language semantics built into HipHop, as HPHPC and HPHPI are based directly on two distinct abstract syntax tree (AST) implementations, rather than sharing a unified intermediate representation. Therefore we developed a high-level stack-based virtual machine specifically tailored to PHP that executes HipHop bytecode (HHBC). HHVM uses HPHPC’s PHP→AST implementation and extends the pipeline to PHP→AST→HHBC. We iteratively codeveloped both an interpreter and a dynamic HHBC→x64 translator that seamlessly interoperate, with the primary goal of cleanly supporting translation.”“
Evans said most existing translators use method-at-a-time JIT compilation like Java and C#, though trace-based translation has also been explored in recent systems such as Tamarin and TraceMonkey. However, “We decided to try a very simple form of tracing that limits each trace to a single basic block with known input types,” he said. This “tracelet” approach simplifies the trace cache management problem, because complex application control flow cannot create a combinatorial explosion of traces.
Evans said HHVM is right now about 1.6 times faster than HPHPI for certain functions, but he said he expects the performance to increase over the coming months as the dynamic translator stabilizes and matures.
“In fact, we predict that HHVM will eventually outperform statically compiled binaries in Facebook’s production environment, in part because we are already sharing enough infrastructure with the static compiler that we will soon be able to leverage static analysis results during tracelet creation,” he said.
Meanwhile, Evans said the HipHop Virtual Machine project is about 90 percent done and Facebook will continue to share HipHop via the public GitHub site for HipHop.
“We hope that the PHP community will find HHVM useful as it matures and engage with us to broaden its usefulness through technical discussions, bug reports and code contributions,” Evans said. “We actively monitor the GitHub site and mailing list.”