live and let live
Thursday, 25 August 2016
A virtual machine designed for executing programs written in Hack and PHP. http://hhvm.com
HHVM is an open-source virtual machine designed for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.
Our getting started guide provides a slightly more detailed introduction as well as links to more information.
We'd love to have your help in making HHVM better. If you're interested, please read our guide to contributing.
HHVM is licensed under the PHP and Zend licenses except as otherwise noted.
The Hack typechecker (hphp/hack) is licensed under the BSD license (hphp/hack/LICENSE) with an additional grant of patent rights (hphp/hack/PATENTS) except as otherwise noted.
See Reporting Crashes for helpful tips on how to report crashes in an actionable manner.
Reporting and Fixing Security Issues
Please do not open GitHub issues or pull requests - this makes the problem immediately visible to everyone, including malicious actors. Security issues in HHVM can be safely reported via HHVM's Whitehat Bug Bounty program:
HHVM提升PHP性能的途徑，採用的方式就是替代Zend引擎來生成和執行PHP的中間字節碼（HHVM生成自己格式的中間字節碼），執行時通過JIT（Just In Time，即時編譯是種軟件優化技術，指在運行時才會去編譯字節碼為機器碼）轉為機器碼執行。Zend引擎默認做法，是先編譯為opcode，然後再逐條執行，通常每條指令對應的是C語言級別的函數。如果我們產生大量重複的opcode（純PHP寫的代碼和函數），對應的則是Zend多次逐條執行這些C代碼。而JIT所做的則是更進一步，將大量重複執行的字節碼在運行的時候編譯為機器碼，達到提高執行效率的目的。通常，觸發JIT的條件是代碼或者函數被多次重複調用。
PHP 7 vs. HHVM Comparison
Let's look at PHP 7 and Hip Hop Virtual Machine: pros and cons of each, benchmarks, and more.
PHP is one of the most popular scripting languages used for web development. The latest version of PHP, PHP 7 is a new version of the language that is been optimized for fast performance. However, PHP has a rival in HHVM (HipHop Virtual Machine) — a virtual tool that executes PHP code. The competition between these two options is heating up, so let’s take a look at the performance that each can offer.
What is HHVM?
In 2008, Facebook started working on a tool to convert PHP script into C++ so it could be compiled and executed on web servers. The aim was to conserve server resources, an important goal, as Facebook’s user base was growing rapidly. In this sense, the project was a success; it allowed the server to accommodate between five and six times more traffic than it had managed before.
Fast-forward a couple years to 2010. Facebook’s server needs had grown even more, placing it in a position to require another innovation to allow it to operate more efficiently. In response to this demand, Facebook developed the HipHop Virtual Machine (HHVM).
HHVM uses Just-In-Time (JIT) compilation to convert PHP code into a type of bytecode. It then converts this bytecode into machine code and optimizes it so that it runs as quickly as possible.
What is PHP 7?
PHP 7 is the PHP community’s response to HHVM. Early announcements of the launch of PHP 7 claimed that it would offer better than 100 percent performance improvements over the previous version of the language, PHP 5.
You might be wondering why PHP skipped version 6. The answer is that development on PHP 6 began in 2005, but it went on so long and ran into so many problems that PHP 6 had developed a bad reputation long before it was ready for release. As a result, the PHP community decided to skip the name PHP 6 and go straight to PHP 7 for the new working version of the language.
The real question is not how PHP 7 compares to PHP 5, as it is pretty clear that PHP 7 offers speedier performance. Instead, the consideration is how PHP 7 compares to HHVM. Many experts have conducted tests on the two ways of handling PHP code, which have revealed some interesting results.
PHP 7 vs. HHVM: Similarities and Differences
Before answering the “which is better” question, let’s take a look at the key differences between PHP 7 and HHVM, as well as the ways in which they are similar.
The fundamental difference between PHP 7 and HHVM is the way in which each one interprets PHP code. PHP 7 uses the standard PHP interpreter, free software that is available for anyone to use, to directly interpret and execute PHP code on the server. This generates HTML code, which is then sent to the client. The client then displays the desired content to the web user.
In contrast, the Hip Hop Virtual Machine first converts PHP code into HipHop bytecode. This code is then translated into machine code and executed. Some optimization takes place during this translation, ironing out inefficiencies in PHP code with the aim of delivering faster performance.
Both the PHP interpreter and HHVM take PHP code and execute it. Therefore, the process of writing the code is pretty much the same in each case. However, if you want to use HHVM, you need to install it on your server and then call it using the hhvm command on the command line.
HHVM has offered much faster performance than previous versions of PHP. However, recent benchmark tests suggest that PHP 7 is slightly faster than HHVM, at least in some situations. Let’s take a look at the results of some benchmark testingconducted by Kinsta.
WordPress: Running on WordPress 4.1.1, PHP 7 allows more than twice as many requests to be executed per second as PHP 5.6. However, it still doesn’t process quite as many as HHVM 3.6.1, which executed 624 requests per second in the test compared to just 604 executed each second by PHP 7.
Drupal: PHP 7 offers a distinct advantage over HHVM for Drupal users. PHP 7 can handle 37 percent more server requests per second compared to HHVM on Drupal 8.
Which Companies Use HHVM?
In addition to Facebook, which developed HHVM, many other businesses have adopted this solution to running PHP applications on their own servers. These include Wikimedia and the e-commerce site Etsy.
Wikimedia: Wikimedia hosts a huge range of educational content, including the famous Wikipedia online encyclopedia. Attracting nearly half a billion Internet users each month, Wikimedia needs to optimize server performance to cope with its high level of demand. HHVM poses a significant advantage over PHP in that it can load multiple SPU cores simultaneously whereas PHP is a single-threaded language that can’t be parallelized. According to Wikimedia, deploying HHVM shrank CPU load from 50 to just 10 percent, halved the mean time taken to respond to users submitting edits and reduced the average page load time from 1.3 seconds to just 0.9 seconds.
Etsy: With 54 million users, Etsy’s servers also face significant demands. Etsy engineers compared HHVM to PHP 5.4 and found that HHVM could cope with up to 280 server requests per second whereas the response time of PHP 5.4 started to dramatically increase once the number of requests grew beyond 190 per second.
What Does the Future Hold for PHP 7 and HHVM?
PHP 7 is due for stable release in November 2015. Therefore, companies are not yet using the new language, but promising benchmark test results of the performance of the beta version of PHP 7 could tempt more companies to adopt the new version of the language.
The future looks bright for PHP 7, but what about HHVM? It is likely that it is far from dead. Many businesses are already using HHVM to increase performance on their sites. The transition between PHP and HHVM is not instantaneous. It took Etsy more than six months to complete the transition. With the speed benefits of PHP 7 compared to HHVM being only very slight, it is unlikely that businesses will rush to switch back to PHP.
Why Does the HHVM vs. PHP 7 Competition Matter to PHP Shop Owners?
As an online store owner, you need to make your decision on whether to use PHP 7 or HHVM based on the platform that hosts your shop. For example, if your site is built using WordPress, take a look at benchmark tests for HHVM and PHP 7 to find out how the latest release of each one performs. You want to choose the solution that can offer the biggest reduction in page load times, server response times and CPU usage.
Reasons to Choose HHVM
HHVM uses dynamic translation to deliver faster performance in many situations, including on WordPress.
HHVM uses less memory to process each request in cases where it faces a very large number of requests.
HHVM developers are steadily increasing the number of PHP code bases that the engine can run. It can already run the latest version of WordPress, along with many other common PHP frameworks and applications.
HHVM is open source. Even though HHVM has been developed by Facebook, it is open source, which means the source code is available to anyone who wants to use or alter it.
Reasons to Choose PHP 7
PHP 7 performs faster than HHVM in some situations, including when running on Drupal 8.
Using PHP 7 doesn’t require you to install or setup HHVM.
Code written in PHP 5 should work as expected after a transition to PHP 7, although some features of PHP 4 code are no longer supported in the new release. In practice, this means that any code created in the last decade is probably ready for the transition to PHP 7.
PHP 7 is developed by the PHP community, a group with a long-standing reputation for creating stable and reliable PHP releases.
HHVM vs. PHP 7: Make Your Choice
Don’t agonize for too long over the decision. Kinsta recommends that online businesses choose quickly between PHP 7 and HHVM. The sooner you make your decision, the sooner you can begin to implement the solution, allowing you to optimize your website performance. A poorly performing website can cause your reputation to suffer, which can be difficult to reverse.
Both HHVM and PHP 7 offer significant benefits compared to older versions of PHP. Make your choice and start the process of switching your site to the new system as soon as possible。
An Introduction to HHVM
just-in-time (JIT) compiler. In these respects, HHVM has similarities to virtual machines for other languages including C#/CLR and Java/JVM.
A little big of history
In early 2008 Facebook began working on HipHop(now HPHP), a PHP execution engine; its original motivation was to convert Facebook massive PHP code base into C++ in order to save resources and increase the application performance. The original release was known as HPHPc a PHP to C++ compiler.
For next 2 years Facebook continued working on HipHop adding HPHPi ( a 'developer mode' version of HPHP) and HipHop debugger known as HPHPd, this allowed developers to watch and step through the code and interactively debug PHP applications running on the HipHop platform.
At it's peak, HipHop PHP code showed up to 6x times better performance than its ZEND counterpart.However, there where several drawbacks to this first iteration of HipHop:
HPHPc did not fully supported the PHP language, most noticeably the lack of support for create_function() and eval() --Honestly, I don't see the lack of support for the last one as something bad-- functions.
Facebook developers had to maintain two difference engines (HPHPc and HPHPi) with resulted in duplication of efforts and waste of resources.
Finally, HPHPc required a vastly different deployment process which will hurt adoption by PHPdevelopers.
In light of these problems Facebook took two key actions on early 2010, the first was to open source the HipHop platform. -- Open sourcing a project like this is a great way to build a community around the project and getting external help from that community --/
At the same time Facebook started the development of the modern version of HipHop, known as HHVM (HipHop Virtual Machine), HHVM improves the strengths of HPHPc and corrects many of the critical problems.
HHVM was build on top of HPHPc and it works by converting PHP code into high level bytecode(an intermediate language) this bytecode is the translated into machine code dynamically at runtime by a JIT (Just-In-Time) compiler.
If you are like me you probably have a vague recollection of the concepts of bytecode, machine code and Just-In-Time compilers, so let's take a momentarily side step and quickly review these concepts and how the they play a key role on HHVM.
Bytecode, Machine code, JIT, Oh my ...!
Bytecode: Is a non human readable code designed for efficient execution by an interpreter or compiler.When HHVM first loads our project it converts all the PHP code into this intermediary form of bytecode; the bytecode generate is not particular to any type of architecture and could be portable among different systems.
Machine code: Are a set of instructions designed to be executed directly by the CPU. If you ever played with Assembly (Who hasn't !?) you know very well what machine code is, for those very few of you who haven't had the pleasure of working on assembly or similar languages; machine code is generated by the compiler or interpreter and then handed to the CPU
JIT (Just In Time) compiler: Just in time compilation is a technique used to improve performance of software, this is achieved by compiling bytecode during execution, the byte code is store in memory and then the JIT compiler loads and compiles as many sections of the byte code as needed.
The performance and speed gained by applying these techniques is what gives HipHop and subsequently HHVM its core strengths. Keeping a PHP code base while achieving performance comparable to compiled applications.
Currently HHVM supports PHP 5.4 almost on its entirety, however there are still numerous bugs that prevent some applications from running, for that reason Facebook has set as goal to have the top 20 open source PHP applications running on HHVM. The first popular application to achieve this was Wordpress.
So now that we have a better understanding of what HHVM does and the advantages if running it we can start testing our applications on HHVM. In a subsequent post I will be covering the setup of a dedicated Vhost for HHVM, running benchmarks against your applications and eventually (fingers crossed) how to run Magento on HHVM.