Why Facebook's hiphop to convert PHP to C + + instead of converting PHP into a compiled language ... Is it faster to execute the compiled file directly?
Reply content:
The first thing to do is to look at the whole history: Facebook has launched the HHVM (HipHop VM) after HipHop (HPHPC), which compiles PHP into C + + and then compiles it to machine code (at runtime), which compiles PHP into machine code. So it is possible to compile PHP into machine code, and Facebook has done so in both HPHPC and HHVM.
(Of course, HHVM to compile PHP into machine code has also undergone several internal forms, but not as hphpc as the use of exposed C + + as the middle form of compilation. )
So the rest of the question is:
1, why HPHPC to choose to compile PHP into C + + and then compile to machine code?
2. When is the machine code compiled? What doesn't make it easy to compile the machine code in advance (AOT)?
Discuss the first question first.
HPHPC is an AOT-compilation (ahead-of-time) scheme that supports a subset of PHP that is actually "compiled". The ultimate goal is to compile PHP into machine code. The practice is to compile to C + +, and then use the ordinary C + + compiler to eventually compile to machine code. This is actually to take C + + as the compiler's IR (intermediate representation, middle) to use, because already have a ready-made C + + compiler can be compiled C + + source code to the machine, equal to the compiler backend (IR-machine code) can be saved , just write a compiler front end (source-and IR). This is the point mentioned in the previous reply.
Is it faster to execute the compiled file directly?
C + + source code is not "direct execution" of Satan. The C + + compiler must be compiled into the machine code. To make things look complete.
Similar schemes have been used in the past to c--
or simply use C as the target language, first compile your own language into c--or C, and then take the ready-made C compiler when the compiler backend is used.
In fact, there is a benefit, that is, Hiphop's runtime is written in C + +: like a Variant
This kind of built-in type is implemented with a C + + class, the most convenient way to link up with such a runtime is to generate C + + code-the rest of the matter is given to the ready-made linker (linker) to solve. C + + is rarely directly linked to target code compiled in other languages, usually C + + will export a set of C interfaces with other native language interactions, and even the target code compiled by different C + + compilers may not necessarily be linked together. Compile PHP into C + + source code will not have their own control link AH compatibility of what the problem.
Curious classmates can go and see what HPHPC generated C + + code looks like. For example, a post on the stack said: what does the C + + output of the HipHop PHP compiler look like?
Let's discuss a second question.
The entire compilation outflow is counted, HPHPC + g++ is the user writes PHP code before the execution of PHP compiled into machine code, is the AOT, and HHVM is in the user write PHP code is executing PHP compiled into a machine code, is a dynamic compiler (generally speaking it is a JIT compiler also line, But strictly speaking it is a bit later than the JIT compiler's work time).
AOT compilation is not easy to handle the PHP source code that is generated/loaded at run time, so it is difficult to support Eval. Usually an AOT scheme to deal with dynamically generated/loaded code, the runtime with an interpreter or a JIT compiler, the runtime's implementation complexity has become a complete VM almost. So HPHPC chooses not to support the dynamic features such as Eval, Create_function ().
And in the Run-time compilation (JIT or dynamic compilation) can be a perfect response to dynamically loaded code, anyway, you dynamically generated I took over the dynamic compilation, Bing will block punches. HHVM has chosen this path, so it can also support a larger subset of PHP.
The disadvantage is that the compilation is done at runtime, so compile time is also counted into the running time, so the compilation is not easy to do the weight of the cost-effective optimization, the JIT compiler to more careful consideration of the cost balance. Because they don't want to re-write the Il to the machine code process, the big trouble. Do you know what a lunatic it is to generate machine code? Xie invited ~ asked the high answer will not say, interpreted language and compiled language in writing, project structure, coding ideas have fundamentally different, if created a PHP is compiled, the cost of the creation of the language is very large not to say, the existing business has been written in PHP, if replaced by the new language, It's better to re-use a mature compiled language like Java write ~ You see, the @vczh invented by the wheel brother is also mostly virtual machine language or compiled to the CLI, generating native code requires a lot of programming work and optimization work, relative to the mature C + + compiler, Re-writing a PHP compiler is obviously a thankless thing. This is too much trouble, they ~ ~ ~