为何OPcache的opcache.max_accelerated_files参数仅接受质数?
opcache.max_accelerated_files? Great question! This choice all boils down to optimizing the performance of the hash table OPcache uses to track cached files. Let’s break it down simply:
Minimize hash collisions
OPcache relies on a hash table to map file identifiers (like paths or inodes) to their cached opcode entries. When figuring out where to store an entry, it uses a modulo operation (hash_value % table_size) to pick a slot in the table. Prime numbers have far fewer divisors than composite numbers, which makes the modulo operation distribute hash values way more evenly across the table. If we used a composite number (say 1000), many hash values would cluster in the same slots, leading to more collisions—where multiple entries have to share a single slot, slowing down lookups significantly.Speed up cache lookups
Fewer collisions mean OPcache spends less time resolving conflicts (like traversing linked lists of entries that landed in the same slot). This directly translates to faster retrieval of cached opcodes, which is the core goal of using OPcache: to speed up PHP script execution.Proven industry best practice
Using prime numbers for hash table sizes is a classic optimization in software engineering. It’s a well-documented way to cut down collision rates, so the PHP team adopted this standard approach for OPcache’s configuration to ensure reliable, high-performance caching.
内容的提问来源于stack exchange,提问作者Oriol Roselló

