PHP opcode opcode basics

Currently on a very large project that I don't plan on reusing for another site, I have the site name hardcoded into files all over the place. Now, if I ever change the name of the site, it would take a lot of effort to change it all over the place. I know the obvious solution is to just store the name as a variable or constant, but I think you could call that my micro-optimizing way of thinking: I always figured this would be the least PHP needs to parse. I do understand that it won't make much of a difference, but I just wanted to know if using an opcode cache like APC would mean PHP doesn't even have to re-parse it?

+2


source to share


2 answers


Indeed: you shouldn't care about something like that.

Any difference in configuration will mean a lot more of a difference (for example, a setting apc.stat

for APC can have a big impact on the load on your server - and anything, like database queries, you will have hundreds of times more impact)

Here the totality is probably important:

  • Are you getting any benefit (other than this nano optimization) that the site name is not hardcoded?
  • Are you getting any benefit from hardcoding (same exception)?

If the answer is no anyway, and your application works ... well, that's important!


If you have time to spend with micro-optimizations like this, then it's probably better to spend your application code with a profiler looking at your DB requests, the number of HTTP requests you make to get static JS / CSS / images, updating PHP or changing your code so that it can run on PHP 5.3 (since PHP 5.3 comes with some optimizations over 5.2), ...

All of these are likely to give you a higher payoff; -)


Edit after comment:



Basically when the PHP file is loaded:

  • the file is read from disk
  • it is parsed and compiled into opcode
  • opcodes are executed

With opcode cache:

  • if there is room in RAM containing opcodes, they are loaded from RAM (this is, not reading the file, and also parsing / compiling)
    • If not, see the steps before - just add "save opcodes to RAM" before executing, for next query
  • and opcodes are executed

The parameter apc.stat

determines whether APC should check the date and time when the file was last modified in order to decide between using opcodes from RAM or recompiling the file if the last opcodes are in RAM.

Disabling this option means:

  • files are not checked on disk => faster and use fewer resources
    • For example, I've seen CPU usage drop from 10% to 15% when disabling this option on a fairly busy server.
  • but in the absence of a change check, if you want the changes to be taken into account, you need to clear the cache


That said, what I said is true: there are probably a lot of things you can optimize, which would mean more important gains than simply "should use hardcoded values" versus "should use constants / variables" ...

+6


source


this wat exactly happens .. without going into the details of the marker level ...

PHP is a scripting language that most people realize that it is not compiled. Although this is true in the traditional sense, in the sense that we don't call gcc or javac; instead, we compile every time the script is requested. In fact, the compilation lifecycles of PHP and Java are quite similar because they both are compiled into a set of intermediate instructions (opcodes or bytecodes) that are then run in a virtual machine (Zend VM or JVM).

enter image description here

The parsing and compilation phases are slow. When we add opcache, we short-circuit this process, keeping the result of the parsing and compilation phases, leaving only execution to run dynamically as always. Basically, we are now closer to the Java lifecycle; with the main differences being that we saved in shared memory instead of a file and can automatically recompile if changes happen to the script.



enter image description here

Use the opcode cache. This will give you more room to improve performance than any micro-optimization could possibly be. Also, when using Zend OpCache, there are many optimizations done for you (for example, switching $ i ++ to ++ $ i when the return value is not used).

Using opcode cache no longer has to be extra, it will allow you to get more performance from your hardware with minimal effort.

+2


source







All Articles