HipHop for PHP

This is a discussion on HipHop for PHP within the Tech Board forums, part of the Community Boards category; http://wiki.github.com/facebook/hiphop-php/ Perhaps I'm just naive, but can someone explain to me how translating PHP to C++ and then compiling it ...

  1. #1
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951

    HipHop for PHP

    http://wiki.github.com/facebook/hiphop-php/

    Perhaps I'm just naive, but can someone explain to me how translating PHP to C++ and then compiling it could be faster than just interpreting?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Epy
    Perhaps I'm just naive, but can someone explain to me how translating PHP to C++ and then compiling it could be faster than just interpreting?
    Because by interpreting prior to and at compile time instead of run time, you err... save on interpreting high level source code at run time.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    I think there's some disconnect in my mind here...

    1) Compiled languages: source code -> machine code prior to execution, at execution machine code is executed directly by CPU
    2) Interpreted languages: source code read line-by-line -> line-by-line execution of different commands
    3) JIT, i.e. Java: source code -> bytecode prior to execution, at execution bytecode -> dynamic translation to machine code by VM

    Is HipHop taking the PHP and translating into C++ so that you can compile it and basically use it like a CGI file later? It's making it sound like the program translates PHP to C++ and compiles it right then and there, right when it's needed.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Epy
    Is HipHop taking the PHP and translating into C++ so that you can compile it and basically use it like a CGI file later?
    I don't have any experience with HipHop, but take a look at Running HipHop. The instructions get to the point where the reader is supposed to have a compiled binary (from the given PHP source) that is run.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    Ah, so it seems that it takes one or more php files and combines them into a single executable, a standalone server.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Epy View Post
    2) Interpreted languages: source code read line-by-line -> line-by-line execution of different commands
    I think only some interpreters work exactly that way. I'd guess the only common examples would be shells, but it perhaps includes php too.

    Major implementations of Perl, python, (and I would have assumed php, but maybe not) et. al. first compile the source into bytecode, then execute it. When you run a script from the command line, both these stages happen one after another, which is why you can receive distinct compile errors. vs. actual runtime errors.

    However, they don't have to. For example, using CGI, a server executes scripts the same way you would from the command line; this allows you to change the script without stopping the server (or having privileges to access it) but it is slower. The normative apache php module is the equivalent of this.

    But if you "load" the script into the server at start-up (using some other method, eg, with apache, mod_perl, and I think mod_python does the same thing), it and the bytecode interpreter are compiled into the server. Hence, you will get compile time errors on the source scripts when the server starts, and changing those scripts while the server is running will not accomplish anything, since they are not used subsequently.

    It looks to me like hiphop is a method for doing this with php, but independent of any server or interpreter, because it uses machine code. Sounds like a good idea, but...

    Quote Originally Posted by Epy View Post
    Ah, so it seems that it takes one or more php files and combines them into a single executable, a standalone server.
    The question is, does that amount to anything different than writing a stand-alone server in php and just running that? In perl or python, I don't think it would too much, altho bytecode != machine code. However, it would still be a convenience, because it would save writing the server part. Unfortunately, if the server created is a single synchronous process, its use value is limited -- a single slow connection will hamstring the whole. That's probably fine for a system daemon tho.

    So it would probably be better to have a mod_hiphop, even tho this ties you to apache or whatever.
    Last edited by MK27; 12-13-2011 at 06:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    If you start digging, it says that speedup is anywhere from 2-6x, and the memory footprint is reduced by 90% if you can believe that.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Epy View Post
    If you start digging, it says that speedup is anywhere from 2-6x, and the memory footprint is reduced by 90% if you can believe that.
    It's very easy to believe. Mod_perl and mod_python are much faster than CGI for the same reason*. PHP has never been considered high performance by anyone, it just simplifies development.

    The fact remains: a single process synchronous server is not suitable for deployment online unless you have little or no traffic. Ie, whatever facebook is doing, they are not putting a hiphop app out front -- it is tiered in somewhere. For Joe Average, web developer, it would be more useful as a second generation mod_php, because you must have concurrent request handling, either via multi-threading, forking, or asynchronous queuing.

    Of course, if Joe Average can write his own threaded/forking/asynch server in php and compile it with hiphop, then he's set.

    * likewise, because perl and python both use intermediate bytecode, you could easily write a high performance threaded or async server from scratch that way. I've been working on an async server with a perl interface for a few months; altho a lot of the core is in C, it still all depends on the perl interpreter. It benchmarks about 80% the speed of a multi-threaded apache (on a dual core system) with a high level of concurrency -- as a single process without threads (ie, using only one core), and uses about 10% of the memory in a minimal configuration.
    Last edited by MK27; 12-13-2011 at 01:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    It's funny, I feel like I already know a lot about computers/programming/etc, but at the same time there's so much more to learn.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21