# realpath() caching

- Thread: https://externals.io/message/12919
- Original archive: https://news-web.php.net/php.internals/12919
- Started by Andi Gutmans on 2004-09-21T01:28:08+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #12919 — Andi Gutmans — 2004-09-21T01:28:08+00:00

URL: https://externals.io/message/12919#12919

Hi,

Attached is the promised patch to do `realpath()` caching.
You can set cache size and TTL via the php.ini (default is 2 minutes TTL 
and 16K memory).
It doesn't use the SAPI `time()` but uses the syscall `time()` 
because it seemed to be slightly faster but we can adjust it to use the 
SAPI one if anyone measures a difference in performance (might depend on 
the OS).

Feedback welcome.
Also any feedback on the VM patch? I'd like to commit it soon but I haven't 
heard anything from this list (people were saying they wanted to review it).

Thanks,
Andi

---

## #12920 — Andi Gutmans — 2004-09-21T02:24:19+00:00

URL: https://externals.io/message/12919#12920 · In reply to: #12919

Thanks for pointing out that the attachment didn't make it through. You can 
find it at:
http://snaps.php.net/~andi/realpath_cache2.diff

Andi

At 06:28 PM 9/20/2004 -0700, Andi Gutmans wrote:
>Hi,
>
>Attached is the promised patch to do `realpath()` caching.
>You can set cache size and TTL via the php.ini (default is 2 minutes TTL 
>and 16K memory).
>It doesn't use the SAPI `time()` but uses the syscall `time()` because it 
>seemed to be slightly faster but we can adjust it to use the SAPI one if 
>anyone measures a difference in performance (might depend on the OS).
>
>Feedback welcome.
>Also any feedback on the VM patch? I'd like to commit it soon but I 
>haven't heard anything from this list (people were saying they wanted to 
>review it).
>
>Thanks,
>Andi

---

## #12924 — Andi Gutmans — 2004-09-21T19:02:15+00:00

URL: https://externals.io/message/12919#12924 · In reply to: #12919

I was asked to provide some more info about this patch.
In general, PHP does a `realpath()` on files (most importantly included 
files) so that when include_once() and other PHP functionality needs to 
know if two file paths are identical it will give a definite answer. 
 `realpath()` itself is quite a slow function (especially on some specific OS) 
and therefore should be saved when possible.
As it is rare that a `realpath()` for a certain file path + cwd combination 
ever changes, it's a perfect candidate for caching (actually a `realpath()` 
can only really change if you are adding/changing symlinks).
Therefore, this patch implements caching of `realpath()` calls. There are two 
INI settings which can be changed:
realpath_cache_size - Size of cache (default's to 16K; if set to 0 it turns 
of caching).
realpath_cache_ttl - Time to live for cache entry (default 120 seconds; if 
set to 0 then doesn't refresh).

On a loaded side even a small TTL should do the trick, although it might 
make sense to raise the TTL to something much higher than 120 seconds 
because as I said, it doesn't usually change.
Hope that's enough info.
Andi




At 07:24 PM 9/20/2004 -0700, Andi Gutmans wrote:
>Thanks for pointing out that the attachment didn't make it through. You 
>can find it at:
>http://snaps.php.net/~andi/realpath_cache2.diff
>
>Andi
>
>At 06:28 PM 9/20/2004 -0700, Andi Gutmans wrote:
> > […]

---

## #12930 — Sara Golemon — 2004-09-21T23:09:53+00:00

URL: https://externals.io/message/12919#12930 · In reply to: #12924

> realpath_cache_ttl - Time to live for cache entry (default 120 seconds; if
> set to 0 then doesn't refresh).
>
Ah, didn't grok the TTL at first glance....  that fairly well negates the
need for an explicit flush.... If the FS layout needs to change all one
would have to do is turn down the TTL, make the change, and turn it back up
after the cache is sure to be cleared...

-Sara

---

## #12932 — Christian Schneider — 2004-09-22T09:52:41+00:00

URL: https://externals.io/message/12919#12932 · In reply to: #12930

Sara Golemon wrote:
> > […]
> Ah, didn't grok the TTL at first glance....  that fairly well negates the
> need for an explicit flush.... If the FS layout needs to change all one
> would have to do is turn down the TTL, make the change, and turn it back up
> after the cache is sure to be cleared...

Hmm.. if I understand correctly then one can only turn it down to 1 
second as 0 means unlimited. So if an application changes the tree it 
would have to sleep(1) for it to happen.

I'd strongly suggest that we have an explicit flush option. And no, I 
don't think restarting the server is an opton as this can't be done from 
inside an application.

BTW: This needs to be well documented as running into caching problems 
can cause a very annoying debugging sessions due to the Heisenbug 
nature. Happened to more than one person I know with `filesize()` with 
missing `clearstatcache()` calls. One of PHP's main feature is programmer 
efficiency so we should be careful to add optimizations which make 
things harder for programmers.

- Chris

---

## #12933 — Elfyn McBratney — 2004-09-22T11:01:23+00:00

URL: https://externals.io/message/12919#12933 · In reply to: #12932

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

Btw, cool patch. :-)  I'll see if we can start testing this in an experimental 
ebuild in Gentoo land.


> Sara Golemon wrote:
> > […]
>
> Hmm.. if I understand correctly then one can only turn it down to 1
> second as 0 means unlimited. So if an application changes the tree it
> would have to sleep(1) for it to happen.

In real life, that would only mean a 'broken' path for 1 second.  And changing 
path's on a production system doesn't happen all that regularly, in my 
experience.

On busy sites, a lot of users could get that broken file (not exist, 
whatever), but if you don't code `realpath()` file i/o related correctly i.e., 
checking the file open'ed ok at least, (and in the `stat()`/is_*() case) 
checking the file size, you're toast anyway. ;-)

> I'd strongly suggest that we have an explicit flush option. And no, I
> don't think restarting the server is an opton as this can't be done from
> inside an application.

Same here, if it counts. ;-)  For those short-by-1-second cases, and during 
development, t'would be handy.

Elfyn

- -- 
Elfyn McBratney
beu on irc.freenode.net/savannah.[non]gnu.org

PGP Key ID: 0x456548B4
PGP Key Fingerprint:
  29D5 91BB 8748 7CC9 650F 31FE 6888 0C2A 4565 48B4
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBUVuDaIgMKkVlSLQRAvhiAJ9IGQtNBi1T+XNIddkEbxWblgrfAACfXeGr
sbhCUYI5tascMi95g9Is+ug=
=z+yd
-----END PGP SIGNATURE
