3

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.

Aaron Kreider
  • 1,705
  • 5
  • 22
  • 44
  • You mean hard-coded arrays like lists of cities, zipcodes, etc? – Mike B Jul 03 '12 at 00:49
  • I do have to ask, why would you load 270 kB of arrays on every request? Stick that stuff in a database, or at least split it into multiple class files and load them on demand with an autoloader. – Ilmari Karonen Jul 03 '12 at 11:12
  • I should probably use multiple files. I wasn't putting it in the database because my mysql server and web server are on different computers and there is a 35 ms ping time. A lot of the file is an array of 3000 counties in the US. But interestingly, that largest array doesn't take that long to load. So I could break it into smaller files, but then knowing when they need to get loaded is hard. – Aaron Kreider Jul 03 '12 at 18:36
  • Update: loading the large county array (180 kb) takes 4 ms. The rest of the constants (90 kb) take 196ms to load. – Aaron Kreider Jul 03 '12 at 18:45
  • Update: the problem was using the gettext library to translate everything. When I get rid of around 1000 function calls, the load time is reduced to 6 ms. – Aaron Kreider Jul 03 '12 at 19:13

3 Answers3

2

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.

Harald Brinkhof
  • 4,375
  • 1
  • 22
  • 32
1

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

Chris Henry
  • 11,914
  • 3
  • 30
  • 31
0

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.

Aaron Kreider
  • 1,705
  • 5
  • 22
  • 44