Every include caches a path for each directory above it
One include of one file put fourteen entries in the realpath cache. Re-resolving a thousand already-loaded paths cost 6.6 ms with a small cache and 0.35 ms with the default.
Alden PikeBackend engineer and technical writer

One include, one file, fourteen cache entries:
<?php
include __DIR__ . '/tree/vendor/acme-00/pkg-00/src/Domain/Model/Class0.php';
printf("entries: %d, bytes: %d\n", count(realpath_cache_get()), realpath_cache_size());
entries: 14, bytes: 2464
Thirteen of those entries are directories. The file is one of them, and the other thirteen are every directory between the filesystem root and it — which is what makes this cache’s size a question about the shape of your tree rather than the number of your files.
How these numbers were taken
PHP 8.5.10, NTS, arm64, Homebrew build, on an Apple M4 Pro laptop with 24 GB and 12 logical cores, running macOS and not quiesced. Measurements are CLI, so the per-process argument below is the mechanism rather than an FPM measurement.
The workload is a generated tree: 1,000 small class files in 842 directories,
laid out like a vendor/ directory at four packages deep, with paths of twelve
components. It is synthetic, and a real dependency tree may be shallower or
deeper — the depth is the multiplier, so that difference matters.
Timings are the median of six interleaved runs after three warmup rounds, taken
with hrtime() inside the process, with ranges given. Cache figures are exact
and identical across runs. OPcache is off unless a row says otherwise.
What an entry is
realpath_cache_get() returns the cache as
an array keyed by the path that was looked up:
.../scratchpad/tree/vendor/acme-00 {"is_dir":true,"realpath":"...","expires":1788360709}
.../tree/vendor/acme-00/pkg-00 {"is_dir":true,"realpath":"...","expires":1788360709}
.../pkg-00/src/Domain/Model {"is_dir":true,"realpath":"...","expires":1788360709}
/private/tmp {"is_dir":true,"realpath":"...","expires":1788360709}
A key, an is_dir flag, the resolved absolute path, and an expiry. Resolving a
path means walking it component by component, and every component the walk
touches becomes an entry — including /private/tmp, which is a symlink on macOS
and therefore worth resolving once and remembering.
The consequence is the useful one: the second file in the same directory reuses thirteen of those fourteen entries and adds one. Files that share directories share almost all of their path resolution, and a deep tree costs more per file than a shallow one because there is more chain above each leaf.
Filling it, and running out
Including all 1,000 files at four cache sizes:
realpath_cache_size |
Time | Bytes held | Entries |
|---|---|---|---|
| 16K | 26.746 ms (25.510–27.395) | 16,174 | 72 |
| 64K | 25.100 ms (24.742–25.890) | 65,362 | 280 |
| 256K | 20.657 ms (20.240–21.746) | 261,958 | 1,111 |
| 4096K | 17.197 ms (16.341–21.477) | 436,546 | 1,849 |
Two things are visible at once. The cache fills to whatever it is allowed and stops: 16,174 bytes against a 16K limit, 65,362 against 64K. And the whole working set is 1,849 entries in 436,546 bytes — 1,000 files plus 842 directories plus a handful of roots, at about 236 bytes each — so at 4096K it fits with most of the cache unused.
The time difference across that range is 36%, which is a real number and also the less interesting one, because the first pass is dominated by compiling a thousand files.
The cost lands where nobody looks
Isolate the path resolution by including everything twice. The second pass hits
include_once on files that are already loaded, so it compiles nothing and does
nothing except resolve a thousand paths:
realpath_cache_size |
First pass | Second pass |
|---|---|---|
| 16K | 26.184 ms | 6.620 ms |
| 16K | 26.576 ms | 6.721 ms |
| 16K | 25.526 ms | 6.889 ms |
| 4096K | 16.474 ms | 0.349 ms |
| 4096K | 16.368 ms | 0.383 ms |
| 4096K | 16.605 ms | 0.351 ms |
Three runs each, printed individually because the separation is the point. Nineteen times, on work the process had already done once.
That is what an undersized realpath cache actually costs. Not the first resolution — that has to walk the tree whatever you do — but every subsequent one in the same process, which under a long-lived worker is nearly all of them. The 16K cache held 72 of the 1,849 entries, so 96% of every later lookup was a miss and a fresh walk.
What that is per lookup
Divide it out. A thousand path resolutions in 0.349 ms is 0.35 microseconds each when the entry is in the cache; the same thousand in 6.620 ms is 6.62 microseconds each when it is not. So a miss costs roughly 6.3 microseconds more than a hit on this filesystem, which is one tree walk of twelve components.
That figure is small enough to ignore per lookup and large enough to matter per request, and the multiplier is how many paths a request resolves. A request that includes 186 files resolves rather more than 186 paths, because each one drags its directory chain behind it; on the tree measured here the ratio was 1,849 entries for 1,000 files.
The arithmetic from there is yours rather than mine, and it is worth doing before deciding this is not your problem: a worker serving 100 requests a second, each resolving 500 paths that miss, spends 100 × 500 × 6.3 microseconds — 315 milliseconds of CPU per second of wall clock, on one core, walking directories it walked a moment ago. Halve the miss rate and you halve that. This is an extrapolation from one machine’s numbers and not a measurement of any real application, but it is the shape of the thing, and it is why the cache exists at all.
The default is not what the advice assumes
Almost every tuning list says to raise realpath_cache_size from its small
default. That default changed years ago. With no ini file at all:
php -n -r 'echo ini_get("realpath_cache_size"), " / ", ini_get("realpath_cache_ttl");'
4096K / 120
php.net documents the same: "4M" and "120". It also states the part that
makes the tuning question answerable — the size is bytes, and “does not directly
control the number of distinct paths that can be cached”, because a long path
costs more than a short one. The 236 bytes per entry above is a property of these
paths, not a constant.
So the useful form of the advice is not “raise it”. It is: measure what your application holds, and raise it only if that figure is sitting at the limit.
Two minutes, and then it is gone
The other half of the configuration is the TTL, and 120 seconds is shorter than people expect. With it set to one second:
after include: 14 entries; after 2s with ttl=1: 0 still unexpired
Every entry has its own expires, and an entry past it is re-resolved on the
next lookup. On a default configuration that means a worker handling a request
every few minutes re-walks the tree each time, while a worker handling ten
requests a second effectively never does.
The cache is also per-process. Under PHP-FPM every worker keeps its own, a newly
spawned worker starts with none, and
a pool that recycles workers or scales them on demand
is choosing how often that cost is paid. pm = ondemand with a short idle
timeout is the configuration that pays it most.
What OPcache does to the picture
The working set is not fixed either. The same Symfony request, measured through the built-in server with the cache large enough not to evict:
| Realpath entries | Bytes | |
|---|---|---|
| OPcache off | 271 | 112,782 |
| OPcache on | 35 | 9,755 |
A script served from the opcode cache does not need its path resolved the way a script being compiled does, so the number of distinct paths a warm request touches collapses. Which means the realpath cache matters most in exactly the situations where OPcache is not helping: the first requests after a deploy, a worker that has just started, and any code path that opens files at run time rather than including them.
It is also why this is a different question from
the autoloader’s filesystem probing,
which measured as free. Those probes are file_exists() calls on paths that
mostly do not exist; this cache is about resolving the ones that do, repeatedly,
for the life of the process.
What to do about it
Print the two numbers at the end of a real request:
printf("realpath: %d bytes, %d entries\n", realpath_cache_size(), count(realpath_cache_get()));
If the byte figure is well under your configured limit, the cache is holding everything and there is nothing to tune. If it is sitting at the limit, entries are being evicted and the second-pass column above is what you are paying.
Raise the TTL before the size, on a deployment where files do not change under a
running process. The size decides whether a path is in the cache; the TTL decides
how long, and 120 seconds is short for a worker that lives for hours. Both are
INI_SYSTEM, so neither can be changed from inside a request.
And keep the tree in mind when the number is large. A working set is components,
not files: flattening src/Domain/Model/Entity/Order.php into something shallower
removes entries from every path under it, which is a real effect and almost never
worth reorganising a codebase for. Knowing it is what tells you whether 400 KB or
40 MB is the right expectation for your own application.
Frequently asked
Should I raise realpath_cache_size?
Measure first. The default is already 4 MB, not the 16K most tuning advice was written against, and the tree measured here filled only 436,546 bytes of it. Raise it when realpath_cache_size() at the end of a request is close to your configured limit, which means entries are being evicted.
How do I know what my working set is?
Print realpath_cache_size() and count(realpath_cache_get()) at the end of a real request. The first is the bytes currently held, the second is the number of path components. If the byte figure sits at your limit, the cache is full and evicting.
Why does the cache have more entries than I have files?
Because it stores every directory on the way to each file. A path twelve components deep contributes up to twelve directory entries plus the file, and files that share a directory share those entries - which is why a deep tree costs more than a wide one.
Does the realpath cache survive between requests?
Within one process, yes, until the TTL expires it - 120 seconds by default. It is per-process, so under PHP-FPM each worker has its own and a freshly spawned worker starts empty, which is what makes pm.max_requests and ondemand pools relevant to it.
Alden Pike
Alden Pike writes about PHP internals, performance, architecture and production behavior — the layer beneath the frameworks. Measurements over assumptions, trade-offs over universal rules.
More about the author

