Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do PHP opcode cache work with __autoload?

Sorry if this is basic, I am trying to learn as much as I can about OO in PHP and I am slowly learning how to use it (very limited).

So I am wanting to know if __autoload() has any affect on PHP opcode cache's?

like image 488
JasonDavis Avatar asked Sep 08 '09 21:09

JasonDavis


People also ask

What is PHP OpCode caching?

What are OpCode Caches? OpCode Caches are a performance enhancing extension for PHP. They do this by injecting themselves into the execution life-cycle of PHP and caching the results of the compilation phase for later reuse. It is not uncommon to see a 3x performance increase just by enabling an OpCode cache.

How does OpCode cache work?

OPcache is a caching engine built into PHP. When enabled, it dramatically increases the performance of websites that utilize PHP. From php.net: OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

What is cached by an OpCode cache?

OPcache is a type of caching system that saves precompiled script bytecode in a server's memory called a cache, so each time a user visits a web page, it loads faster.

How do I enable the OpCode cache?

Enabling opcode caching If you do not see the Select PHP Version icon, your server does not support this feature. Select the check box next to the opcode caching extension you want to enable: If you are using PHP version 5.4 or older, select apc. If you are using PHP version 5.5 or newer, select opcache.


2 Answers

Opcode caches work (or at least should work) with autoloading but you can potentially take a performance hit from.

From Remember: be nice to byte code caches:

<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 { } }

and this mail from Ramus:

To clarify, of course conditionally included files get compiled and cached. The issue is not the included files but the resulting conditionally defined classes and functions needing to be redefined on every request. Whether that is significant or not comes down to the specifics of the situation, but there is no doubt that it is slower. It comes down to a NOP vs. a FETCH_CLASS, for example and the NOP is obviously way faster.

like image 187
cletus Avatar answered Oct 06 '22 01:10

cletus


(Disclaimer : I only know APC)

What an opcode cache do is :

  • when a file is included/required, it take the full path to that file
  • check if the opcodes corresponding to that file are already in RAM (in opcode cache)
    • if yes, return those opcode so they are executed
    • if no, load the file and compile it to opcodes ; and store opcodes in cache.

The important point, here, is the entry point : the full path to the file.


What autoloading generally do is :

  • get the name of a class
  • transform it to the name of a file
  • include/require that file

So, the informations that are relevant for the opcode cache (full path to the file, and the fact that it is included/required) are still here.

In consequence, autoload shouldn't bring any trouble with op code caching.

(And, when using APC, it doesn't, as far as I can tell)

like image 44
Pascal MARTIN Avatar answered Oct 06 '22 01:10

Pascal MARTIN