[PHP] 聊一聊关于php源码中refcount的疑问

2204 0
Honkers 2022-11-6 09:04:11 | 显示全部楼层 |阅读模式
在浏览PHP源码的时候,在众多的*.stub.php中,发现了这样的注释,@refcount 1
通过翻看build/gen_stub.php源码,发现了在解析*.stub.php文件时,关于返回信息的代码。
  1. <?php
  2. class ReturnInfo {
  3.     const REFCOUNT_0 = "0";
  4.     const REFCOUNT_1 = "1";
  5.     const REFCOUNT_N = "N";
  6.     const REFCOUNTS = [
  7.         self::REFCOUNT_0,
  8.         self::REFCOUNT_1,
  9.         self::REFCOUNT_N,
  10.     ];
  11.     //...
  12.     private function setRefcount(?string $refcount): void
  13.     {
  14.         $type = $this->phpDocType ?? $this->type;
  15.         $isScalarType = $type !== null && $type->isScalar();
  16.         if ($refcount === null) {
  17.             $this->refcount = $isScalarType ? self::REFCOUNT_0 : self::REFCOUNT_N;
  18.             return;
  19.         }
  20.         if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) {
  21.             throw new Exception("@refcount must have one of the following values: "0", "1", "N", $refcount given");
  22.         }
  23.         if ($isScalarType && $refcount !== self::REFCOUNT_0) {
  24.             throw new Exception('A scalar return type of "' . $type->__toString() . '" must have a refcount of "' . self::REFCOUNT_0 . '"');
  25.         }
  26.         if (!$isScalarType && $refcount === self::REFCOUNT_0) {
  27.             throw new Exception('A non-scalar return type of "' . $type->__toString() . '" cannot have a refcount of "' . self::REFCOUNT_0 . '"');
  28.         }
  29.         $this->refcount = $refcount;
  30.     }
复制代码
明显,如果返回值类型是scalar,也就是标量(基本数据类型,整型、浮点型、字符串等),那么refcount指定为0,否则为N。如果设置了注释,那么以注释为最高优先级。
以函数ob_list_handlers为例:
  1. /**
  2. * @return array<int, string>
  3. * @refcount 1
  4. */
  5. function ob_list_handlers(): array {}
复制代码
返回值是array,所以默认的refcount应该是N,但由于设置了注释@refcount 1,所以返回值的引用计数被替换成1。
这些逻辑我能看懂,但设置返回值引用计数的目的是什么?我还是一头雾水
我接着往下排查,发现通过返回值的引用计数,在生成func_info的时候,会有些不同。如果返回值引用计数为1或N,则会用对应的宏去初始化func_info结构体。如果是0,则不进入初始化列表。
以上的代码逻辑依然可以在gen_stub.php中找到,1393行,getOptimizerInfo。
  1. public function getOptimizerInfo(): ?string {
  2.         if ($this->isMethod()) {
  3.             return null;
  4.         }
  5.         if ($this->alias !== null) {
  6.             return null;
  7.         }
  8.         if ($this->return->refcount !== ReturnInfo::REFCOUNT_1 && $this->return->phpDocType === null) {
  9.             return null;
  10.         }
  11.         $type = $this->return->phpDocType ?? $this->return->type;
  12.         if ($type === null) {
  13.             return null;
  14.         }
  15.         return "\tF" . $this->return->refcount . '("' . $this->name->__toString() . '", ' . $type->toOptimizerTypeMask() . "),\n";
  16.     }
复制代码
获取函数原型的refcount,生成诸如F1()或FN()的代码,生成的头文件位置在Zend/Optimizer/zend_func_infos.h。
  1. static const func_info_t func_infos[] = {
  2.     F1("zend_version", MAY_BE_STRING),
  3.     FN("func_get_args", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),
  4.     F1("get_class_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
  5.     F1("get_class_methods", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  6.     F1("get_included_files", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  7.     FN("set_error_handler", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT|MAY_BE_NULL),
  8.     FN("set_exception_handler", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT|MAY_BE_NULL),
  9.     F1("get_declared_classes", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  10.     F1("get_declared_traits", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  11.     F1("get_declared_interfaces", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  12.     F1("get_defined_functions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),
  13.     F1("get_defined_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
  14.     F1("get_resource_type", MAY_BE_STRING),
  15.     F1("get_loaded_extensions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
  16.     F1("get_defined_constants", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
  17.     F1("debug_backtrace", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
  18.     F1("get_extension_funcs", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
  19.     F1("gc_status", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE),
  20.     F1("bcadd", MAY_BE_STRING),
  21.     F1("bcsub", MAY_BE_STRING),
  22.     F1("bcmul", MAY_BE_STRING),
  23.     F1("bcdiv", MAY_BE_STRING),
  24.     F1("bcmod", MAY_BE_STRING),
  25.     F1("bcpowmod", MAY_BE_STRING),
  26.     F1("bcpow", MAY_BE_STRING),
  27.     F1("bcsqrt", MAY_BE_STRING),
  28.     FN("bzopen", MAY_BE_RESOURCE|MAY_BE_FALSE),
  29.     F1("bzerror", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING),
  30.     F1("cal_from_jd", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL),
  31.     F1("cal_info", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY),
  32.     F1("curl_copy_handle", MAY_BE_OBJECT|MAY_BE_FALSE),
  33.     //...
  34. };
复制代码
再去看看F1和FN的宏定义。
  1. typedef struct _func_info_t {
  2.     const char *name;
  3.     unsigned    name_len;
  4.     uint32_t    info;
  5.     info_func_t info_func;
  6. } func_info_t;
  7. #define F0(name, info) \
  8.     {name, sizeof(name)-1, (info), NULL}
  9. #define F1(name, info) \
  10.     {name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}
  11. #define FN(name, info) \
  12.     {name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}
  13. #define FC(name, callback) \
  14.     {name, sizeof(name)-1, 0, callback}
复制代码
仅仅是设置了不同的type mask,F1设置了MAY_BE_RC1,FN设置了MAY_BE_RCN | MAY_BE_RC1。
依然一头雾水,但是通过目录名,我依稀能猜出这跟性能优化有关,跟JIT有关系。我决定继续追查下去,看看这些初始化后的结构体在哪里使用过。
我们很清楚,设置位信息用|,那判断有没有设置肯定用&,全局搜索& MAY_BE_RCN,再看看哪些代码跟优化有关,定位到了如下代码,在zend_jit.c的530行:
  1. #ifdef ZEND_JIT_USE_RC_INFERENCE
  2.     /* Refcount may be increased by RETURN opcode */
  3.     if ((info & MAY_BE_RC1) && !(info & MAY_BE_RCN)) {
  4.         for (j = 0; j < ssa->cfg.blocks_count; j++) {
  5.             if ((ssa->cfg.blocks[j].flags & ZEND_BB_REACHABLE) &&
  6.                 ssa->cfg.blocks[j].len > 0) {
  7.                 const zend_op *opline = op_array->opcodes + ssa->cfg.blocks[j].start + ssa->cfg.blocks[j].len - 1;
  8.                 if (opline->opcode == ZEND_RETURN) {
  9.                     if (opline->op1_type == IS_CV && opline->op1.var == EX_NUM_TO_VAR(var)) {
  10.                         info |= MAY_BE_RCN;
  11.                         break;
  12.                     }
  13.                 }
  14.             }
  15.         }
  16.     }
  17. #endif
复制代码
如果返回值的引用计数是1,而不是N的时候,并且开启了返回值引用计数推导功能,就走这段代码。这段代码又涉及到所谓SSA,静态单赋值的编译器设计方式。
在编译器设计中,静态单一赋值形式(通常缩写为SSA形式或简称SSA)是中间表示(IR)的属性,它要求每个变量只分配一次,并且每个变量在使用之前定义。原始IR中的现有变量被拆分为版本,在教科书中,新变量通常由原始名称用下标表示,以便每次定义都有自己的版本。在SSA形式中,use-def链是显式的,每个包含一个元素。
所以上面的代码就是判断SSA的cfg(control flow graph控制流图)的块是不是可达的,如果可达,执行条件中的代码。
还是不太通透,虽然能推断出设置refcount跟优化有关,跟静态单一赋值有关,但在写扩展的时候,什么时候该用@refcount 1,还是不太清楚。
总结
到此这篇关于php源码中refcount疑问的文章就介绍到这了,更多相关php源码中refcount内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行