PHP → C AOT 编译器

用 PHP 语法
写原生二进制

TinyPHP 把 PHP 代码(强类型子集)编译为原生 C,再由 GCC/Clang/TCC 生成二进制。无 Zend VM、无 OPCache、零运行时依赖,性能提升 300-500 倍。

300-500x性能提升
18-36x数组遍历
16B对象头大小
312+内置函数

核心特性

六大核心能力,让 PHP 写出原生级性能

AOT 编译

PHP 源码直接编译为原生 C,再由 GCC/Clang/TCC 生成二进制。无 Zend VM、无 OPCache、零运行时依赖。

三编译器四平台

TCC 亚秒编译,GCC/Clang -O2 再提速 3-10 倍。支持 Windows/Linux/macOS x86_64 与 aarch64。

COS 风格对象系统

对象头仅 16 字节,struct 嵌套继承零开销,VTable 直接函数指针调用。比 PHP 对象精简 5 倍。

多线程支持

Thread/Mutex/CondVar/WaitGroup OOP API,基于 tinycthread。Thread-Local 运行时,多线程无锁竞争。

C 互操作 PHPC

完整的 PHP ↔ C 双向互操作:C->func() 直接调用,C.Type 类型注解,数组/对象/回调互操作,defer 资源管理。

扩展系统

对标 PHP extension,#import 按需引入。已内置 pcntl、posix、openssl、pcre、pdo、sqlite3、stream 等。

代码示例

几分钟从 PHP 写到原生二进制

  • Hello World
  • C 互操作
  • 多线程
<?php
class Main {
    public function main(): void {
        echo "hello world\n";
    }
}
$ php tphp.php hello.php  →  $ ./hello  →  hello world
#include "my_func.h"

function add(int $a, int $b): C.int {
    return C->my_add(c_int($a), c_int($b));
}

class Main {
    public function main(): void {
        int $r = C->my_add(c_int(10), c_int(20));
        var_dump($r);  // int(30)
    }
}
<?php
class Main {
    public function main(): void {
        $t = new Thread(function(): int {
            echo "hello from thread\n";
            return 42;
        });
        $t->start();
        echo "ret=" . $t->join() . "\n";  // ret=42
    }
}

内存优化

从对象头到字符串池,每一字节都精打细算

SSO 小字符串

24B 内联缓冲区,≤23 字节零堆分配。

128KB 字符串池

bump allocator + Arena,O(1) 分配。

128 槽数组/对象复用池

LIFO + 1.5× 增长,热路径零 malloc。

ROPE 多片段拼接

编译期展平为单次分配,concat-4 提速 6 倍。

Thread-Local 运行时

每线程独立内存池,无锁竞争。

泛型数组紧凑存储

array<int> 比 array<mixed> 节省 67% 内存。

开始用 PHP 写原生二进制

下载源码,几分钟内编译出你的第一个原生 PHP 程序。