phpoptimizationapcopcode-cache

Basics of PHP opcode cache


Currently on a very large project that I do not plan to re-use for another site, I have the site's name hardcoded into the files everywhere. Now, if I were ever to change the site name it would take a lot of effort to change that everywhere. I know the obvious solution is to just store the name as a variable or a constant, but I guess you could call it my micro-optimizing way of thinking: I always figured it would be one less thing PHP has to parse. I do realize it won't make much difference, but I just wanted to know whether using an opcode cache like APC would mean that PHP wouldn't even have to re-parse that?


Solution

  • Really : you shouldn't care about anything like that.

    Any difference in configuration will means much more difference (for instance, the apc.stat option, for APC, can have quite an impact on the load of your server -- and anything like DB queries you do will have hundreds of time more impact)

    Here, what probably matters is maintenability :

    If the answer is "no" in either case, and your application works... well, that's what matters !


    If you have time to spend with that kind of less than micro optimisations, it would probably be better spent going through your application code with a profiler, going through your DB queries, the number of HTTP requests you are doing to fetch static JS/CSS/images, upgrading PHP or modifying your code so it can run on PHP 5.3 (as PHP 5.3 comes with some optimisations over 5.2), ...

    All those will most probably get you a higher gain ;-)


    Edit after the comment :

    Basically, when a PHP file is loaded :

    With an opcode cache :

    The apc.stat option defines whether APC should examine the last modification date/time of a file to decide between using the opcodes from RAM, or re-compiling the file if it is more recent that the opcodes in RAM.

    Disabling this option means :


    Still, what I said is true : there are probably lots of things you can optimise that will mean more important gain than a simple "should I use hard-coded values" versus "should I use constants/variables".