How does OPcache max_accelerated_files actually work?

Viewed 1900

Maybe this is stupid question but i'm trying to figure out how does max_accelerated_files actually work...

I understand the "description/instructions" from PHP net

opcache.max_accelerated_files integer The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 100000 in PHP < 5.5.6, and 1000000 in later versions.

But my question is what it does with this number once is configured. Does it allocate a memory for this setting? why don't we just set number 1000000 and that's it if we have enough memory? What happens if we let say configure this number to 2000 and we have 2010 files? Do they some sort stack and once its that file turn it gets cached? What happens with un cached files?

Thank you for your answers

1 Answers

OPCache stores cached scripts in an HashTable, a data structure with very fast lookup time (on average), so cached scripts can be retrieved quickly. max_accelerated_files represent the maximum number of keys that can be stored in this HashTable. You could think about it as the max number of keys in an associative array. The memory allocated for this is part of the shared memory that OPCache can use, that you can configure it with the opcache.memory_consumption option. When OPCache tries to cache more scripts than the available number of keys, it triggers a restart and clean the current cache.

So let's just say you configured opcache.max_accelerated_files to 223 and a request to your /home route parse and cache into OPCache 200 scripts. As long as your next requests will need only those 200 scripts OPCache is fine. But if one of the following requests parse 24 new scripts, OPCache triggers a restart to make room for caching those. Since you don't want that to happen you should monitor OPCache and choose an appropriate number.

Keep in mind that one file can be cached more than once with different keys if required with a relative path like require include.php or require ../../include.php. The cleanest solution to avoid this is to use a proper autoload.

Related