PHP缓存与编译的问题
根据我的理解,如果您使用 APC、eAccelerator 等 PHP 缓存程序,那么操作码将存储在内存中,以便在后续请求时更快地执行。我的问题是,假设您使用像 phc 甚至 HPHP 这样的编译器(尽管我知道它们在动态构造方面存在问题),为什么编译脚本总是更好/更快?当您可以编译并跳过该步骤时,为什么要费心存储操作码,因为它们必须由 Zend 引擎重新读取,Zend 引擎使用 C 函数来执行它?
from my understanding, if you use a PHP caching program like APC, eAccelerator, etc. then opcodes will be stored in memory for faster execution upon subsequent requests. My question is, why wouldn't it ALWAYS be better/faster to compile your scripts, assuming you're using a compiler like phc or even HPHP (although I know they have issues with dynamic constructs)? Why bother storing opcodes since they have to be re-read by the Zend Engine, which uses C functions to execute it, when you can just compile and skip that step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能简单地编译为 c 并让您的 php 脚本以相同的方式执行。 HPHP 进行真正的编译,但它不支持 php 功能的整个超集。
其他编译器实际上只是在二进制文件中嵌入了 php 解释器,因此您实际上并没有真正编译代码。
PHP 不应该被编译。操作码缓存非常快并且足以满足 99% 的应用程序的需要。如果您有 facebook 级别的流量,并且您已经优化了后端数据库,那么编译可能是提高性能的唯一方法。
PHP 对于 std c 库来说并不是一个薄层。
You cannot simply compile to c and have your php script execute the same way. HPHP does real compilation, but it doesn't support the whole superset of php features.
Other compilers actually just embed a php interpreter in the binary so you aren't really compiling the code anyway.
PHP is not meant to be compiled. opcode caching is very fast and good enough for 99% of applications out there. If you have facebook level of traffic, and you have already optimized your back end db, compilation might be the only way to increase performance.
PHP is not a thin layer to the std c library.
如果 PHP 没有 eval(),则可能可以(相对)轻松地直接进行 PHP 编译的二进制转换。但由于 PHP 本身可以通过 eval() 动态构建/执行脚本,因此不可能创建完整的二进制文件。任何二进制文件都必须包含整个 PHP,因为编译器不知道您的动态代码可以做什么。您将从一个 1 或 2k 的小脚本变成一个巨大的数兆字节的二进制文件。
If PHP didn't have
eval()
, it probably would be possible to do a straight PHP->compiled binary translation with (relative) ease. But since PHP can itself dynamically build/execute scripts on the fly via eval(), it's not possible to do a full-on binary. Any binary would necessarily have to contain the entirety of PHP because the compiler would have no idea what your dynamic code could do. You'd go from a small 1 or 2k script into a massive multi-megabyte binary.