3

By my understanding, when PHP encounters include (or require, or their _once versions), it looks up the file in the filesystem and parses it identically to how it would if the code were in the place of the include call (with the exception of return called in the file).

So, on a busy server, it is easily conceivable that many people will be hitting included files (for instance, the file that connects to a database, or defines global functions) repeatedly. Could this have a noticeable impact on performance?

Would it be advantageous to "compile" includes by dumping the contents of the file into the relevant places?

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • if for example the code in the included file is not including other files then the logic can simply be saved in a string and run as eval care fully to avoid heavy FILE IO. – Khurram Ijaz Jul 09 '12 at 05:38
  • That depends on many factors, like how the file system is handling "popular" files, whether those are cached in RAM, disk cache, an opcode cache etc. Only on a very bare bone, non-optimized system would the file actually be read from a physical spinning disk every time... – deceze Jul 09 '12 at 06:25
  • You've got a point! but, assuming that the OS knows how to `cache` effectively, and assuming that the included files are not huge - I believe that it should be a non-issue. – Nir Alfasi Jul 09 '12 at 06:25
  • 1
    @deceze hell, we wrote kinda the same thing about an hour after the question was posted... :) – Nir Alfasi Jul 09 '12 at 06:27

1 Answers1

1

My understanding of include(), require(), and the like is that it works a lot like the C preprocessor #include directive and basically runs all that code as if it were inline in the current file at that position, as you believe.

As some of the above comments have said, if those files are being frequently used (e.g. constantly called via include() ), they are likely sitting in RAM or at least a disk cache.

It's worth nothing that PHP files are essentialy JIT compiled and cached anyway, so you shouldn't notice a performance hit either way. (More detailed info here).

Also, as a sidenote - include_once() and require_once() do have a significant overhead when compared to include() and require(), so if speed is a factor, try to avoid the use of those calls.

Community
  • 1
  • 1
mszaro
  • 1,362
  • 1
  • 13
  • 25