Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the implementation principle and performance of PHP function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand the implementation principle and performance of PHP function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Classification of php functions

In php, functions are divided into two categories: user function (built-in functions) and internal function (built-in functions). The former is some user-defined functions and methods in the program, while the latter is all kinds of library functions provided by php itself (such as sprintf, array_push, etc.). Users can also write library functions by extending them, which will be described later. For user function, it can be subdivided into function (function) and method (class method). These three functions will be analyzed and tested in this paper.

The realization of php function

How does a php function finally execute, and what is the process?

To answer this question, let's first 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 lexical parsing, syntax parsing and other stages, the source program is translated into instructions (opcodes), and then the ZEND virtual machine executes these instructions sequentially to complete the operation. Php itself is implemented in c, so in the end, all the functions of c are called. In fact, we can think of php as software developed by c. From the above description, it is not difficult to see that the execution of functions in php is also translated into opcodes to call, and each function call actually executes one or more instructions.

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

The copy code is as follows:

Typedef union _ zend_function {

Zend_uchar type; / * MUST be 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

Type indicates the type of function: user function, built-in function, overloaded function. Common contains the basic information of functions, including function names, parameter information, function flags (ordinary functions, static methods, abstract methods) and so on. In addition, for user functions, there is a function symbol table that records internal variables, which will be detailed later. Zend maintains a global function_table, which is a large hahs table. When the function is called, the corresponding zend_function is first found in the table based on the function name. When making a function call, the virtual machine calls the method according to the different decisions of the type, and the execution principle of different types of functions is different.

Built-in function

Built-in function, which is essentially a real c function, each built-in function, php in the final compilation will be expanded into a function called zif_xxxx, such as our common sprintf, corresponding to the bottom is zif_sprintf. When Zend executes, if it is found to be a built-in function, it simply does a forwarding operation.

Zend provides a series of api to call, including parameter acquisition, array manipulation, memory allocation, and so on. The parameter acquisition of the built-in function is realized by the zend_parse_parameters method. For the array, string and other parameters, zend implements a 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, with only one more forward call.

Built-in functions are dynamically loaded through so in php, and users can also write their own so according to their needs, which is what we often call extensions. ZEND provides a range of api for extension

User function

Compared with the built-in function, the user-defined function implemented through php has a completely different execution process and implementation principle. As mentioned earlier, we know that php code is translated into opcode to execute, and user functions are no exception. In fact, each function corresponds to a set of opcode, which is stored in zend_function. Therefore, the call to the user function ultimately corresponds to the execution of a set of opcodes.

Saving of "" local variables and realization of recursion

We know that function recursion is done through the stack. In php, a similar approach is used to implement it. 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 a stack, and whenever a function is called, a new symbol table is assigned and added to the stack. When the call ends, the current symbol table is unstacked. As a result, the preservation and recursion of the state are realized.

For stack maintenance, zend optimizes here. Pre-allocating a static array of length N to simulate the stack is often used in our own programs to simulate dynamic data structures through static arrays, which avoids memory allocation and destruction caused by each call. ZEND just 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 exceeds N, the program will not have stack overflow. In this case, zend will allocate and destroy the symbol table, which will lead to a lot of performance degradation. In zend, the current value of N is 32. Therefore, when we write php programs, the function call level had better not exceed 32. Of course, if it is a web application, it can itself be the depth of the function call hierarchy.

The parameter passing is different from the built-in function calling zend_parse_params to get the parameter, the parameter in the user function is obtained by instruction. A function has several parameters that correspond to several instructions. Specific to the implementation is the ordinary variable assignment. From the above analysis, we can see that, compared with the built-in function, because the stack table is maintained by ourselves, and the execution of each instruction is also a c function, the performance of the user function will be much worse, and there will be specific comparative analysis later. Therefore, if a function has a corresponding php built-in function implementation, try not to rewrite the function to implement it.

This is the end of the content of "how to understand the implementation principle and performance of PHP function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report