$30 off During Our Annual Pro Sale. View Details »

The Small World Of PHP Engines

The Small World Of PHP Engines

Every PHP developer know the PHP Engine from Zend, and certainly the one from Facebook HHVM. But did you know that it exists almost a dozen of alternatives PHP engines ?

This talk is a trip around the small world of PHP engines, to discover new horizons from the engines made on JVM to the ones that compile PHP directly to binary, and even an embedded one for IoT.

Benoit Jacquemont

February 23, 2017
Tweet

More Decks by Benoit Jacquemont

Other Decks in Programming

Transcript

  1. View Slide

  2. What's a PHP Engine?
    Anything that can run PHP code

    View Slide

  3. Why Creating a PHP
    Engine?

    View Slide

  4. 1. To Increase Performances

    View Slide

  5. 2. To Attract some parts of
    the biggest web dev
    ecosystem

    View Slide

  6. Let's go on a trip!

    View Slide

  7. The Civilized Territories

    View Slide

  8. The Zend Engine
    by
    official PHP Engine
    appeared first in PHP 4
    Zend Engine III for PHP
    7

    View Slide

  9. Zend Engine: How it works
    Execution Time
    PHP Code
    ⬇ compiled to
    Op Code
    ▶ executed by
    Zend VM
    ▶ executed by
    Processor

    View Slide

  10. HHVM
    Hip Hop Virtual Machine
    by
    Perfomances = $$$$$$$

    View Slide

  11. HHVM: How it works
    Execution Time
    PHP Code
    ⬇ compiled to
    Op Code
    ▶ compiled by JIT to
    Machine Code
    ▶ executed by
    Processor

    View Slide

  12. The Binary Compilers
    Back to the roots!

    View Slide

  13. Roadsend PHP
    Direct compiler from PHP to native
    binaries
    Started in 2007, abandonned in 2012
    www.roadsend.com

    View Slide

  14. Roadsend PHP
    How it works
    Compilation Time
    PHP Code
    ⬇ compiled to
    Machine Code in
    Native Binaries
    Execution Time
    Machine Code from
    Native Binaries
    ▶ executed by
    Processor

    View Slide

  15. Hip Hop PHP
    by
    Performances!
    Transpile PHP to C++
    Superseded by HHVM (Hip Hop VM), as not dynamic
    enough (no eval)

    View Slide

  16. Hip Hop PHP
    How it works
    Compilation Time
    PHP Code
    ⬇ transpiled to
    C++ Code
    ⬇ compiled to
    Machine Code in
    Native Binaries
    Execution Time
    Machine Code from
    Native Binaries
    ▶ executed by
    Processor

    View Slide

  17. The Perl Lost World

    View Slide

  18. Perl, What's that?
    Used to be the most popular language for the Web (made
    CGI popular!) around 2000s
    System and development oriented
    Still used a lot as "ops" language
    apt-get, dpkg
    rpm

    View Slide

  19. Introducing Perl 6

    View Slide

  20. Building Perl 6 VM

    View Slide

  21. "Parrot is a virtual machine aimed at running all dynamic
    languages."
    Implementations started for 38 languages, included: Java,
    Lua, Brainfuck, C, Python, Ruby, Perl 6, .... , PHP
    Proudly still in development since 2000
    www.parrot.org

    View Slide

  22. Pipp: Pipp is Parrot's PHP
    Part of the all dynamic languages
    effort
    Last commit: 8 years ago
    github.com/bschmalhofer/pipp

    View Slide

  23. How Pipp works
    Execution Time
    PHP Code
    ⬇ compiled to
    Parrot Code
    ▶ executed by
    Parrot VM
    ▶ executed by
    Processor

    View Slide

  24. The Java Land

    View Slide

  25. Quercus
    "So using PHP with Grails is a quick and painless
    endeavor."
    Java class usage and Unicode compliance
    provide Zend standard exts (cURL, PDO, ...)
    One of the rare alt PHP Engine able to run full
    applications (Drupal, Wikimedia)
    Unfortunately, not actively maintained
    quercus.caucho.com
    $string = new java.lang.StringBuffer("Unicode string ✈");

    View Slide

  26. Quercus: How it works
    Execution Time
    PHP Code
    ⬇ transpiled to
    Java Code
    ▶ compiled by javac to
    Byte Code
    ▶ executed by
    JVM
    ▶ executed by
    Processor

    View Slide

  27. JPHP
    Benefits properly from the JVM
    JIT
    Debug via the xdebug protocol
    1.1x faster than PHP 7
    Wants to allow PHP on Android
    Don't provide Zend standard exts
    Active but not a lot of contributors

    View Slide

  28. JPHP samples: Multithreading
    $thread = new Thread(function () {
    $i = 0;
    while (true) {
    $i++;
    echo $i, "\n";
    sleep(2); // every 2 seconds
    }
    });
    $thread->start(); // start thread

    View Slide

  29. JPHP samples: UI
    $form = new UIForm();
    $form->size = [600, 300];
    $form->title = 'My Program';
    $button = new UIButton();
    $button->text = 'Click Me';
    $button->on('click', function(MouseEvent $e) {
    UIDialog::message('Hello World');
    });
    $form->add($button);
    $form->show();

    View Slide

  30. JPHP
    How it works
    Compilation Time
    PHP Code
    ⬇ compiled to
    Byte Code
    Execution Time
    Byte Code
    ⬇ compiled by JIT to
    Machine Code
    ▶ executed by
    Processor

    View Slide

  31. View Slide

  32. The Snake Country

    View Slide

  33. HippyVM
    started at
    uses (JIT inside)
    Not actively maintained anymore

    View Slide

  34. HippyVM Performances

    View Slide

  35. HippyVM: How it works
    Execution Time
    PHP Code
    ⬇ transpiled by HippyVM to
    RPython
    ▶ compiled by PyPy JIT into
    Machine Code
    ▶ executed by
    Processor

    View Slide

  36. MS World

    View Slide

  37. Phalanger
    "improves the performance, allows to use modern
    environments ... compiles legacy PHP code"
    Not maintained anymore
    phalanger.codeplex.com

    View Slide

  38. Phalanger: how it works
    Compilation Time
    PHP Code
    ⬇ compiled to
    Common
    Intermediary
    Language
    Execution Time
    Common Intermediary
    Language Code
    ⬇ compiled by .Net JIT to
    Machine Code
    ▶ executed by
    Processor

    View Slide

  39. PeachPie
    Superseds Phalanger
    Benefits from the Roslyn compiler platform
    Target PHP 7.1 syntax
    First item in the FAQ:"PHP sucks, why port this
    horrendous language to .NET?"
    www.peachpie.io

    View Slide

  40. PeachPie Benchmarks

    View Slide

  41. PeachPie: how it works
    Compilation Time
    PHP Code
    ⬇ compiled to
    Common
    Intermediary
    Language
    Execution Time
    Common Intermediary
    Language Code
    ⬇ compiled by .Net JIT to
    Machine Code
    ▶ executed by
    Processor

    View Slide

  42. The Land of the Little Ones

    View Slide

  43. PH7 - The Embedded PHP Engine
    Build for real embedded hardware
    Embedded VM similar to Zend VM
    Dedicated to IoT and home routers
    ph7.symisc.net

    View Slide

  44. PH7 - Distinctive Features
    Full type hinting
    Native UTF-8 support.
    Function arguments can take any complex expressions as
    their default values.
    "Correct and consistent implementation of the ternary
    operator."
    echo (true?'true':false?'t':'f');

    View Slide

  45. PH7 code sample
    #define PHP_PROG ""echo 'Welcome guest'.PHP_EOL;"\
    "echo 'Current system time is: '.date('Y-m-d H:i:s').PHP_EOL;"\
    "echo 'and you are running '.php_uname();?>"
    #include
    #include
    #include "ph7.h"
    int main(void) {
    ph7 *pEngine;
    ph7_vm *pVm;
    ph7_init(&pEngine);
    ph7_compile_v2(pEngine, PHP_PROG, -1 &pVm, 0);
    ph7_vm_exec(pVm,0);
    }

    View Slide

  46. PH7 - How it works
    Compilation Time
    C code with PHP code
    inside
    ⬇ compiled to
    Machine Code with PHP
    code inside
    Execution Time
    Machine code with PHP
    Code inside
    ⬇ PHP code compiled
    to
    Op Code
    ▶ executed by
    Embedded VM
    ▶ executed by
    Processor

    View Slide

  47. The Rusted Land

    View Slide

  48. Tagua VM
    Written in Rust
    Security Focused: Safety First!
    Benefits from the LLVM
    platform
    github.com/tagua-vm/tagua-vm

    View Slide

  49. Tagua VM - How it works
    Execution Time
    PHP Code
    ⬇ compiled to
    Op Code
    ▶ executed by
    Tagua VM
    ▶ executed by
    Processor

    View Slide

  50. The Future?
    JIT on Zend Engine
    Implemented as part of opcache
    Uses LLVM as backend
    github.com/zendtech/php-src/tree/zend-
    jit/ext/opcache/jit

    View Slide

  51. Conclusion
    That's a big world out there!
    The Leaders
    Zend Engine
    HHVM
    The
    Challengers
    Peache Pie
    Tagua VM

    View Slide

  52. Thank you!
    Questions?

    View Slide