Learn more about PHP opcode caching principles

Source: Internet
Author: User
Tags apc php script zend

What is opcode

OpCode (operate code) is a part of a computer directive that specifies the action to be performed, and the format and specification of the instruction is specified by the specified specification of the processor

OpCode is a PHP script-compiled intermediate language, like Java's bytecode, or. NET of MSL

Why use the opcode cache

The purpose of the opcode cache is to avoid duplication of compilation and reduce CPU and memory overhead. If the performance bottleneck of dynamic content is not the CPU and the content, but the IO operation, such as the IO cost of database query, the performance improvement of opcode cache is very limited at this time. Anyway, since the opcode cache can reduce CPU and memory overhead, this is certainly a good thing.

The common opcode Cahce module in PHP is as follows

    1. Apc

    2. optimizer+ (currently open source and integrated with php5.5+ Opcache)

    3. XCache

    4. Eaccelerator

OpCode principle

For example, there is the following code

<?phpecho ' Hello world '; $a = 1 + 1;echo $a;? >


PHP executes this code in the following 4 steps (to be exact, via PHP's language engine Zend)

Scanning (lexing) translates the PHP code into a language fragment (Tokens) parsing, transforms Tokens into a simple and meaningful expression complilation, compiles an expression into opcodeexecution, sequential execution of opcode, one at a time, to implement PHP script functions


Such as

Lexing Stage

Lex is the basis table for lexical analysis. The Zend engine will parse the input PHP code (literally: ZEND/ZEND_LANGUAGE_SCANNER.C will be based on the ZEND/ZEND_LANGUAGE_SCANNER.L (lex file)) to get a single word, PHP provides a function: Token_get_all can parse a piece of PHP code into tokens

If you use this function to analyze the sample code above, the result is as follows:

Array (    [0] => Array         (             [0] => 374             [1] => <?php             [2] => 1         )     [1] => Array         (            [0] => 377             [1] =>             [2] => 1        )     [2] => Array         (             [0] => 317             [1] => echo             [2] => 1        )     [3] = > Array         (             [0] => 377          &NBSP;&NBSP;&NBSP;[1]&NBSP;=&GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[2]  => 1        )     [4] =>  Array         (             [0] => 316             [1] =>  "Hello world "            [2] => 1         )     [5] => ;     [6] => Array         (             [0] => 310             [1] =>  $a              [2] => 1        )      [7] => Array         (             [0] => 377             [1] =>             [2] => 1        )     [8] => =     [9] => Array         (             [0] => 377             [1] =>             [2] => 1        )      [10] => Array         (             [0] => 306             [1] => 1             [2] => 1        )      [11] => array         (           &NBSP;&NBSP;[0]&NBSP;=&GT;&NBSP;377&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[1]  =>            [2] => 1         )     [12] => +     [13] => Array         (             [0] => 377             [1] =>             [2] => 1        )     [14]  => Array         (           &nBSP;&NBSP;[0]&NBSP;=&GT;&NBSP;306&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[1]  => 1            [2] =>  1        )     [15] => ;     [16] => Array         (             [0] => 377             [1] =>             [2] => 1        )      [17] => Array         (             [0] => 317            &nbsP [1] => echo            [2] =>  1        )     [18] => array          (           &NBSP;&NBSP;[0]&NBSP;=&GT;&NBSP;377&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[1]  =>            [2] => 1         )     [19] => Array          (            [ 0] => 310            [1] =>   $a             [2] => 1         )     [20] => ;    [21] =>  Array         (             [0] => 376             [1] => ?>            [ 2] => 1        ))


Analysis of the return result we can find that the source of the string, characters, spaces will be returned as is. The characters for each source code appear in the appropriate order. In other words, such as tags, operators, statements are converted into a containing, two-part Array:token ID (that is, within the Zend of the Token's corresponding code, such as T_echo,t_string) and the original content of the source code

Parsing stage

The parsing phase first discards the extra spaces in the tokens array and then converts the remaining tokens to a simple expression

echo a contanst stringadd numbers togetherstore The result of the prior expression to a variableecho a variable
Complilation Stage

The complilation stage compiles the tokens into Op_array, each of which contains the following 5 parts

The opcode number indicates the type of operation for each op_array, such as the result of add,echo results stored opcode operand 1 to opcode operand operand 2 extended value 1 shaping to distinguish the overloaded operator


For example, my PHP code will be parsing into:

Zend_echo ' Hello World ' zend_add ~0 1 1zend_assign!0 ~0zend_echi ~0

In the above code we didn't see $a, where did we go?


This will introduce the operands, each of which consists of the following two parts:

    1. Op_type: for is_const,is_tmp_var,is_var,is_unuesed or IS_CV

    2. U a Union, which holds the value (const) or Lvalue (Var) of the operand in different types depending on the Op_type


And for Var, every Var is different.

Is_tmp_var as the name implies is that this is a temporary variable, save some Op_array results, so that the next op_array to use, this operand u holds a pointer to the variable table of a handle (integer), this operand is generally used to start, such as ~0 expression Unknown temporary variable for number No. 0 in the variable table


Is_var This is our general sense of the variable, they begin with $ to represent


IS_CV represents a cache mechanism used by the compiler after ze2.1/php5.1, which holds the applied variable address, and when a variable is applied for the first time, it will be CV-the reference to this variable will not need to find the active symbol table again. CV variable already! The beginning indicates


So, my $ A is optimized! 0.


Opcode Cache principle

Through the above introduction, we understand opcode, about opcode cache schematic diagram is as follows


We can see that there is one more stage in addition to the lexing,parsing,complilation,execution phase: detecting If a file is updated

If there is no update to get the cached opcode directly, go directly to the execution stage and return the result

If updated, follow the original process (add a link: The red part of the figure cache opcode)


The original address: in-depth understanding of PHP opcode Caching principle
Tags: php opcache opcode APC

Smart recommendations
    • PHP Only variables can is passed by reference
    • PAC Automatic Proxy
    • PHP failed to create Git branch via exec
    • PHPSTORM9 can not enter the Chinese comma period and other symbols, how to break?
    • Phpexcel reading data

Learn more about PHP opcode caching principles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.