Live data from Hacker News

PHP "require" Performance

gazehawk.com

1–10 of 38 posts

Re: PHP "require" Performance

#3
post #2

There's a really nice discussion on Stack Overflow about this: http://stackoverflow.com/questions/186338/why-is-require-onc...

The require vs. require_once was a byproduct of this research: I was really interested in __autoload vs. require, as that seems to have fewer numbers online for it.

Re: PHP "require" Performance

#4
One way to improve cpu-bound performance is to switch to something like HipHop for PHP [1], which compiles PHP down to C++ and runs that. Granted, this incurs a non-trivial switching cost.

However, it's scary to me that someone is suggesting the potentially fatal-inducing use of "require" over "require_once" (if you call it twice on the same file, you end up re-defining whatever classes or methods were required and the script dies). It seems like optimizing for developer time and simplicity over cpu utilization is better here. Using HipHop would incur a one-time operational cost, but I suspect it would be worth the additional developer and speed gains.

[1]: https://github.com/facebook/hiphop-php/wiki/

Re: PHP "require" Performance

#5
There should really be no reason why __autoload() should be significantly slower than require_once(). It's even trivially easy to ensure your classes are __autoloaded()'d from absolute paths to make opcode caches happy.

Re: PHP "require" Performance

#6
post #4

One way to improve cpu-bound performance is to switch to something like HipHop for PHP [1], which compiles PHP down to C++ and runs that. Granted, this incurs a non-trivial switching cost. However, it's scary to me that someone is suggesting the potentially fatal-inducing use of "require" over "require_once" (if you call it twice on the same file, you end up re-defining whatever classes or methods were required and t…

By using __autoload and a class/file naming convention, you can get away with using just require, and not having to write require statements manually. Decreased time for developers, and near optimal load times.

Re: PHP "require" Performance

#9
post #5

There should really be no reason why __autoload() should be significantly slower than require_once(). It's even trivially easy to ensure your classes are __autoloaded()'d from absolute paths to make opcode caches happy.

My initial thought was "surely it's just a quick hash lookup", but then require_once is probably using the canonical path for that lookup, which involves calling realpath() or abspath(), at that point all bets are probably off, especially in a shared hosting environments involving things like NFS.

Edit: I looked at the implementation. require_once/include_once unconditionally opens the file, whether it has been loaded already or not. Attempting to follow that path through about 10 levels of indirection (seriously), at some point the PHP implementation passes its notion of the path back to Zend, which suggests possible abspath() etc. Also somewhere in PHP's streams.c I see it unconditionally seek()ing the new file too.

If nothing else, then the layers of crap I just trawled through might cause some slowness, if not some heavy library call like abspath().

I just remembered why I stopped using PHP. :)

Re: PHP "require" Performance

#10
post #7

Hmm, PHP 5.2+ is still that slow with require_once ? I thought they fixed that. Anyway, just use function_exists instead.

Yup, we're on 5.3. May have something to do with the relative vs. absolute paths (we use relative), but still...

Turns out we didn't need require_once anyway, all but a couple of includes in our entire codebase were in that one area.

Post reply on HN