Baidu engineers explain the implementation principle and performance analysis of PHP functions (I)

Source: Internet
Author: User
Tags types of functions
: This article mainly introduces Baidu engineers to introduce the implementation principle and performance analysis of PHP functions (1). For more information about PHP tutorials, see.

Baidu engineers explain the implementation principle and performance analysis of PHP functions (I)

Preface
In any language, a function is the most basic component unit. What are the features of php functions? How is function calling implemented? What is the performance of php functions? what are the suggestions for use? This article will analyze the principle and try to answer these questions based on the actual performance test, so as to better compile the php program while understanding the implementation. Some common php functions will also be introduced.
Php Function Classification
In php, functions are divided horizontally into two categories: user function and internal function ). The former is some user-defined functions and methods in the program, and the latter is various library functions provided by php (such as sprintf and array_push ). You can also compile the library functions through the extension method. this will be described later. User functions can be subdivided into functions and methods. these functions are analyzed and tested in this article.
Php function implementation
How is a php function executed? what is the process like?
To answer this question, let's take a look at the process of executing the php code.

As you can see, php implements a typical dynamic language execution process: after a piece of code is obtained, after the lexical parsing, syntax parsing, and other stages, the source program will be translated into commands (opcodes), and then the ZEND virtual machine will execute these commands sequentially to complete the operation. Php itself is implemented using c, so all the functions that are finally called are c functions. In fact, we can regard php as a software developed by c. It is not difficult to see from the above description that the execution of functions in php is also translated into opcodes for calling. each function call actually executes one or more commands.
Zend describes each function based on the following data structure:
Typedef union _ zend_function {zend_uchar type;/* MUST be the first element of this struct! */Struct {zend_uchar type;/* never used */char * function_name; using * scope; using fn_flags; union _ zend_function * prototype; zend_uint num_args; zend_uint vertex; using * arg_info; zend_bool functions; unsigned char return_reference;} common; incluop_array; incluinternal_function;} zend_function; typedef struct _ functions {HashTable * functions; zend_function * function; void * reserved [functions];} zend_function_state;
The type indicates the function type: User function, built-in function, and overload function. Common contains basic information about a function, including the function name, parameter information, and function flag (Common function, static method, and abstract method. In addition, for user functions, there is also a function symbol table that records internal variables and so on. this will be detailed later. Zend maintains a global function_table, which is a large hahs table. When a function is called, the corresponding zend_function is first found from the table based on the function name. When a function is called, the virtual opportunity determines the call method based on the type. The Execution Principle of different types of functions is different.
Built-in functions
Built-in functions are essentially true c functions. every built-in function, php, after final compilation, will be expanded into a function named zif_xxxx, such as our common sprintf, zif_sprintf corresponds to the underlying layer. When Zend is executed, if it finds that it is a built-in function, it simply performs a forwarding operation.
Zend provides a series of APIs for calling, including parameter acquisition, array operations, and memory allocation. The parameter acquisition of built-in functions is implemented through the zend_parse_parameters method. for parameters such as arrays and strings, zend implements shallow copy, so this efficiency is very high. It can be said that for php built-in functions, the efficiency is almost the same as the corresponding c function, the only one more forwarding call.
Built-in functions are dynamically loaded using the so method in php. you can also write the corresponding so as needed, which is also known as the extension. ZEND provides a series of APIs for extension
User functions
Compared with built-in functions, user-defined functions implemented through php have completely different execution processes and implementation principles. As mentioned above, we know that the php code is translated into an opcode for execution, and the user function is no exception. In reality, each function corresponds to a set of opcode, these commands are stored in zend_function. Therefore, the call to a user function corresponds to the execution of a set of opcodes.
"Save local variables and implement recursion
We know that function recursion is done through stacks. Php also uses a similar method. Zend assigns an active symbol table (active_sym_table) to each php function to record the status of all local variables in the current function. All symbol tables are maintained in the form of stacks. each time a function is called, a new symbol table is allocated to the stack. When the call ends, the current symbol table goes out of the stack. The state is saved and recursive.
Zend has been optimized for stack maintenance. A static array with a length of N is pre-allocated to simulate the stack. this static array is often used in our own programs to simulate dynamic data structures, this method avoids memory allocation and destruction caused by each call. ZEND only cleans 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 will not experience Stack Overflow. in this case, zend will allocate and destroy the symbol table, as a result, performance will be greatly reduced. In zend, N is currently set to 32. Therefore, when writing a php program, it is recommended that the function call level be no more than 32. Of course, for web applications, the depth of the function call hierarchy can be defined.
"Parameter transfer is different from that of the built-in function that calls zend_parse_params to obtain parameters. parameters in user functions are obtained through commands. A function has several parameters corresponding to several commands. The specific implementation is the assignment of common variables. From the above analysis, we can see that, compared with the built-in functions, because the stack table is maintained by itself, and the execution of each instruction is also a c function, the performance of user functions is relatively poor, A detailed comparative analysis will be provided later. Therefore, if a function has a built-in php function implementation, try not to rewrite the function.

Reference Source:
Baidu engineers explain the implementation principle and performance analysis of PHP functions (I)
Http://www.lai18.com/content/410091.html

The above section describes the implementation principle and performance analysis of PHP functions by Baidu engineers (1). It includes some content and hopes to help those who are interested in PHP tutorials.

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.