增量编译缓存

TypePHP 的二次编译速度,很大程度上取决于对象文件缓存是否命中。编译器对两类源文件做内容指纹缓存:phpx 的 misc 源,以及编译器自己生成的 .cc 单元。

Sources: src/Translator.php

两类缓存

方法适用对象缓存键组成
hasMiscObjectFileCache()phpx/src/misc/*.cc编译命令 + PHP ABI(PHP_VERSION_ID / ZEND_MODULE_API_NO / PHP_ZTS / PHP_INT_SIZE 等)+ phpx 头文件 mtime
hasGeneratedObjectFileCache()构建目录下的生成 .cc编译命令 + PHP ABI + 所有生成头文件内容指纹func_decl.h / data_decl.h / 各 _arginfo.h

Sources: src/Translator.php · src/Translator.php

缓存键构造

hash('sha256',
    buildCompileFileCommand(source, object)   // 可复现的编译命令行
    . "\0" . serialize($abi)                    // PHP ABI 快照
    [. "\0" . header . "\0" . hash_file(header)] // 仅生成单元:逐头文件内容
)

键写入 <object>.typephp-cache 元数据文件(getMiscObjectCacheMetadataFile())。每次 compileFile() 先比较元数据文件中的键,再比较 .o 与源/头的 mtime——任一不满足即失效并重编译

Sources: src/Translator.php · src/Translator.php

设计要点

Sources: src/Translator.php · src/Translator.php

相关阅读