PHP5 Frameworks: Autoloading and Opcode Caching

2020-02-05 17:59发布

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?

1条回答
forever°为你锁心
2楼-- · 2020-02-05 18:04

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

查看更多
登录 后发表回答