PHP APC - Why is loading cached array op codes slow?

1.2k Views Asked by At

I'm using APC to reduce my loading time for my PHP files. My files load very fast, except for one file where I define more than 100 arrays. This 270 kb file takes 200 ms to load. The rest of the files are full of objects, methods, and functions.

I'm wondering: does OP code caching not work as well for arrays?

My APC cache should be big enough to handle all of my classes. Currently 40% of my cache is free. My hit rate is 99%.

apc.shm_size=32 M
apc.max_file_size = 1M
apc.shm_segments= 1

APC 3.1.6

I'm using PHP 5.2, Apache 2, and Windows Vista.

3

There are 3 best solutions below

0
Harald Brinkhof On

All your arrays need to be serialized when stored in cache and then unserialised again when you load them from cache, this costs time and might be the significant factor of speed loss that you experience. (for your info: Serialisation)

One way to speed up serialisation a bit is to use igbinary, igbinary can be used seamlessly with APC by putting apc.serializer=igbinary in php.ini or in the ini file that goes over APC. (note: this requires APC >= 3.1.7)

You could also put apc.stat (in the same ini file) as 0 so that it only check files for modifications once as opposed to every time.

0
Chris Henry On

One thing about opcode caching is that unless you have it configured correctly, it will continue to stat each file to look for changes. This can cause significant overhead if you need to parse and convert many files to opcode.

You typically get a huge boost in performance by setting apc.stat = 0. However, be aware, that in order to make changes to your code, you'll need to call apc_clear_cache() or restart apache.

http://www.php.net/manual/en/apc.configuration.php#ini.apc.stat

0
Aaron Kreider On

The problem was using the gettext library to translate everything. When I get rid of around 1000 function calls, the load time is reduced from 200 ms to 6 ms.

My guess is that the serialization of the data is also a problem, however it is a secondary one.