Swoole‑Compiler v4 Introduces a Native PHP AOT Compiler Boosting Execution Speed Up to 150×

The Swoole‑Compiler v4 adds a native Ahead‑of‑Time (AOT) compiler that transforms PHP scripts into standalone binaries, eliminating the ZendVM interpreter, achieving up to 150× speed gains in intensive calculations such as Fibonacci and π, while detailing supported syntax, limitations, C/C++ interop, real‑world Workerman testing, and future roadmap.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Swoole‑Compiler v4 Introduces a Native PHP AOT Compiler Boosting Execution Speed Up to 150×

Overview

Swoole‑Compiler v4 brings a Native AOT (Ahead‑of‑Time) compiler that can compile PHP code directly into a native binary executable. By bypassing the traditional PHP interpreter, the compiled binary runs at a speed comparable to compiled languages such as Rust or Go, with reported performance improvements of up to several hundred times.

Runtime Mechanism

The AOT compiler does not implement a separate PHP runtime like HHVM or KPHP. Instead it reuses the ZendPHP underlying library and the PHPX compatibility layer, compiling PHP code to machine instructions that share the same ABI as C++ and Go. Consequently the compiler is fully compatible with the entire PHP ecosystem, including extensions, eval, and include/require.

All built‑in functions, classes and constants provided by PHP extensions are callable.

Both static compilation and dynamic interpretation are supported; the eval and include/require constructs continue to work.

Composer packages and autoloading are fully supported.

Syntax Compatibility

The AOT compiler supports the majority of PHP syntax, but because it performs static compilation, several runtime‑dependent features are unavailable:

The $$ variable syntax is not supported.

The extract function cannot create variables at runtime.

Generators ( yield) are not supported; developers should use fiber/swoole/swow coroutines instead.

Multi‑level break or continue must be rewritten as goto or try/catch.

Literal strings containing \0 are prohibited because they are incompatible with C++ string handling.

Function calls with mismatched argument counts cannot be compiled.

Property hook syntax is not supported.

References inside dynamic calls (e.g., closures with reference parameters) must be expressed with refval().

Compilation Process

Running ./swoole_compiler examples/code/main.php triggers the following steps:

./swoole_compiler examples/code/main.php
prepare: examples/code/main.php
prepare completed: 1 source files in total
convert: examples/code/main.php
format: build/examples/code/main.cc
cd /home/swoole/workspace/aot && clang-format -i /home/swoole/workspace/aot/build/examples/code/main.cc
generate stub file: examples/code/main.php
format: build/extension-main.cc
cd /home/swoole/workspace/aot && clang-format -i /home/swoole/workspace/aot/build/extension-main.cc
Starting parallel compilation with 4 jobs for 5 files
Successfully compiled 5 files
g++ ... -o main $(php-config --includes) -I ~/workspace/projects/phpx/include -I /home/swoole/workspace/aot/build/include -I /home/swoole/workspace/aot/src/cpp  -O0 -g -Wall -L $(php-config --prefix)/lib -L ~/workspace/projects/phpx/lib  -lphpx -lphp

The resulting main binary is an ELF 64‑bit executable of about 377 KB, which can be further reduced with upx or strip.

Performance Benchmarks

Two classic CPU‑bound tests were used to compare the AOT compiler with the standard PHP interpreter and with PHP‑JIT.

Fibonacci (recursive)

function fib(int $n): int {
    if ($n == 1 || $n == 2) {
        return 1;
    } else {
        return fib($n - 1) + fib($n - 2);
    }
}

function main(int $argc, array $argv): void {
    $n = $argv[2];
    $begin = microtime(true);
    echo fib($n) . "
";
    echo "Time: " . (microtime(true) - $begin) . "
";
}

Running the script with n=40 produced:

$ ./cli.php examples/fib.php 40
102334155
Time: 14.816216945648193   // PHP interpreter
$ ./fib examples/fib.php 40
102334155
Time: 0.10961484909057617   // AOT compiled binary (≈150× faster)

π Calculation (Leibniz series)

use native_types;
function main() {
    ini_set("precision", 17);
    $rounds = (int) file_get_contents("./rounds.txt", true);
    $stop = $rounds + 2;
    $begin = microtime(true);
    $x = 1.0;
    $pi = 1.0;
    for ($i = 2; $i <= $stop; $i++) {
        $x = -1.0 + 2.0 * ($i & 0x1);
        $pi += $x / (2 * $i - 1);
    }
    $pi *= 4.0;
    print $pi . "
time: " . (microtime(true) - $begin) . "
";
}

With 100 000 000 iterations the AOT binary completed in 0.094 s, whereas the interpreter needed 6.52 s – a 72× improvement.

JIT Comparison

Enabling opcache.jit=On reduced execution time of the same tests by a few multiples, but the AOT binary remained two orders of magnitude faster, confirming that static compilation eliminates interpreter overhead entirely.

Native Types

PHP’s default dynamic typing incurs a performance penalty. By adding use native_types;, integer and float variables are declared as native types, avoiding automatic conversion on overflow and yielding substantial speed gains. However, overflow will truncate precision instead of promoting to float.

New C/C++ Inter‑Call Design

The compiler now allows PHP code to call C++ functions directly. A .stub.php file declares the function signature, while a corresponding .cc file implements it using the PHPX library. Example stub:

function fn_test(int $a, int $b): string {}

Generated C++ implementation:

php::Str php_fn_test(php::Int a, php::Int b) { /* ... */ }

Objects are passed as php::Object &this_, and members are accessed via this_.attr() or this_.call(). This mechanism also supports vectors, allowing efficient bitmap representations for large boolean arrays (e.g., prime sieves) that would otherwise consume tens of gigabytes in pure PHP.

Real‑World Project Test – Workerman

The AOT compiler was applied to the Workerman framework. Minor code adjustments were required (e.g., moving stray code into functions, replacing retval() calls). After compilation, the binary was started with:

./swoole_compiler projects/workerman/src/ -o workerman
./workerman start

The server listened on http://0.0.0.0:2345 and responded with “Hello World”, demonstrating that a full‑featured PHP application can run as a native executable.

Python Inter‑Call Support (Preview)

Future preview releases will allow PHP code to invoke Python modules directly, exposing sys, os, and numpy APIs. This feature is not yet available in the current version.

Release Roadmap

Preview (before 2026‑05‑01): feature showcase, not for production.

Testing (before 2026‑10‑01): most bugs fixed, suitable for non‑critical projects.

Official (before 2027‑05‑01): stable for production use.

Both v3.2 (Opcode encryption) and v4.0 (AOT Native) will be maintained. v4.0 aims to evolve into a new static language that resembles PHP but compiles to native code.

Community Support

Developers can obtain the preview download link via the Swoole‑Compiler WeChat customer service or join the official WeChat group for direct interaction with the Swoole team.

PerformancecompilerPHPbenchmarkAOT
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.