The performance of PHP is analyzed from the following aspects: memory management, variables, functions, operating mechanism and network model.

1. Memory management

Similar to Nginx’s approach to memory management, PHP is internally based on memory pools and introduces the concept of a memory pool’s life cycle. On the memory pool side, PHP hosts all memory-related operations for PHP scripts and extensions. The management of large memory and small memory adopts different implementation methods and optimizations. During the life cycle of memory allocation and reclamation, PHP uses the mechanism of one initial request + dynamic capacity expansion + memory identity reclamation, and remasks the memory pool directly after each request.

2. The variable

PHP is known to be a weak variable type language, so inside PHP, all PHP variables correspond to a single type, Zval, which is defined as follows:

In terms of variables, PHP does a lot of optimization work, such as Reference counting and copy on Writer mechanism. This ensures optimized memory usage and reduces the number of memory copies. In terms of arrays, PHP uses an efficient internal Hashtable implementation.

3. The function

Inside PHP, all PHP functions are converted to a function pointer inside. For example, an extension function

ZEND_FUNCTION ( my_function ); Function my_function(){}

It’s going to be a function when you expand it inside

void zif_my_function ( INTERNAL_FUNCTION_PARAMETERS ); 

void zif_my_function( 

int ht, 

zval * return_value, 

zval * this_ptr, 

int return_value_used, 

zend_executor_globals * executor_globals

 ); 

From this point of view, PHP functions also correspond internally to a function pointer.

4. Operation mechanism

When talking about PHP performance, many people say “C/C++ is compiled, JAVA is semi-compiled, PHP is interpreted”. This means that PHP is dynamically parsed before the code is run, so from this point of view, PHP performance is necessarily poor.

Indeed, output from a PHP script run is really a process of dynamic parsing and then code running.

The PHP runtime is also divided into three phases:

When the Parse. Grammar analysis stage. When the Compile. Compiling produces opCode intermediate code. When the Execute. Run, run dynamically for output.

5. Dynamic operation

From the above analysis, PHP does a lot of work in memory management, variables, functions, execution mechanisms, etc., so as a matter of principle, PHP should not have performance problems, or at least should be close to JAVA performance.

But why do many people still feel PHP is slow? Especially in the performance comparison of some computations, we always find that the performance of PHP processing is relatively low. This brings us to the performance issues brought about by the dynamic nature of the PHP language. Since PHP is a dynamic runtime, all variables, functions, object calls, scope implementations, and so on are determined at runtime. This fundamentally determines some of the things that are hard to change in PHP performance: variables and functions that can be determined statically in C/C++ need to be determined dynamically in PHP, which means that PHP intermediate code cannot be run directly and needs to be run on Zend Engine.

6. Network model

Currently using PHP, the ideal and general mode is to use FastCGI (PHP-FPM). Php-fpm is similar to Nginx in network model, adopting multi-process Master+ multi-worker mode. Php-fpm itself is based on the epoll model in Libevent. From the point of view of network model, there is no performance difference between this method and other network models.

7. Conclusion

From the above analysis, PHP itself does not have significant performance differences in basic memory management, variables, functions, running mechanism and network model. However, due to the dynamic running nature of PHP, IT determines the performance of PHP compared to other compiled languages. All variable lookups, function runs, and so on add CPU overhead and extra memory overhead to hash lookups. The exact amount of this overhead can be determined by subsequent baseline performance and comparative analysis.

Therefore, you can also see in general some scenarios where PHP is not well suited for: heavy computational tasks, large data operations, and applications where memory requirements are very strict. If you want to implement these functions, it is recommended to do so by extending them and then providing hook functions for PHP calls. This can reduce the internal calculation of variables, functions and other series of overhead.

Pay attention and don’t get lost

All right, everybody, that’s all for this article. All the people here are talented. As mentioned earlier, PHP has a lot of technical points, but there are too many to write, and you won’t read too much after you write it, so I have arranged it into PDF and document, if you need to click here shimo.im/docs/rjJttd… >>> How to grow as an architect