19

I am trying to find a definite answer to the question that autoload kills performance when using APC and why(benchmarks?)

P.S. Found this link using google/stackoverflow, but I am wondering if this still holds? PHP must been improved to handle this? Because autoload is kind of cool!

Alfred
  • 60,935
  • 33
  • 147
  • 186
  • 1
    Reference: http://stackoverflow.com/questions/1396501/do-php-opcode-cache-work-with-autoload, http://stackoverflow.com/questions/1941541/php5-frameworks-autoloading-and-opcode-caching – Mike B Jan 24 '11 at 23:53
  • Thanks Mike B. I read that topic. I was hoping for some benchmarks. I should update my answer I guess – Alfred Jan 25 '11 at 00:30
  • 2
    Interesting question - I'd never even thought that autoloading would cause issues, and I use a large library that uses a lot of autoloading and it benefits hugely from APC, so my thoughts are that simple autoloading isn't bad for APC. Benchmarks would be interesting though. – Hamish Jan 25 '11 at 03:07

2 Answers2

6

Personally, I don't believe relying on __autoload() is good practice. PHP is a loosely typed language, not a lazily typed language. :)

Check out some performance here:

Rasmus's answer on this (which you also found) was my guidline through all this years:

<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 { } }
seven
  • 2,388
  • 26
  • 28
  • 1
    It still holds on PHP5.3? I would like to see some benchmarks or something backing this up or an article or something! – Alfred Jan 26 '11 at 17:06
  • This is most comprehensive benchmark to date I could find - http://weierophinney.net/matthew/archives/245-Autoloading-Benchmarks.html – seven Jan 31 '11 at 20:14
5

I did some more googling and found this interesting article summarized below:

Benchmarks were run 10 times for each strategy:

enter image description here

Conclusion

Each approach has its merits. During development, you don't want to necessarily run a script to generate the class map or manually update the class map every time you add a new class. That said, if you expect a lot of traffic to your site, it's trivially easy to run a script during deployment to build the class map for you, and thus let you eke out a little extra performance from your application.

Alfred
  • 60,935
  • 33
  • 147
  • 186