Introduction to the open-source Facebook project Hiphop-php (original)
Author: Yu Chao EMail: yuchao86@gmail.com
Facebook's open-source Hiphop-php can compile php into a C ++ program to improve the running efficiency of the site. The latest source code of hiphop-php is available on github.
Haiping Zhao (Zhao Haiping), creator of Hiphop-php, defines Hiphop as follows:
HipHop is a source code converter. It converts PHP code to highly optimized C ++ code and then compiles it using the g ++ compiler. It can execute source code in a semantic equivalent, but sacrifices some features that are rarely used, such as eval (). To further improve the performance, HipHop includes a code transformer, a re-implemented PHP runtime system, and uses these performance optimization advantages to rewrite many common PHP extensions.
There are many problems encountered during compilation, and many packages need to be installed in the middle, but they are basically carried out according to the wiki steps:
========================================================== ========================================================== =====
1. install the associated library. During the compilation process, you can install other packages by running the sudo yum-y install command:
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ ls
Bin CMake CMakeCache.txt CMakeFiles CMakeLists.txt configure doc LICENSE. php license. ZEND local phpt README. md src
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ pwd
/Home/yuchao/dev/hiphop-php
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ sudo yum-y install git cmake boost pcre-devel libicu-devel \
Libmcrypt-devel oniguruma-devel mysql-devel gd-devel boost-devel \
Libxml2-devel libcap-devel binutils-devel flex bison \
Expat-devel
========================================================== ========================================================== =====
2. Download The Hiphop-php installation package and the dependent libraries libevent and curl. Requests that can run from binary executable files depend on both libraries.
1) obtain the source code of Hiphop-php.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ git clone git: // github.com/facebook/hiphop-php.git
2) obtain various libraries required for compilation, such as re2c, tbb, curl, and libevent.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ wget "http://downloads.sourceforge.net/project/re2c/re2c/0.13.5/re2c-0.13.5.tar.gz? Use_mirror = cdnetworks-us-2"
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ wget "http://www.threadingbuildingblocks.org/uploads/77/142/2.2/tbb22_20090809oss_src.tgz"
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ wget "http://curl.haxx.se/download/curl-7.20.0.tar.bz2"
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ wget "http://www.monkey.org /~ Provos/libevent-1.4.13-stable.tar.gz"
3. decompress the downloaded dependency packages. The GZ package uses xvzf and the bz2 package uses xvjf.
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ tar xvjf curl-7.20.0.tar.bz2
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ tar xvzf libevent-1.4.13-stable.tar.gz
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ tar xvzf re2c-0.13.5.tar.gz
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ tar xvzf tbb22_20090809oss_src.tgz
4. Install and decompress various dependent packages. If an error occurs during compilation, make an error according to the prompt information so that you can learn more.
1) install the Intel parallel thread Library
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ CD tbb22_20090809oss
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ gmake
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cp./build/* _ release/*. so. */usr/lib/
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ ldconfig
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd ..
2) install re2c embedded lexical parsing tools that are more efficient than lex.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd re2c-0.13.5
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ mkdir-p/usr/local/re2c
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $./configure-prefix =/usr/local/re2c
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ make & make install
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd ..
3) install the event trigger mechanism for processing network I/O, timer, and signal processing library libevent,
Refer to my other article libevent source code analysis http://blog.csdn.net/cyberexp2008/article/details/5674693
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd libevent-1.4.13-stable
You must patch the libevent version in the hiphop-php/src/third_party/directory.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cp ../hiphop-php/src/third_party/libevent-1.4.13.fb-changes.diff ./
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ patch-p1 <libevent-1.4.13.fb-changes.diff
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $./configure -- prefix =/usr/local/libevent/
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ make & make install
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd ..
4) install CURL, a file transfer tool used to process URL syntax in the command line mode. For more information, see Google.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd curl-7.20.0
You must also patch the libcurl version in the hiphop-php/src/third_party/directory.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cp ../hiphop-php/src/third_party/libcurl. fb-changes.diff ./
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ patch-p1 <libcurl. fb-changes.diff
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $./configure -- prefix =/usr/local/curl
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ make & make install
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ cd ..
========================================================== ========================================================== ============
5. Next, install hiphop-php. Before official compilation, you must set the CMAKE_PREFIX_PATH, HPHP_HOME, and HPHP_LIB environment variables.
1) set The environment variables. cmake searches for The paths of libevent and curl. If The settings are incorrect, I will prompt CMake Error: The following variables are used in this project during compilation, but they are set to NOTFOUND. please set them or make sure they are set and tested correctly in the CMake files:
The error message of CCLIENT_INCLUDE_PATH (ADVANCED). troubleshoot the error by yourself.
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ export CMAKE_PREFIX_PATH =/usr/local/libevent/
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ export CMAKE_PREFIX_PATH = $ CMAKE_PREFIX_PATH:/usr/local/curl/
2) install hiphop-php
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ CD hiphop-PHP
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ git submodule init
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ git submodule update
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ export hphp_home = 'pwd'
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ export hphp_lib = 'pwd'/bin
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ cmake.
[Yuchao @ yuchao-Latitude-E5410 hiphop-PHP] $ make
========================================================== ========================================================
After the installation is complete, we can start to use the hphp command. Below is a simple test of a PHP file, the Code is as follows:
For details about how to run the PHP compiling file, refer to the following:
Https://github.com/facebook/hiphop-php/wiki/Running-HipHop
<? PHP
$ I = 0;
For ($ J = 0; $ j <1000000; $ J ++)
$ I + = $ J;
Echo $ I, "N ";
?>
Compile with hphp:
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ hphp test. php -- keep-tempdir = 1 -- log = 3
Prompt to generate a new executable file
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $/tmp/hphp_c9sbnG/program
Compare the running time:
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ time php test. php
499999500000
Real 0m0. 307 s
User 0m0. 299 s
Sys 0m0. 007 s
[Yuchao @ yuchao-Latitude-E5410 hiphop-php] $ time/tmp/hphp_c9sbnG/program
499999500000
Real 0m0. 140 s
User 0m0. 076 s
Sys 0m0. 006 s
As you can see, the execution time of php compiled by hiphop is almost doubled. HEREBY DECLARE
References:
Http://www.ioncannon.net/programming/918/building-hiphop-php-for-fedora-12-on-64-bit-and-32-bit-systems/.
Https://github.com/facebook/hiphop-php/wiki/building-and-installing.
Https://github.com/facebook/hiphop-php/wiki/Installing-or-Building-HipHop-PHP-via-RPM-on-CentOS-5.