返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
PHP

PHP7-0 New Features

Variable Types PHP 7 added type constraints for function parameters and return values. Why did PHP add types? In fact, this feature was preparation for the JIT feature in PHP 7.1. With types added, PHP JIT can accurately determine variable types and generate the optimal machine instructions. (Optimization for compute-intensive operations) Usage example function test(int $a, string $b, array $c) : int { //code } Error Exceptions In the past, PHP programs

曦远 (DealiAxy)约 9 分钟
目录14 个章节

Variable Types

PHP 7 added type constraints for function parameters and return values. Why did PHP add types? In fact, this feature was preparation for the JIT feature in PHP 7.1. With types added, PHP JIT can accurately determine variable types and generate the optimal machine instructions. (Optimization for compute-intensive operations)

Usage example

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

Errors and Exceptions

In the past, after a PHP program encountered an error, the Zend engine would raise a fatal error and terminate the program. PHP 7 can use try/catch to catch errors.

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

PHP 7 Performance Optimizations

zval uses stack memory

In the Zend engine and extensions, a PHP variable often needs to be created, which at the bottom is a zval pointer. Previous versions dynamically allocated zval memory from the heap via MAKE_STD_ZVAL, while PHP 7 can use stack memory directly. Benefit: saves the cost of memory allocation

Underlying code

//php5
zval *val;
MAKE_STD_ZVAL(val);

//php7
zval val;

zend_string stores the hash value; array lookups no longer need to recompute the hash

PHP 7 created a new type specifically for strings called zend_string. In addition to a char * pointer and length, it added a hash field to store the string's hash value. Array key lookups no longer need to repeatedly compute the hash value.

struct _zend_string{
    zend_refcounted     gc;
    zend_ulong          h;
    size_t              len;
    char                val[1];
};

Data is stored directly in hashtable buckets, reducing the number of memory allocations and improving Cache hit rate and memory access speed

In previous versions of PHP, every time data was inserted into an array, memory had to be allocated once, and the memory was stored on different memory pages, which lowered the hit rate on access.

zend_parse_parameters changed to a macro implementation, 5% performance improvement

Improves PHP extension performance

4 new OPCODEs added: call_user_function, is_int/string/array, strlen, defined — these 4 functions became PHP OpCode instructions, making them faster

Other performance optimizations

  • Basic types such as int, float, bool changed to direct value copy
  • Sorting algorithm improvements
  • PCRE with Jit (regular expressions)
  • execute_data and opline use global registers
  • Use the PGO feature of gdb 4.8

PHP 7 JIT

The PHP 7.0-final version will not include the JIT feature

JIT is the abbreviation of Just in Time, meaning converting instructions to binary machine code at runtime. For compute-intensive programs, JIT can convert PHP OpCode directly into machine code, greatly improving performance. The PHP development team restarted the JIT development plan themselves, and PHP 7.1 is expected to include the JIT feature

Why PHP 7.0 does not have JIT

JIT does not provide much of a performance improvement for real-world projects such as WordPress

PHP Built-in Development Test Server

cd ~/php
php -S 0.0.0.0:8080

This starts a server with ~/php as the website root directory.

About


For more interesting tips, follow my WeChat official account: DealiAxy Every article is also collected on my blog: blog.deali.cn

_EOF_

PHP7-0 New Features

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…