Home Backend Development PHP7 Detailed explanation of FAST_ZPP in PHP7 kernel

Detailed explanation of FAST_ZPP in PHP7 kernel

Jun 08, 2020 am 09:24 AM
php

Detailed explanation of FAST_ZPP in PHP7 kernel

从PHP7开始,大家可能会发现,不少函数不再使用传统的参数处理方式,而是改用了我们称之为Fast zend parameters parsing(FAST_ZPP)的新型方式, 比如在PHP7之前,count函数是这样的:

PHP_FUNCTION(count)
{
    zval *array;
    long mode = COUNT_NORMAL;
 
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &array, &mode) == FAILURE) {
        return;
    }
    ....
}
Copy after login

在PHP7以后,变成了:

PHP_FUNCTION(count)
{
    zval *array;
    zend_long mode = COUNT_NORMAL;
 
    ZEND_PARSE_PARAMETERS_START(1, 2)
        Z_PARAM_ZVAL(array)
        Z_PARAM_OPTIONAL
        Z_PARAM_LONG(mode)
    ZEND_PARSE_PARAMETERS_END();
    ...
}
Copy after login

很多PHP扩展开发的同学可能在初次接触的时候,会觉得很陌生,不要焦虑,让我慢慢道来 :)

当时在做PHPNG(PHP7的开发项目代号)的开发的时候,我们主要的发现性能提升点的一个方式就是bench各种大型实际项目,来发现占用资源比较大的部分,而最常用benchmark对象之一是wordpress,因为它够复杂,够慢,(它也是我们开发JIT的时候对主要bench目标:)) 代表了非OO型代码类的典型应用, 在实际的benchmark的过程中我们发现,将近有6%的耗时被zend_parse_parameters给占用了。

事实上zend_parameters_parsing确实是一个很庞大的函数:

ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...)

它根据type_spec字符串中指定的标识符,来处理输入参数,而这个参数符有很多种(具体含义可以参看: README.PARAMETER_PARSING_API):

a A b C d f h H l L o O p P r s S z * + | / !
Copy after login

根据不同的组合来表示我们的PHP函数要接受的参数类型,比如例子中的count, 通过”z|l”表示要接受一个zval类型的参数,和一个可选的long类型的mode参数,当zend_parse_parameters在runtime的时候被调用的时候,就会需要分析这些字符,然后调用对应的逻辑,对于一些本身就很简单的函数来说,比如count,这个开销就会显得很明显。

再回头来看这个函数的特点,我们会发现,比如对于count这个例子来说,其实type_spec在编译期就是确定的常量,也就是说,其实在编译的时候,我们就应该已经知道了”a|l”应该调用那些对应的参数处理逻辑。

而事实上,当代的编译器都具备这个基本优化能力, 比如对于如下的代码:

#include <stdlib.h>
 
#define AAA  1;
int main() {
    int a = AAA;
    if (a) {
        abort();
    }
    return 0;
}
Copy after login

如果我们尝试让编译优化(-o2)它,并检查生成的汇编:

main:
.LFB18:
    subq    $8, %rsp
    call    abort@PLT
Copy after login

大家可以看到,if判断已经被抹掉了, 因为在编译时刻, 就能知道a是1, if一定为真。

而FAST_ZPP就是充分借助了这个能力而来的一种新型的参数申明方式, 比如对于Z_PARAM_ZVAL(array)

#define Z_PARAM_ZVAL_EX(dest, check_null, separate) \
        if (separate) { \
            Z_PARAM_PROLOGUE(separate); \
            zend_parse_arg_zval_deref(_arg, &dest, check_null); \
        } else { \
            ++_i; \
            ZEND_ASSERT(_i <= _min_num_args || _optional==1); \
            ZEND_ASSERT(_i >  _min_num_args || _optional==0); \
            if (_optional && UNEXPECTED(_i >_num_args)) break; \
            _real_arg++; \
            zend_parse_arg_zval(_real_arg, &dest, check_null); \
        }
 
#define Z_PARAM_ZVAL(dest) \
    Z_PARAM_ZVAL_EX(dest, 0, 0)
Copy after login

在编译时刻就能被先替换为:

zend_parse_arg_zval(((zval*)execute_data) - 1, &array, 0);
Copy after login

而如果我们进一步审视zend_parse_arg_zval:

static zend_always_inline void zend_parse_arg_zval(zval *arg, zval **dest, int check_null)
{
    *dest = (check_null &&
        (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) ||
         (UNEXPECTED(Z_ISREF_P(arg)) &&
          UNEXPECTED(Z_TYPE_P(Z_REFVAL_P(arg)) == IS_NULL)))) ? NULL : arg;
}
Copy after login

我们会发现它也是一个inline申明的函数,而参数因为是常量,那么就可以进一步被evaluate成:

zval *array = ((zval*)execute_data) - 1;
Copy after login

怎么样,是不是一看就知道会快很多? 没有type_spec分析,没有额外的函数调用,直接获取到参数。

刚刚说到的inline函数可以在编译时期根据常数的剪枝内联, 也是用来避免同类函数的重复代码的很好的方法,在PHP7中也有大量使用,有兴趣的可以参看zend_hash.c中的很多相似函数的定义。

当然,这么做也有一个问题就是, 会增大我们程序的binary size, 这个也很容易理解, 比如对于count来说,本来原来只是调用一个外部函数,一个call指令就够了,但现在就会有很多内联进来的指令。

而binary size变大以后,执行时期的cache miss就会增大,也会影响性能,所以FAST_ZPP我们也不是建议全部使用, 而真是针对实际应用中调用频率比较大,并且本身函数逻辑较为简单的函数来使用.

总结一下,一般来说,我们自己写的扩展函数,并不需要一定使用FAST_ZPP, 因为如果自身是复杂的函数逻辑的, 这点开销对比起来,其实也还好了。

最后,附上新的FAST_ZPP API和老的参数描述之间的对应如下:

Detailed explanation of FAST_ZPP in PHP7 kernel

推荐教程:《PHP》《PHP7教程

The above is the detailed content of Detailed explanation of FAST_ZPP in PHP7 kernel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles