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

Source: Internet
Author: User
Tags array length data structures php code shallow copy sprintf types of functions zend

This article mainly introduced Baidu engineers talk about the implementation of PHP functions and performance Analysis (a), the need for friends can refer to the

Objective

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

The classification of PHP functions

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

Implementation of PHP functions

How does a PHP function end up being executed, and what is the process?

To answer this question, let's look at the process through which the PHP code is executed.

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

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

Copy code code as follows:

typedef Union _ZEND_FUNCTION {

Zend_uchar type; /* Must be the ' the ' 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 types of functions: User functions, built-in functions, overloaded functions. 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 also a function symbol table, recording the internal variables, etc., this will be described in detail later. Zend maintains a global function_table, which is a large hahs table. The function call will first find the corresponding zend_function from the table according to the function name. When a function call is made, the virtual opportunity decides to invoke the method according to the different type, and the different types of functions, the execution principle is not the same.

Built-in functions

Built-in function, which is essentially the real C function, each built-in function, PHP will be launched after the final compilation of a function called zif_xxxx, such as our common sprintf, corresponding to the bottom is zif_sprintf. Zend in execution, if found to be a built-in function, then simply do 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 functions are obtained by the Zend_parse_parameters method, and for the array, string and other parameters, the Zend realizes the shallow copy, so the 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 in PHP are all through the way of dynamic loading, users can also write their own according to the corresponding so, that is, we often say the 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 above, we know that PHP code is translated into a opcode to execute, user functions are no exception, in fact, each function corresponds to a group of opcode, this set of instructions are saved in the zend_function. Thus, the invocation of a user function ultimately corresponds to the execution of a set of opcodes.

The preservation of local variables and the realization of recursion

We know that function recursion is done through the stack. In PHP, this is accomplished using a similar approach. Zend assigns an active symbol table (active_sym_table) to each PHP function to record 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 is called, a new symbol table is allocated to the stack. When the call ends, the current symbol table is out of the stack. This realizes the preservation and recursion of the state.

For the maintenance of the stack, Zend is optimized here. Allocating a static array of length n to simulate the stack, which simulates dynamic data structures through static arrays, is often used in our own programs, avoiding the memory allocation and destruction that comes with each invocation. Zend simply clean 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 more than N, the program does not appear stack overflow, in this case Zend will be the symbol table allocation, destruction, resulting in a lot of performance degradation. In Zend, N is currently 32. Therefore, when we write PHP programs, the function call level is best not more than 32. Of course, if it is a Web application, it can function to call the depth of the hierarchy.

The parameters of the transfer and the built-in function call Zend_parse_params to get different parameters, the user function in the parameters of the acquisition is done through instructions. A function has several parameters that correspond to several instructions. Specific to implementation is the normal variable assignment. Through the above analysis can be seen, compared with built-in functions, because it is the maintenance of 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 comparison analysis. Therefore, if a function has the corresponding PHP built-in function implementation, as far as possible not to write their own function to achieve.

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.