PHP5框架:自动加载和操作码缓存 [英] PHP5 Frameworks: Autoloading and Opcode Caching

查看:145
本文介绍了PHP5框架:自动加载和操作码缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多框架利用 spl_autoload_register()来动态加载类(即控制器和模型)。关于自动加载和操作码缓存的问题有几个帖子。特别是有一篇文章有​​@cletus的回应,其中引用了@Rasmus对于那些利用APC作为操作码缓存的人们做出的许多陈述,这些陈述被证明是不合情理的:



似乎没有任何关于自动加载的替代方法的讨论,它们不会影响操作码缓存的性能。

有没有办法避免自动加载的类不会被添加到字节代码缓存中?



如果没有,是否有任何其他方法可以动态加载将被缓存的类?

解决方案



一个很好的邮件列表线程可以用来表示这个主题。阅读Zend Frameworks邮件列表中的这一个:

http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html


<现在,相关性在这里,因为如果你从尚未定义的
类继承,你可能依靠自动加载来定义它(尽管你也可能
依赖include),实际上存在的自动加载设施可能
鼓励你使用这种继承ANCE。但这不是自动加载的
,它带来了麻烦(请参阅Ramus的它不仅仅是在
博客中为某些麻烦事例子自动加载)。
因此,正确的短语是倾向于依赖自动加载的人倾向于
也使用违反编译时绑定的代码。当然,这不能被视为
作为自动加载错误,并且只是避免自动加载并不会对
bit有帮助 - 您还必须重写代码,以便
编译 - 时间绑定可能会发生。例如,它与
自动加载与新的使用无关。

至于上述影响的放缓 - 即没有
的编译时绑定 - 代码确实变得有点慢,而这样的
代码可能会导致一些不太明显的情况给操作码缓存
带来一些麻烦(不是在自动载入的情况下 - 但是在条件中定义了
的类或者上帝禁止的情况下,创建了不同的定义
取决于条件) - 但它本身与使用
自动加载无关。
然而,放缓的数额似乎被
人大大地夸大了 - 这是什么也没有(并且我重复说明 - NOTHING )将
与由于缺少
的磁盘操作和编译阶段,操作码缓存给出了性能优势。你大概可以
构成一个人为的基准,它会显示一些重要的
放缓,但我不相信任何真正的应用程序都会注意到。

来源: http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html#a640092



A number of frameworks utilize spl_autoload_register() for dynamically loading classes (i.e. controllers and models). There are a couple of posts on the issue of autoloading and opcode caching. One post in particular has a response by @cletus which references @Rasmus making a number of statements which prove to be unsavoury for those utilizing APC as an opcode cache:

There does not appear to be any discussion as to any possible alternatives to autoloading which do not affect opcode cache performance.

Is there a way to get around the fact autoloaded classes do not get added to the byte code cache?

If not, are there any alternative methods for dynamically loading classes which will get cached?

解决方案

There seems to still be confusion about this topic, however in most cases it comes down to ease vs performance.

A good mailing list thread to read would be this one on Zend Frameworks mailing list:

http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html

Now, the correlation is here because if you inherit from not-yet-defined class, you might rely on autoload to define it (though you might also rely on include), and actually the presence of the autoload facility may encourage you to use such inheritance. But this is not the autoload which brings trouble (see after Ramus' "it's not just autoload" in the blog for some examples of troublesome things). So the right phrase would be "people which tend to rely on autoload tend also to use code which defies compile-time binding". Which can't be seen as autoload fault, of course, and just avoiding autoload won't help a bit with that - you would also have to rewrite your code so that compile-time binding could happen. And it has nothing to do with uses of autoload with "new", for example.

As for the slowdown from the effects described above - i.e., absence of the compile-time binding - the code indeed becomes a bit slower and such code can lead in some obscure cases to some trouble to opcode caches (not in the autoload cases - but in cases where classes are defined inside conditions, or, God forbid, different definition is created depending on condition) - but it has next to nothing to do with using autoload by itself. The amount of slowdown, however, seem to be greatly exagerrated by people - it is nothing (and I repeat to be clear - NOTHING) compared to the performance benefit given by the opcode cache due to the absence of the disk operations and compilation stage. You could probably compose an artificial benchmark that would show some significant slowdown, but I do not believe any real application would even notice.

source: http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html#a640092

这篇关于PHP5框架:自动加载和操作码缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆