13

抱歉,如果这是基本的,我正在尝试尽可能多地学习 PHP 中的 OO,并且我正在慢慢学习如何使用它(非常有限)。

所以我想知道 __autoload() 是否对 PHP 操作码缓存有任何影响?

4

2 回答 2

27

操作码缓存可以通过自动加载工作(或至少应该工作),但您可能会受到性能影响。

记住:善待字节码缓存

<arnaud_> does autoload have a performance impact when using apc ?
<Rasmus_> it is slow both with and without apc
<Rasmus_> but yes, moreso with apc because anything that is autoloaded is pushed down into the executor
<Rasmus_> so nothing can be cached
<Rasmus_> the script itself is cached of course, but no functions or classes
<Rasmus_> Well, there is no way around that
<Rasmus_> autoload is runtime dependent
<Rasmus_> we have no idea if any autoloaded class should be loaded until the script is executed
<Rasmus_> top-level clean deps would speed things up a lot
<Rasmus_> it's not just autoload
<Rasmus_> it is any sort of class or function declaration that depends on some runtime context
<Rasmus_> if(cond) function foo...
<Rasmus_> if(cond) include file
<Rasmus_> where file has functions and classes 
<Rasmus_> or heaven forbid: function foo() { class bar { } }

以及来自 Ramus 的这封邮件

为了澄清,当然有条件包含的文件会被编译和缓存。问题不在于包含的文件,而是需要在每个请求上重新定义生成的有条件定义的类和函数。这是否重要取决于情况的具体情况,但毫无疑问它会慢一些。例如,它归结为 NOP 与 FETCH_CLASS,NOP 显然要快得多。

于 2009-09-09T01:34:30.990 回答
16

(免责声明:我只知道 APC)

操作码缓存的作用是:

  • 当包含/需要文件时,它采用该文件的完整路径
  • 检查与该文件对应的操作码是否已经在 RAM 中(在操作码缓存中)
    • 如果是,则返回这些操作码以便执行它们
    • 如果否,则加载文件并将其编译为操作码;并将操作码存储在缓存中。

这里的重点是入口点:文件的完整路径。


自动加载通常做的是:

  • 获取一个类的名称
  • 将其转换为文件名
  • 包括/要求该文件

因此,与操作码缓存相关的信息(文件的完整路径,以及包含/必需的事实)仍然存在。

因此,自动加载不应该给操作码缓存带来任何麻烦。

(而且,据我所知,当使用 APC 时,它不会)

于 2009-09-08T21:56:19.023 回答