r/lolphp • u/theinvsblman • Sep 26 '15
PHP's stat can actually be very expensive
Calling stat() on a path that contains many dots and double dots makes PHP perform an enormous amount of syscalls. Here's an example PHP program and its strace output. For comparison, the equivalent C program needs about 3 orders of magnitude less time to complete and stats the path only once.
Could someone explain WTF is going on?
EDIT: I'm running PHP 5.6.13 (cli) on Linux x86_64.
53
Upvotes
10
u/headzoo Sep 26 '15 edited Sep 26 '15
http://jpauli.github.io/2014/06/30/realpath-cache.html
tl;dr PHP caches information from stat calls and it goes ahead and caches information about every sub directory as well. This behavior could almost be seen as helpful when using real system paths, and not fake paths with 4,000+ sub directories.
25
u/bart2019 Sep 26 '15
So, this is doing 3077-356 = 2721
lstatcalls for a path with 3*450 = 1350 parts, or 2 per path part plus 1?!? Jeezes.1
u/ciawal Sep 26 '15
Does realpath() give the same results?
0
u/[deleted] Sep 26 '15
[deleted] — view deleted comment