KPHP is a PHP compiler developed by VK.com and maintained as proprietary for years until open-sourced in late 2020. It compiles a limited subset of PHP to a native binary running 3-10 times faster than PHP ! The solution analyzes your whole project, translates PHP sources to C++ equivalent, then compiles/links the resulting C++ code and runs it within an embedded HTTP server.

It’s a transpiler or transcompiler, as it translates code from a language to another, but we can also call it compiler as it generates directly a linux binary. Some limitations of compatibility in the compiler take their roots from C++, they are similar to all compiled languages. In PHP, if you made an error in your code, you’ll see it only when the execution point reaches that line. In KPHP, you are unable to build your site until you fix all errors.

KPHP Limitations

  • It doesn’t support features that can’t be compiled, such as calling by name or mocks.
  • It won’t compile code, that breaks the type system, for instance, mixing numbers and objects in an array.
  • It doesn’t have PHP features such as SPL classes and XML parsing.
  • Some PHP syntax details just weren’t implemented, like generators and anonymous classes.

Usually code compiled in KPHP acts the same way as it was interpreted with PHP, however there are some cases where the compiler behavior is really different from the PHP compiler. For example if you have strange math operations with invalid argument types; for example, if you subtract a number from an array, or if you calculate the tangent of a literal string. Also if standard library functions invoked with invalid arguments, like calling substr() with non-numeric limits… etc. It’s unlikely to face misbehavior affecting production, but still, test a compiled site before deploying.

Features in KPHP that are not in PHP

The compiler has features that does not exists in PHP, most of them are dedicated to compile-time checks and runtime performance :

  • strict typing, type inferring, type checks, no ZVAL, much faster
  • async programming: coroutines
  • shared memory across requests with special PHP API to use it
  • compiler checks: constantness, immutability, checked exceptions, and others — via annotations
  • embedded web server, process orchestration, graceful restart and more
  • compile-time optimizations: inlining, constants extracting, pre-compiled visitors, read-only detection…
  • runtime optimizations: typed vectors, SIMD, script allocators, stack variables…

Benchmarks

KPHP cannot be compared to PHP as it does not support all its features, however when it does it can provides you great performance. Which make it an interesting alternative for applications with lots of Math for example or in Machine learning models. Generally, when your code fits best practices, it runs 3–10 times faster than PHP.

KPHPStorm IDE plugin

To make development easier, a plugin for PhpStorm IDE was developed. It makes IDE understand KPHP specifics. You can get the plugin from PHPStorm plugins repository. KPHPStorm greatly improves coding accuracy, indicating if you have missed typing or if types are inconsistent.
You see these errors immediately, not on compilation fail.

KPHPStorm KPHP IDE plugin

Basic usage and deployment

You can use Docker to quickly get KPHP up and running and test its capabilities :

docker pull vkcom/kphp

However for production it’s recommended to use Deb packages :

# for Ubuntu
sudo wget -O /etc/apt/trusted.gpg.d/vkpartner.asc https://repo.vkpartner.ru/GPG-KEY.pub
echo "deb [arch=amd64] https://repo.vkpartner.ru/kphp-focal focal main" | sudo tee /etc/apt/sources.list.d/vkpartner.list

sudo apt update
sudo apt install kphp vk-tl-tools

sudo mkdir -p /var/www/vkontakte/data/www/vkontakte.com/tl/
sudo tl-compiler -e /var/www/vkontakte/data/www/vkontakte.com/tl/scheme.tlo /usr/share/vkontakte/tl-files/common.tl /usr/share/vkontakte/tl-files/tl.tl

Then to run the compiled application, a typical architecture is quite usual: one master process which performs service operations, and many worker processes that handle incoming HTTP requests. The Master open HTTP port on start, or in case of graceful restart take them from the server, which is being shut down. Then manage worker processes, Collect server stats, aggregate stats from workers, and push everything into statsd service, and finally handle service queries.

Usually, you’ll set up nginx in front of the compiled application. Then KPHP is just a proxy_pass backend for nginx. You can also combine it with PHP and profit from both solutions, for example, file uploading can remain PHP-based, whereas API business logic works on KPHP.

KPHP open source software, released under the GPLv3 license, on behalf of VK.com. More information at https://github.com/vkcom/kphp