What is a opcode cache?
When the interpreter finishes parsing the script code, it generates intermediate code that can be run directly, also known as the opcode (Operate code,opcode). The goal of the Opcode cache is to avoid duplication of compilation and reduce CPU and memory overhead. If the performance bottleneck for dynamic content is not CPU and memory, but I/O operations, such as disk I/O overhead from database queries, the performance gains of the opcode cache are very limited. But since the opcode cache can bring down CPU and memory overhead, this is always a good thing.
Modern opcode buffers (optimizer+,apc2.0+, others) are stored using shared memory and can be executed directly from the file without the "deserialization" code before execution. This leads to significant performance acceleration, which typically reduces the overall server's memory consumption and has few drawbacks.
Why use the opcode cache?
This has to be said from the life cycle of the PHP code, which takes five steps when requesting a PHP script, as shown in:
The Zend engine must read files from the file system, scan its dictionaries and expressions, parse files, create computer code to execute (called opcode), and finally execute opcode. Each time the PHP script will execute the above steps, if the PHP source code does not change, then opcode will not change, obviously there is no need to re-generate opcode every time, combined with the ubiquitous caching mechanism in the web, we can cache opcode, It is not faster to access the cached opcode directly later, and the flowchart after enabling opcode caching is as follows:
are there those php opcode cache plugins?
Optimizer+ (optimizer+ in mid-March 2013 opcache,php 5.5 episode Opcache, the others will not disappear? ), Eaccelerator, XCache, APC ...
PHP opcode principle:
OpCode is a PHP script-compiled intermediate language, like Java's bytecode, or. NET MSL, for example, you write down the following PHP code:
<? PHP "Hello World"; =1+1; echo $a;? >
PHP executes this code in the following 4 steps (to be exact, it should be PHP's language engine Zend)
1.scanning (lexing) php code converted to a language fragment ( tokens) 2.parsing,< Span class= "PLN" > tokens Convert to simple and meaningful expression < Span class= "lit" >3.compilation, compile expression into 4.execution, Executes the opcodesphp
Aside: Now some caches such as APC, can make PHP cache opcodes, so that every time there is a request, there is no need to repeat the previous 3 steps, so that can greatly improve the speed of PHP execution.
Then what is lexing? The students who have learned the principles of compiling should have some knowledge of the lexical analysis steps in the compiling principle, Lex is a basis table for lexical analysis. ZEND/ZEND_LANGUAGE_SCANNER.C will perform lexical analysis based on the PHP code entered by the ZEND/ZEND_LANGUAGE_SCANNER.L (lex file) to get a "word", PHP4.2 began to provide a function called Token_get_all, this function can be said a section of PHP code scanning into tokens;
If we use this function to process the PHP code we mentioned at the beginning, we will get the following result:
Array( [0] = Array ( [0] = 367 [1] = Array ( [0] = 316 [1] =Echo) [2] = Array ( [0] = 370 [1] = ) [3] = Array ( [0] = 315 [1] = "Hello World" ) [4] = ; [5] = Array ( [0] = 370 [1] = ) [6] = = [7] = Array ( [0] = 370 [1] = ) [8] = Array ( [0] = 305 [1] = 1 ) [9] = Array ( [0] = 370 [1] = ) [10] = + [11] = Array ( [0] = 370 [1] = ) [12] = Array ( [0] = 305 [1] = 1 ) [13] = ; [14] = Array ( [0] = 370 [1] = ) [15] = Array ( [0] = 316 [1] =Echo [16] => array ( [0] => 370 [1 ] => ) [17] => ; /span>
Analysis of the return result we can find that the source of strings, characters, spaces, will be returned as is. Each character in the source code appears in the appropriate order. And, other such as tags, operators, statements, will be converted to a two-part Array:token ID (that is, in the Zend internal change Token of the corresponding code, such as, t_echo,t_string), and the source of the original content.
Next, is the parsing stage, parsing first discards more spaces in the tokens array, and then converts the remaining tokens to a simple expression of one
1.echo A constant string2.add numbers together 3.store The result of the prior expression to a vari Able4.echo a variable
Then change the compilation stage, it will tokens compiled into a op_array, each op_arrayd contains the following 5 parts:
The identification of the 1.Opcode number indicates the type of operation for each op_array, for example , echo2. Results stored Opcode Results 3. Operand 1 to the operand of Opcode 4. Number of Operations 25. Extended value 1 shaping to differentiate the overloaded operator
For example, our PHP code will be parsing into:
*' Hello world '~011*! 0~0*! 0
You might ask, where is our $ A?
This introduces the operands, each of which consists of the following two parts:
A): for is_const, is_tmp_var, is_var, is_unused, or IS_CV b) u, a consortium, holds the value (const) or Lvalue (var) of this operand in different types, depending on the op_type.
And for Var, every Var is different.
Is_tmp_var, as the name implies, this is a temporary variable, save some Op_array results, so that the next op_array to use, this operand of u holds a pointer to the variable table of a handle (integer), this operand is generally used to start, such as ~0, A temporary variable that represents the unknown number No. 0 of the variable table
Is_var This is our general sense of the variable, they start with a $ expression
IS_CV says ZE2.1/ PHP5.1 later compiler uses a cache mechanism, this variable holds the address of the variable referenced by it, when a variable is referenced for the first time, it will be CV up, the reference to this variable will not need to find the active symbol table again, CV variable to! The beginning indicates.
So it seems that our $ A is optimized to 0.
Reference: http://www.laruence.com/2008/06/18/221.html
Deep understanding of PHP opcode caching principles (RPM)