What optimizations have been made in PHP7

A zval uses stack memory

In the Zend engine and extensions, a PHP variable is often created, and the bottom layer is a zval pointer. Previous versions dynamically allocated a zval memory from the heap through MAKE_STD_ZVAL. And PHP7 can use the stack memory directly. Variables created in PHP code are also optimized, PHP7 preallocates zval directly on stack memory. This saves a lot of memory allocation and memory management operations.

PHP5

zval*val; MAKE_STD_ZVAL(val);

 

PHP7

zval val;

Second, zend_string stores the hash value, and the array query no longer needs to repeatedly calculate the hash value

PHP7 created a new type called zend_string for strings separately. In addition to the char * pointer and length, a hash field was added to store the hash value of the string. Array in PHP is the core data structure. There are often a large number of $array[$key] operations in PHP programs. Although the time complexity of hashtable search is O(1), the conversion of $key to hash value needs to be calculated. . Not only the array operation, in fact, when accessing the class attributes, class methods, and functions at the bottom of PHP, the corresponding pointer must be found through the hashtable first, and then the corresponding operation is performed. Before PHP7, the Zend engine would have a lot of CPU time to calculate the hash value.

In fact, after the PHP program runs, the value of $key is unchanged in most cases. PHP7 simply saves this hash value and uses it directly next time, which saves a lot of hash calculation operations, PHP's hashtable and C numbers

Group performance is consistent.

 

 

From the callgrind performance analysis of the actual project, it will be found that the two operations of alloc and hash occupy a considerable proportion of CPU time. After PHP7 optimization, the CPU time occupied by these two operations has been greatly reduced. (Note: zend_hash still accounts for 12%, because the overall CPU is reduced, so the total time is reduced a lot)

 

 

Three hashtable buckets directly store data

Each element of PHP5's hashtable is a Bucket *, while PHP7 directly stores the Bucket, which reduces the number of memory requests and improves the Cache hit rate and memory access speed.

Four zend_parse_parameters changed to macro implementation

When the C extension function of PHP and the variables in PHP are used for parameter input, the zend_parse_parameters() function should be used. This function finds the zval pointer corresponding to PHP according to a string parameter, and then assigns it. This function actually has a certain performance cost. PHP7 directly replaces the zend_parse_parameters function with macros. In C expansion, it is no longer necessary to use zend_parse_parameters for parameter-by-parameter search. After the macro is expanded, parameter assignment is automatically implemented. This alone improves performance by 5%.

Five new additions of 4 OPCODEs

Many PHP programs use call_user_function, is_int/string/array, strlen , and defined functions a lot. PHP5 is provided in the form of extension functions. In PHP7, these four types of functions are changed to ZendVM's OPCODE instructions, which are faster to execute.

Six other more optimizations

In addition to the above 5 main optimization points, PHP7 has other more detailed performance optimizations. For example, the basic types int, float, bool, etc. are changed to direct value copy, the sorting algorithm is improved, PCRE with JIT, execute_data and opline use global registers, etc. Performance optimizations in PHP7 will continue.

Compared with PHP5.6, the performance of PHP7-alpha has been improved by nearly 3 times. Here's how WordPress behaves on PHP7:

 

What's new in PHP7

In addition to performance optimization, PHP7 has added 2 important new features.

1. Variable Type

The parameters and return values ​​of the PHP7 version of the function have increased type qualification. Why does PHP add types? In fact, this feature is to prepare for the JIT feature of PHP7.1. After adding types, PHP JIT can accurately determine the variable type and generate the best machine instructions.

function test(int $a, string $b, array $c) : int {
  //code
}

2. Error exception

After a PHP program error, the Zend engine will have a fatal error and terminate the program. PHP7 can use try/catch to catch the error. The bottom layer uses Exception instead of Fatal Error. This feature indicates that the PHP language is developing in a more standardized direction. The application layer and the bottom layer are all unified in the way of error throwing as exceptions.

try {
  non_exists_func();
} catch (EngineException $e) {
  echo "Exception: {$e->getMessage()}\n";
}

3. Anonymous class

$test = new class("Hello World") {
  public function __construct($greeting) {
      $this->greeting = $greeting;
  }
};

PHP7 and JIT

The initial direction of PHP7 performance optimization is not the above, but JIT. JIT is the abbreviation of just in time, which means that the instructions are converted into binary machine code at runtime. The bottom layer of the JVM engine of the Java language is to use JIT to compile Java bytecode into binary machine code for execution. There was an intermediate version in the PHP7 development process that was based on JIT. Later, the development team found that after using JIT, there was not much performance improvement for the actual project, so PHP7 finally gave up the JIT solution, and the PHP7.0-final version will not carry JIT. characteristic.

However, if it is a computationally intensive program, it is different. Using JIT to compile PHP OpCode into machine code, the performance of the operation will be greatly improved. The official PHP development team restarted the development of JIT at the end of 2014.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025370&siteId=291194637