亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

聊一聊關于php源碼中refcount的疑問

瀏覽:101日期:2022-06-05 18:54:05

在瀏覽PHP源碼的時候,在眾多的*.stub.php中,發現了這樣的注釋,@refcount 1

通過翻看build/gen_stub.php源碼,發現了在解析*.stub.php文件時,關于返回信息的代碼。

<?php
class ReturnInfo {
    const REFCOUNT_0 = "0";
    const REFCOUNT_1 = "1";
    const REFCOUNT_N = "N";

    const REFCOUNTS = [
self::REFCOUNT_0,
self::REFCOUNT_1,
self::REFCOUNT_N,
    ];

    //...
    
    private function setRefcount(?string $refcount): void
    {
$type = $this->phpDocType ?? $this->type;
$isScalarType = $type !== null && $type->isScalar();

if ($refcount === null) {
    $this->refcount = $isScalarType ? self::REFCOUNT_0 : self::REFCOUNT_N;
    return;
}

if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) {
    throw new Exception("@refcount must have one of the following values: \"0\", \"1\", \"N\", $refcount given");
}

if ($isScalarType && $refcount !== self::REFCOUNT_0) {
    throw new Exception("A scalar return type of "" . $type->__toString() . "" must have a refcount of "" . self::REFCOUNT_0 . """);
}

if (!$isScalarType && $refcount === self::REFCOUNT_0) {
    throw new Exception("A non-scalar return type of "" . $type->__toString() . "" cannot have a refcount of "" . self::REFCOUNT_0 . """);
}

$this->refcount = $refcount;
    }

明顯,如果返回值類型是scalar,也就是標量(基本數據類型,整型、浮點型、字符串等),那么refcount指定為0,否則為N。如果設置了注釋,那么以注釋為最高優先級。

以函數ob_list_handlers為例:

/**
 * @return array<int, string>
 * @refcount 1
 */
function ob_list_handlers(): array {}

返回值是array,所以默認的refcount應該是N,但由于設置了注釋@refcount 1,所以返回值的引用計數被替換成1。

這些邏輯我能看懂,但設置返回值引用計數的目的是什么?我還是一頭霧水

我接著往下排查,發現通過返回值的引用計數,在生成func_info的時候,會有些不同。如果返回值引用計數為1或N,則會用對應的宏去初始化func_info結構體。如果是0,則不進入初始化列表。

以上的代碼邏輯依然可以在gen_stub.php中找到,1393行,getOptimizerInfo

public function getOptimizerInfo(): ?string {
if ($this->isMethod()) {
    return null;
}

if ($this->alias !== null) {
    return null;
}

if ($this->return->refcount !== ReturnInfo::REFCOUNT_1 && $this->return->phpDocType === null) {
    return null;
}

$type = $this->return->phpDocType ?? $this->return->type;
if ($type === null) {
    return null;
}

return "\tF" . $this->return->refcount . "("" . $this->name->__toString() . "", " . $type->toOptimizerTypeMask() . "),\n";
    }

獲取函數原型的refcount,生成諸如F1()FN()的代碼,生成的頭文件位置在Zend/Optimizer/zend_func_infos.h

static const func_info_t func_infos[] = {
    F1("zend_version", MAY_BE_STRING),
    FN("func_get_args", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),
    F1("get_class_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
    F1("get_class_methods", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    F1("get_included_files", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    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),
    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),
    F1("get_declared_classes", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    F1("get_declared_traits", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    F1("get_declared_interfaces", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    F1("get_defined_functions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),
    F1("get_defined_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
    F1("get_resource_type", MAY_BE_STRING),
    F1("get_loaded_extensions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
    F1("get_defined_constants", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
    F1("debug_backtrace", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
    F1("get_extension_funcs", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
    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),
    F1("bcadd", MAY_BE_STRING),
    F1("bcsub", MAY_BE_STRING),
    F1("bcmul", MAY_BE_STRING),
    F1("bcdiv", MAY_BE_STRING),
    F1("bcmod", MAY_BE_STRING),
    F1("bcpowmod", MAY_BE_STRING),
    F1("bcpow", MAY_BE_STRING),
    F1("bcsqrt", MAY_BE_STRING),
    FN("bzopen", MAY_BE_RESOURCE|MAY_BE_FALSE),
    F1("bzerror", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING),
    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),
    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),
    F1("curl_copy_handle", MAY_BE_OBJECT|MAY_BE_FALSE),
    //...
};

再去看看F1FN的宏定義。

typedef struct _func_info_t {
    const char *name;
    unsigned    name_len;
    uint32_t    info;
    info_func_t info_func;
} func_info_t;

#define F0(name, info) \
    {name, sizeof(name)-1, (info), NULL}
#define F1(name, info) \
    {name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}
#define FN(name, info) \
    {name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}
#define FC(name, callback) \
    {name, sizeof(name)-1, 0, callback}

僅僅是設置了不同的type maskF1設置了MAY_BE_RC1FN設置了MAY_BE_RCN | MAY_BE_RC1

依然一頭霧水,但是通過目錄名,我依稀能猜出這跟性能優化有關,跟JIT有關系。我決定繼續追查下去,看看這些初始化后的結構體在哪里使用過。

我們很清楚,設置位信息用|,那判斷有沒有設置肯定用&,全局搜索& MAY_BE_RCN,再看看哪些代碼跟優化有關,定位到了如下代碼,在zend_jit.c的530行:

#ifdef ZEND_JIT_USE_RC_INFERENCE
    /* Refcount may be increased by RETURN opcode */
    if ((info & MAY_BE_RC1) && !(info & MAY_BE_RCN)) {
for (j = 0; j < ssa->cfg.blocks_count; j++) {
    if ((ssa->cfg.blocks[j].flags & ZEND_BB_REACHABLE) &&
ssa->cfg.blocks[j].len > 0) {
const zend_op *opline = op_array->opcodes + ssa->cfg.blocks[j].start + ssa->cfg.blocks[j].len - 1;

if (opline->opcode == ZEND_RETURN) {
    if (opline->op1_type == IS_CV && opline->op1.var == EX_NUM_TO_VAR(var)) {
info |= MAY_BE_RCN;
break;
    }
}
    }
}
    }
#endif

如果返回值的引用計數是1,而不是N的時候,并且開啟了返回值引用計數推導功能,就走這段代碼。這段代碼又涉及到所謂SSA,靜態單賦值的編譯器設計方式。

在編譯器設計中,靜態單一賦值形式(通常縮寫為SSA形式或簡稱SSA)是中間表示(IR)的屬性,它要求每個變量只分配一次,并且每個變量在使用之前定義。原始IR中的現有變量被拆分為版本,在教科書中,新變量通常由原始名稱用下標表示,以便每次定義都有自己的版本。在SSA形式中,use-def鏈是顯式的,每個包含一個元素。

所以上面的代碼就是判斷SSA的cfg(control flow graph控制流圖)的塊是不是可達的,如果可達,執行條件中的代碼。

還是不太通透,雖然能推斷出設置refcount跟優化有關,跟靜態單一賦值有關,但在寫擴展的時候,什么時候該用@refcount 1,還是不太清楚。

 總結

到此這篇關于php源碼中refcount疑問的文章就介紹到這了,更多相關php源碼中refcount內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: PHP
主站蜘蛛池模板: 久久99精品久久久久久久不卡 | 91精品全国免费观看老司机 | 国产成人一区二区三区精品久久 | 一级黄色录像视频 | 青青黄色 | 高h文bl| 无需付费大片免费在线观看 | 久久精品道一区二区三区 | 久久这里只有精品2 | 日韩午夜高清福利片在线观看 | 国产成年网站v片在线观看 国产成版人视频网站免费下 | 久久久久久久九九九九 | 日韩欧美在线综合 | 手机在线1024 | 亚洲欧美精品日韩欧美 | 国产一级做a爰片久久毛片 国产一级做a爰片久久毛片99 | 国产精品日韩高清在线蜜芽 | 国产免费一区二区 | 日韩美一区二区三区 | 国产原创麻豆精品视频 | 国产亚洲精品观看91在线 | 在线你懂 | 久久本道综合色狠狠五月 | 亚洲 中文 欧美 日韩 在线人 | 黄色在线观看视频网站 | wwww国产| 免费啪视频观在线视频在线 | 国产精品欧美日韩精品 | 欧美一级高清在线观看 | 婷婷四房综合激情五月性色 | 久热99这里只有精品视频6 | 色综合中文字幕在线亚洲 | 亚洲丝袜第一页 | 久久久久免费精品国产小说 | 成人三级精品视频在线观看 | 一区二区三区网站 | 好硬好湿好爽再深一点h视频 | 毛片网站大全 | 68久久久久欧美精品观看 | 一级特黄特黄毛片欧美的 | 亚洲性色综合图区图片 |