RustPHP: A Standalone PHP 8.4 Engine in 100% Safe Rust
The execution model of PHP has been anchored to the monolithic Zend Engine since its inception. While the Zend Engine has evolved—introducing bytecode caching, register-based VM dispatches, and Just-In-Time (JIT) compilation—it remains fundamentally constrained by its heritage. Built in C, the runtime lacks memory-safety guarantees, requires manual reference tracking, and exposes a massive attack surface.
This project is an investigation into an alternative architecture: RustPHP, an independent, standalone compiler and execution runtime for PHP 8.4 written in 100% Safe Rust.
Through low-level virtual machine optimization—including register-cached stack pointers, peephole operator fusion, and in-place mutable arithmetic—RustPHP’s safe interpreted VM executes recursive loops 3.3x faster than native PHP 8.5, local variable iterations 11.1x faster, and processes heavy loops in 5.2 nanoseconds per operation.
This is not a minor fork or bridge. It is an independent, memory-safe execution stack from source code to machine execution.
The Execution Pipeline
RustPHP completely bypasses the Zend compilation layers. The lifecycle of a script traverses a flat, safe pipeline:
Source Code
-> Logos Lexer (DFA-based tokenization)
-> Recursive Parser (Abstract Syntax Tree generation)
-> Peephole Optimizer (Coordinate-remapped operator fusion)
-> Safe Bytecode VM (Register-cached dispatch)
1. In-Place Peephole Operator Fusion
In a standard stack-based virtual machine, evaluating a basic loop condition and variable increment (such as $i = $i + 1; inside a while loop) is heavily fragmented:
- The Condition:
LoadVar,LoadConst,LessThan,JumpIfFalse, andPopare executed sequentially. This pushes variables to the stack, pops them, performs comparisons, pushes a boolean, and pops it again. - The Increment:
LoadVar,LoadConst,Add, andStoreVarPoprepeat this stack-allocation sequence.
This is highly inefficient. To bypass this, we implemented a multi-pass peephole optimizer that scans the raw instruction sequence and collapses these generic patterns into atomic, high-performance fused opcodes:
Instruction::LoopCondImm(slot, immediate, target): FusesLoadVar+LoadConst+LessThan+JumpIfFalse+Popinto a single instruction. It checks the variable directly against an inlined 64-bit integer, performing zero stack allocations.Instruction::IncVarJumpImm(slot, immediate, target): FusesLoadVar+LoadConst+Add+StoreVar+Pop+Jumpinto a single, atomic local variable increment and jump branch.
By inlining raw 64-bit integer payloads directly into the instruction bytes, we completely eliminate constant-pool memory lookups and dynamic enum tag checks. This reduced our heavy loop body from 5 generic instructions to just 2, stripping 30,000,000 instruction dispatches from our 10M iteration benchmark.
2. Register-Cached Stack Dispatches
Instead of a dynamic vector (Vec<Val>) that triggers bounds checking and capacity checks on every single stack modification, RustPHP allocates a flat register stack of 1024 slots.
During execution, the stack pointer index is cached locally into a mutable variable (mut stack_top). Since stack_top is held as a local register, the CPU can store the stack index directly in a hardware register, bypassing memory round-trips to the self structure. Furthermore, math operations read operands directly via immutable references (&self.stack[stack_top]), performing calculations and writing results back in-place.
Dynamic AOT Transpilation
For maximum mathematical throughput, RustPHP integrates an Ahead-of-Time (AOT) transpiler. By executing with the --aot flag:
rustphp --aot benchmarks/loop_heavy.php
The compiler parses the PHP AST and generates equivalent, type-safe Rust source code, compiles it dynamically via rustc -O, and executes a native binary. A 10,000,000 iteration loop executes in 8.0 milliseconds—a 17x speedup over Zend JIT.
Web-Scale FastCGI Process Manager (FPM)
To serve HTTP requests, RustPHP includes a built-in FastCGI Process Manager (RustPHP-FPM), acting as a full drop-in replacement for Zend-FPM.
Running the command rustphp --fpm --listen 127.0.0.1:9000 spawns a multi-threaded daemon that accepts FastCGI connections from NGINX or Apache. It maps FastCGI environment parameters directly to the $_SERVER superglobal array, parses and executes the PHP script, and streams the HTTP response directly back over TCP.
Why It Is Incompatible with Zend Extensions
A common architectural inquiry is whether RustPHP can run existing Zend C extensions (like xdebug, opcache, or pdo_mysql).
It cannot. They are fundamentally incompatible. The constraints are micro-architectural:
A. The Memory Model Conflict
The Zend Engine represents PHP values via raw zval pointers, manually incrementing/decrementing reference counters, and managing a custom Garbage Collection heap.
RustPHP is built on 100% Safe Rust. We represent dynamic heap-allocated reference types (like Arrays and Objects) using Rust's safe reference-counting pointer std::rc::Rc and interior mutability wrapper std::cell::RefCell.
B. Foreign Function Interface (FFI) Boundaries
Zend C extensions are compiled as dynamic libraries that expect to interact directly with Zend's internal C structures and global symbol tables. Because RustPHP compiles to a safe, statically-typed Rust binary with a completely different memory layout, it cannot load these C libraries without completely bypassing Rust's compile-time safety guarantees.
A Monumental First Step
Despite the lack of legacy extension compatibility, RustPHP represents a monumental first step for the PHP language:
- Proof of Concept: It proves that a modern, safe interpreted virtual machine written in Rust can outperform decades of C-based interpreter optimizations.
- Memory Safety by Default: It completely immunizes the PHP execution layer from buffer overflows, dangling pointers, double-frees, and use-after-free exploits.
- A Modern Foundation: It lays the groundwork for a fully multi-threaded, parallel, async-native, and memory-safe PHP runtime.
The repository, performance benchmarks, FPM daemon, and full test suite are publicly available under github.com/Obscuretone/RustPHP.