Baidu Engineer Talk about the implementation principle and performance analysis of PHP function (i) _php tutorial

Source: Internet
Author: User
Tags types of functions

Baidu engineers talk about the implementation principle and performance analysis of PHP function (i.)


This article mainly introduced the Baidu engineer to say PHP function implementation principle and performance analysis (a), the need for friends can refer to the following

Objective

In any language, a function is the most basic constituent unit. What are the features of the PHP function? How does a function call work? What is the performance of the PHP function, and what are the suggestions for using it? This paper will analyze the actual performance test from the principle and try to answer these questions, and better write the PHP program while understanding the implementation. Some common PHP functions are also introduced.

Classification of PHP functions

In PHP, the functions are divided into two main categories: User function (built-in function) and internal function (built-in functions). The former is the user in the program to customize some of the functions and methods, the latter is the PHP itself provides a variety of library functions (such as sprintf, Array_push, etc.). The user can also write library functions by extending the method, which is described later. For the user function, which can be subdivided into functions (function) and method (class method), the three kinds of functions are analyzed and tested separately in this article.

Implementation of PHP functions

How does a PHP function ultimately execute, and what is the process like?

To answer this question, let's take a look at the process through which the PHP code executes.

As can be seen from Figure 1, PHP implements a typical dynamic language execution process: After getting a piece of code, after the lexical parsing, parsing and other stages, the source program will be translated into a single instruction (opcodes), and then zend the virtual machine to execute the instructions in sequence to complete the operation. PHP itself is implemented in C, so the final call is also a C function, in fact, we can think of PHP as a C developed software. It is easy to see from the above that the execution of functions in PHP is also translated into opcodes to invoke, and each function call actually executes one or more instructions.

For each function, Zend is described by the following data structure

Copy the code code as follows:

typedef Union _ZEND_FUNCTION {

Zend_uchar type; /* must is the first element of this struct! */

struct {

Zend_uchar type; /* never used */

Char *function_name;

Zend_class_entry *scope;

Zend_uint Fn_flags;

Union _zend_function *prototype;

Zend_uint Num_args;

Zend_uint Required_num_args;

Zend_arg_info *arg_info;

Zend_bool pass_rest_by_reference;

unsigned char return_reference;

} Common;

Zend_op_array Op_array;

Zend_internal_function internal_function;

} zend_function;

typedef struct _ZEND_FUNCTION_STATE {

HashTable *function_symbol_table;

Zend_function *function;

void *reserved[zend_max_reserved_resources];

} zend_function_state;

Where type identifies the type of function: User function, built-in function, overloaded function. Common contains basic information about functions, including function names, parameter information, function flags (common functions, static methods, abstract methods), and so on. In addition, for the user function, there is a function symbol table, the internal variables are recorded, and this will be described in detail later. Zend maintains a global function_table, which is a large hahs table. A function call will first find the corresponding zend_function from the table based on the function name. When a function call is made, the virtual opportunity calls the method according to the different type, and the different types of functions are executed differently.

Built-in functions

Built-in function, which is essentially a real C function, each built-in function, PHP after the final compilation will be expanded into a function called zif_xxxx, such as our common sprintf, corresponding to the bottom is zif_sprintf. Zend when executing, if found to be built-in functions, it is simply a forwarding operation.

Zend provides a series of APIs for invocation, including parameter fetching, array manipulation, memory allocation, and so on. The parameters of the built-in function are obtained through the Zend_parse_parameters method, and for the parameters such as arrays, strings, and so on, the Zend realizes a shallow copy, so this efficiency is very high. It can be said that for PHP built-in functions, its efficiency and the corresponding C function is almost the same, the only one more forwarding call.

Built-in functions are dynamically loaded in PHP through so, and users can write their own so, which is what we often say, as an extension. Zend provides a range of APIs for extended use

User functions

Compared with built-in functions, user-defined functions implemented by PHP have completely different execution and implementation principles. As mentioned earlier, we know that PHP code is translated into a opcode to execute, the user function is no exception, in fact, each function corresponds to a set of opcode, this set of instructions are saved in zend_function. Thus, the invocation of the user function is ultimately the execution of the corresponding set of opcodes.

On the preservation of local variables and the realization of recursion

We know that function recursion is done through the stack. In PHP, a similar approach is used. Zend assigns an active symbol table (active_sym_table) to each PHP function, recording the state of all local variables in the current function. All symbol tables are maintained in the form of stacks, and each time a function call is assigned, a new symbol table is allocated to the stack. When the call ends, the current symbol table is out of the stack. This enables the preservation and recursion of the state.

For stack maintenance, Zend is optimized here. A static array of length n is pre-allocated to simulate the stack, and this method of simulating dynamic data structures by static arrays is often used in our own programs, which avoids the memory allocation and destruction of each invocation. Zend simply clean off the symbol table data at the top of the current stack at the end of the function call. Because the static array length is n, once the function call level exceeds n, the program does not appear stack overflow, in this case Zend will be the symbol table allocation, destruction, which will result in a lot of performance degradation. In Zend, the current value of N is 32. Therefore, when we write PHP programs, the function call hierarchy is best not more than 32. Of course, if it is a Web application, it can call the depth of the hierarchy itself.

"parameter is passed with the built-in function call Zend_parse_params to get the parameters, and the parameters in the user function are obtained by instruction. A function has several parameters that correspond to several instructions. Specific to the implementation is the normal variable assignment. The above analysis shows that, compared with the built-in function, because it is to maintain the stack table, and the execution of each instruction is a C function, the performance of the user function is relatively poor, there will be a specific comparative analysis. Therefore, if a function has a corresponding PHP built-in function implementation, try not to re-write the function to implement.

http://www.bkjia.com/PHPjc/1000084.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000084.html techarticle Baidu Engineers talk about the implementation principle and performance analysis of PHP function (i) This article mainly introduced the Baidu engineer to say PHP function implementation principle and performance analysis (a), the need for friends can refer to ...

  • 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.