Skip to content / Aller au contenu
php127.hazyhaar.fr
Theme / Thème

How php127 works

This article follows a PHP script from its source text to its execution in php127, then describes how the memory of a request is managed. The descriptions are taken from the code of revision 11f6d94, package by package. The few measurements quoted come from the execution loop diagnostic of 2026-11-20 (diagnostic-boucle.out.md), carried out on an earlier revision, e0a4a88.

From text to intermediate code

A script goes through four packages, in this order.

The lexer (lexer) splits the text into tokens as it reads, following the token catalogue of PHP 8.3.6. Scanning the bytes is handed to kernels generated by the sgoiter tool, and keyword recognition to tables computed when the binary is built, not at run time.

The parser (parser) builds the PHP 8.3 syntax tree (ast). Invalid syntax returns a syntax error, and a fault detected after parsing returns a compile error; in both cases no partial tree is returned.

The compiler (compiler) translates the tree into intermediate code. The rule is the same: the first error stops code generation, and no partial program comes out.

The intermediate code format (bytecode) defines the instruction set, the program format and a stack verifier, which checks the program before it runs.

To make this concrete, here is what the compiler emits for a for loop counting to five million, as the diagnostic recorded it with a disassembler written for the occasion:

LoadCV i ; Const 5000000 ; IsSmaller ; JmpZ end ; [body] ; PostIncCV i ; Jmp start

Local variables, including those of the main script, are numbered slots (the "CVs"), with no symbol table to look up.

The virtual machine

The vm package runs this code in the arena and with the services of the current request. It is a stack machine: each instruction takes its operands from a stack and pushes its result onto it. A function call f($i) is thus written InitCall ; LoadCV i ; SendVal ; DoCall ; Pop on the caller's side.

A stack machine executes more instructions than a register machine for the same work. php127 partly makes up for this by fusing some instruction sequences at run time: the sequence LoadCV, Const, IsSmaller, JmpZ of the loop above is recognised and handled in one go. The diagnostic of 2026-11-20 showed that this recognition, redone on every pass, is itself costly; the article on speed comes back to it.

Each call creates a frame; on revision e0a4a88, the diagnostic of 2026-11-20 measured it at 272 bytes, and the pending call description at 168 bytes. These sizes do not measure 11f6d94 or 5b31e86. The diagnostic recorded a signal check every 1024 instructions, not at each instruction.

In server mode, FastCGI or built-in HTTP, php127 keeps compiled programs in memory from one request to the next (sapi/fpm/cache.go): a script is not recompiled on every call. This cache is internal to the runtime. It should not be confused with PHP's opcache extension, which is absent from php127 and whose tests are excluded from the conformance target.

The memory of a request

This is where php127 departs clearly from the C interpreter.

The zone package provides a memory arena per request, which its documentation describes as deterministic. PHP strings, arrays and objects are not Go objects handed to the garbage collector: they are stored in the arena and referred to by a handle. A PHP value (types.Value) takes 16 bytes, like PHP's zval; it carries a type, flags, and either the value itself for a scalar or the handle that leads into the arena.

Objects have their own reference count, a handle checked by a generation number, and a destructor called exactly once (types). Reference cycles, which a counter alone does not free, are handled by an iterative cycle collector of the Bacon-Rajan kind (types/gc.go).

When the request ends, Reset runs the pending clean-up callbacks and resets the arena (zone/zone.go). This does not describe every process state: the compiled-program cache persists between requests (sapi/fpm/cache.go), as does data written to files, sessions or databases.

This choice has two measurable effects in the diagnostic of 2026-11-20. The Go garbage collector weighs little: Go allocation accounts for 3% of the concatenation profile, and garbage collector marking does not show up in it. On the other hand, every access to an arena value goes through decoding and a bounds check, and that cost shows in array reads and in strings. It is one of the causes of slowness covered in the article on speed.

Extensions and services

Extensions, such as mysqli, pdo, GD, intl or zip, are Go packages brought together by extmux, which aggregates them without depending on their implementation. The contracts between the engine and its services are declared in runtimeapi. The mysqlpilote driver speaks the MySQL network protocol without a C library, and sqlitepilote adapts a pure-Go implementation of SQLite. PDO offers the sqlite and mysql drivers, and no others (pdo/engine.go).

The sapi package provides PHP's HTTP functions and a server with isolated requests; sapi/fpm reads pool files in the PHP-FPM format. Streams and file operations (streams), sessions (session) and regular expressions (pcre) keep their state per request.

What this architecture does not guarantee

Isolation by arena does not replace a memory limit that holds. A record of 2026-11-20 notes that a test from the official suite made php127 exceed 24 GiB (dettes.md): the memory_limit directive did not yet bound that case. Likewise, separation between two applications does not come from php127 alone. On the site that publishes this blog, it relies on two separate processes, under two system accounts, each in a hardened systemd unit.

Lire en français