Recently I posted to the PHP Internals regarding lazy loading support for APC.  I’ve been working on this for a while internally at Facebook, and we’ve actually become quite dependent on this feature.   When you run an include in PHP with APC loaded, you incur a cost of copying opcode and associated data for the compiled files.  APC files are stored in a shared memory segment, but sometimes this data needs to be copied into the local process space.  Lazy loading is a feature that avoids this cost based upon the concept that large code frameworks are only likely to actually require a portion of their code.

While not entirely accurate or proven, a good way to describe this is with the 80-20 rule or Pareto principle which estimates that in many situations 80% of the effects (in this case processor time) are caused by 20% of the causes (code).   With this in mind we can optimize for this situation by delaying copying of functions and classes from the APC cache until we actually need them, thus reducing our number of copies.  Of course this depends entirely upon the structure of the code, for example using the autoloading feature can often significantly reduce these costs as well.  If you’re running code that is very intelligent with it’s includes, you also probably won’t see  a significant gain unless your you have a significant amount of code or are very performance oriented.  It’s been my experience, however, that includes often get out of hand as demonstrated by the inclued graphs that people have generated.  It’s also logical to assume that you won’t always be using every method in a class, or every function in an included file.  As an example, a database include that has read, write, and search functions is likely to only use one call depending on the request.  Sometimes good organization costs us in other un-obvious ways.

To get started with lazy loading you’ll need to get the latest version of APC from CVS, and apply some patches to PHP.   I’ve posted the patch for php-5.3 here: http://tekrat.com/downloads/bits/apc_lazy_php53.patch

Two INI settings enable lazy loading for functions and classes respectively:

apc.lazy_functions=On|Off

apc.lazy_classes=On|Off

This is just a first run, and I will be making some more optimizations in the near future including looking at method level lazy loading and reducing the cost of tracking lazy loaded entries themselves.  I would love to hear any feedback, results, problems you encounter with this feature!