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!
4 Responses for "APC Lazy Loading: Initial support"
I couldn’t get this to work, with lazy classes support on; the classes load once and not on the second refresh… not sure what’s up with that..
Any possibility of a version that works with 5.3 stable?
the 5.3 fpm patch set builds against it fine,
I got a 1 of 8 hunk failed with CVS and 5.3 stable version..
any chance there is a patch for 5.3 release? I attempted to manually patch and noticed some of the methodologies found in the patch are adopted in zend_execute_API.c and such. also noticed that lazy loading is available in the latest APC beta, but does not include a current patchset for php. If there is anything I can do to help, let me know :)
THANKS!!!
Hello,
Great work! But the patch you provide does not apply well on the upcoming PHP 5.3.3. Could you please upload an updated version?
Thanks in advance. Keep up the good work.
yes.
this patch doesnt apply to 5.3.2 either…
please please give us an update :D
thanks!
Leave a reply