


Baidu engineers talk about the implementation principles and performance analysis of PHP functions (1)
Baidu engineers talk about the implementation principles and performance analysis of PHP functions (1)
Preface Reference source:
In any language, functions are the most basic building blocks. What are the characteristics of PHP functions? How is function calling implemented? How is the performance of PHP functions? Any suggestions for usage? This article will analyze based on the principles and combine it with actual performance testing to try to answer these questions, and better write PHP programs while understanding the implementation. At the same time, some common PHP functions will be introduced.
Classification of PHP functions
In PHP, if divided horizontally, functions are divided into two categories: user function (built-in function) and internal function (built-in function). The former are some functions and methods customized by users in the program, and the latter are various library functions provided by PHP itself (such as sprintf, array_push, etc.). Users can also write library functions through extension methods, which will be introduced later. For user function, it can be subdivided into function (function) and method (class method). In this article, these three functions will be analyzed and tested respectively.
Implementation of php function
How is a php function ultimately executed? What is the process like?
To answer this question, let’s first take a look at the process of executing the php code.
As you can see from Figure 1, PHP implements a typical dynamic language execution process: after getting a piece of code, after going through stages such as lexical analysis and syntax analysis, the source program will be translated into instructions (opcodes). The ZEND virtual machine then executes these instructions in sequence to complete the operation. Php itself is implemented in C, so the functions ultimately called are all C functions. In fact, we can regard PHP as a software developed in 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 instructions.
For each function, zend is described by the following data structure
where type indicates the type of function: user function, built-in function, overloaded function. Common contains the basic information of the function, including function name, parameter information, function flags (ordinary functions, static methods, abstract methods), etc. In addition, for user functions, there is also a function symbol table that records internal variables, etc., which will be described in detail later. Zend maintains a global function_table, which is a large hash table. When a function is called, the corresponding zend_function will first be found from the table based on the function name. When making a function call, the virtual machine determines the calling method based on the type. Different types of functions have different execution principles.
Built-in functions
Built-in functions are essentially real C functions. For each built-in function, PHP will expand into a function named zif_xxxx after final compilation. For example, our common sprintf corresponds to zif_sprintf at the bottom layer. When Zend is executing, if it finds a built-in function, it simply performs a forwarding operation.
Zend provides a series of APIs for calling, including parameter acquisition, array operations, memory allocation, etc. The parameters of the built-in function are obtained through the zend_parse_parameters method. For parameters such as arrays and strings, zend implements shallow copying, so this efficiency is very high. It can be said that for PHP built-in functions, their efficiency is almost the same as that of the corresponding C functions, with the only additional forwarding call.
Built-in functions are dynamically loaded in PHP through so. Users can also write corresponding so according to their needs, which is what we often call extensions. ZEND provides a series of APIs for extension use
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 PHP code is translated into opcodes for execution, and user functions are no exception. In fact, each function corresponds to a set of opcodes, and this set of instructions is saved in zend_function. Therefore, the call of the user function ultimately corresponds to the execution of a set of opcodes.
》》Save of local variables and implementation of recursion
We know that function recursion is completed through the stack. In php, a similar method is used to achieve this. 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. Whenever a function is called, a new symbol table is allocated and pushed onto the stack. When the call ends, the current symbol table is popped off the stack. This achieves state preservation and recursion.
For stack maintenance, zend has made optimizations here. Pre-allocate a static array of length N to simulate the stack. This method of simulating dynamic data structures through static arrays is often used in our own programs. This method avoids the memory allocation caused by each call. destroy. ZEND just cleans the symbol table data on the top of the current stack at the end of the function call. Because the length of the static array is N, once the function call level exceeds N, the program will not cause stack overflow. In this case, zend will allocate and destroy the symbol table, which will cause a lot of performance degradation. In zend, the current value of N is 32.Therefore, when we write PHP programs, it is best not to exceed 32 function call levels. Of course, if it is a web application, the function call level itself can be deep.
》》Transfer of parameters is different from the built-in function calling zend_parse_params to obtain parameters. The acquisition of parameters in user functions is completed through instructions. The number of parameters a function has corresponds to the number of instructions. Specific to implementation, it is ordinary variable assignment. From the above analysis, it can be seen that compared with the built-in functions, since the stack table is maintained by itself, and the execution of each instruction is also a C function, the performance of the user function will be relatively much worse. There will be a specific comparative analysis later. Therefore, if a function has a corresponding PHP built-in function, try not to rewrite the function yourself to implement it.
Baidu engineers talk about the implementation principles and performance analysis of PHP functions (1)
http://www.lai18.com/content/410091.html
The above introduces the implementation principles and performance analysis of PHP functions explained by Baidu engineers (1), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.