TypePHP 编译器
https://swoole.com/aot/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
2.4 KiB
46 lines
2.4 KiB
<blog>
|
|
|
|
# 构建速度与性能
|
|
|
|
TypePHP 的「编译速度」包含两个维度:**编译期(PHP→C++→二进制)自身耗时** 与 **生成产物的运行性能**。本章聚焦前者——哪些环节最慢,以及已落地的优化。
|
|
|
|
Sources: [docs/AOT_BUILD_SPEED_RESEARCH.md](docs/AOT_BUILD_SPEED_RESEARCH.md) · [docs/aot-optimization-priority.md](docs/aot-optimization-priority.md)
|
|
|
|
## 时间都花在哪
|
|
|
|
编译期主要成本:
|
|
|
|
1. **PHP 解析**:每个文件被多次解析(预处理一次、转换一次)——这是主要冗余;
|
|
2. **C++ 翻译**:`doConvert()` 遍历 AST 并发射 C++;
|
|
3. **C++ 编译**:大量 `.cc` 单元编译,受 PCH 与并行度影响;
|
|
4. **链接**:最终链接往往是大项目瓶颈之一。
|
|
|
|
Sources: [docs/AOT_BUILD_SPEED_RESEARCH.md](docs/AOT_BUILD_SPEED_RESEARCH.md) · [src/Translator.php](src/Translator.php#L2455-L2459)
|
|
|
|
## 已落地的优化
|
|
|
|
| 优化 | 说明 | 位置 |
|
|
|---|---|---|
|
|
| **S1 合并解析** | 同一文件 AST 在进程内缓存复用(`parseCachedAst`),消除重复解析 | `src/Translator.php` `parseCachedAst()` |
|
|
| **S2 增量对象缓存** | 生成单元按「编译命令 + PHP ABI + 生成头内容指纹」缓存 `.o`,改动一处不必全量重编 | [增量编译缓存](incremental-cache) |
|
|
| **PCH 预编译头** | 预编译 `phpx.h` 等全局头,加速每个编译单元 | `src/Build/PrecompiledHeaderManager.php` |
|
|
| **并行编译** | `-j` 用 `pcntl` 多进程编译 | `src/Translator.php` `compileWithPcntl()` |
|
|
| **misc 缓存** | phpx 运行时源按 ABI 指纹缓存 | `hasMiscObjectFileCache()` |
|
|
|
|
Sources: [src/Translator.php](src/Translator.php#L1116-L1160) · [src/Translator.php](src/Translator.php#L1449-L1487) · [src/Translator.php](src/Translator.php#L1584-L1637)
|
|
|
|
## 调试与剖析
|
|
|
|
- `--dry`:只生成 C++,跳过编译/链接,用于检查翻译与定位;
|
|
- `--profile`:启用 gperftools 剖析(仅 Linux),并在 `./tpc app.prof` 模式调 `pprof --web`;
|
|
- `--no-progress` / `-j`:控制进度展示与并行度。
|
|
|
|
Sources: [src/compiler.php](src/compiler.php#L48-L73) · [src/Translator.php](src/Translator.php#L30-L36) · [src/Translator.php](src/Translator.php#L251-L255)
|
|
|
|
## 相关阅读
|
|
|
|
- 缓存机制细节 → [增量编译缓存](incremental-cache)
|
|
- 优化器如何提升产物性能 → [优化器](optimizer)
|
|
- 优先级规划 → `docs/aot-optimization-priority.md`
|
|
|
|
</blog>
|
|
|