LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > Linux教程 >

In depth understanding of PHP Opcode cache principle

时间:2016-11-10 21:12来源:未知 编辑:linuxeye 点击:
What is the opcode cache? When the interpreter to complete the analysis of script code, will they generate intermediate code can be run directly, also known as the operation code (Operate Code, opcode). Opcode cache aims to avoid duplicatio

What is the opcode cache?
When the interpreter to complete the analysis of script code, will they generate intermediate code can be run directly, also known as the operation code (Operate Code, opcode). Opcode cache aims to avoid duplication of compilation, reduce the cost of CPU and memory. If the performance bottleneck of dynamic content is not the CPU and memory, but in I/O operation, such as database query the overhead of disk I/O, then the performance of opcode cache's ascension is very limited. But since the reduction of opcode cache can bring the cost of CPU and memory, which is always good.

Modern operating code register (Optimizer+, APC2.0+, others) the use of shared memory to store, and can be directly executable from it, instead of before execution to deserialize the &rdquo code; &ldquo. This will bring significant performance acceleration, usually reduces the overall memory consumption of server, and few defects.

Why use Opcode cache?
it started from the PHP code of the life cycle, the request PHP script, through five steps, as shown below:

The Zend engine has to read a file from the file system, scanning the dictionary and expression, parse the file, create to perform computer code (called Opcode), the implementation of Opcode. Every time the request PHP scripts are executed over the steps above, if the PHP source code does not change, then the Opcode will not change, obviously there is no need to each re generation of Opcode, combined with the omnipresent caching mechanism in Web, we can put the Opcode cache down, to directly access the cached Opcode isn't faster, process Fig. after enabling Opcode cache is shown as follows:

The PHP opcode cache plug-in?
Optimizer+ (Optimizer+ renamed Opcache, in 2013 PHP in mid March 5.5 integrated Opcache, the other will not disappear? ),eAccelerator,xcache,APC …

PHP opcode principle:
Opcode is an intermediate language PHP scripts are compiled, like Java ByteCode, or.NET MSL, for example, such as you write PHP code:

<?php
   echo "Hello World";
   $a =1+1;
   echo $a;?>

PHP this code is executed by the following 4 steps (should be PHP language engine Zend specifically,)

1.Scanning(Lexing),WillPHPCode conversion of language fragments(Tokens)2.Parsing,WillTokensExpression into a simple but meaningful3.Compilation,To compile the expression intoOpocdes4.Execution,Sequential executionOpcodes, Each one, in order to achievePHPThe script functions. 

Digression: now some Cache such as APC, allows the PHP to cache Opcodes, in this way, each time a request arrives, you do not need to repeat the previous 3 steps, which can greatly improve the execution speed of PHP.

What is the Lexing? To learn the students of compiler theory should be the principle of compiler lexical analysis steps to understand somewhat, table based on Lex is a lexical analysis. Zend/zend_language_scanner.c based on Zend/zend_language_scanner.l (Lex file), to enter the PHP code for lexical analysis, so as to get a “ ”, PHP4.2 provides a function called token_get_all, this function can speak a PHP code Scanning into Tokens,

If we mentioned at the beginning of the PHP code using this function, the following will result in:

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]=>;)

Analysis of this result we can find, string, source of the character, the space, will return as is. Each source code of the character, will appear in the appropriate order. However, others such as label, operator, statement, will be converted to a Array: Token ID contains two parts (i.e. within the Zend codes, such as T_ECHO, Token, T_STRING), and the source of the original content.
Next, Parsing stage, Parsing first discards the Tokens Array more than the blank, and then the rest of the Tokens into a simple expression a

1.echo a constant string
2.add two numbers together
3.store the result of the prior expression to a variable
4.echo a variable

Then change to Compilation stage, it will compile Tokens into a op_array, each op_arrayd contains 5 parts as follows:

1.OpcodeDigital identification, points out eachop_arrayThe type of operation, for exampleadd , echo
2.ResultDepositOpcodeResult3.Operand1GiveOpcodeOperand4.Operand25.Extension value1Plastic is used to distinguish over loaded operators

For example, our PHP code will be Parsing:

* ZEND_ECHO 'Hello World'* ZEND_ADD ~011* ZEND_ASSIGN !0~0* ZEND_ECHO !0

You may ask, we $a go there?

This will introduce the operands, each operand is composed of the following two parts:

a)op_type :For theIS_CONST, IS_TMP_VAR, IS_VAR, IS_UNUSED, or IS_CV 
b)u,A joint body, according to theop_typeDifferent, using different types of saved the operand value(const)Or an lvalue(var)

For VaR, each VaR is not the same

IS_TMP_VAR, As the name suggests, this is a temporary variable, save some op_array results, so that the next op_array use, the operand u holds a pointer to the variable table a handle (integer), the operand is generally used. At the beginning, such as ~0, said temporary variables table 0 unknown

IS_VAR this is our general sense of variables, they begin with a $

IS_CV said a cache ZE2.1/PHP5.1 compiler uses, this variable holds the address of the variable is that it refers to, when a variable is first referenced, CV will be up, to reference the variables do not need to find the active symbol table again, CV variables to! At the beginning of that.

------分隔线----------------------------
标签:Opcode cache
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
栏目列表
推荐内容