{"id":10006770,"date":"2022-10-31T13:44:16","date_gmt":"2022-10-31T05:44:16","guid":{"rendered":"https:\/\/ityet.com\/?p=10006770"},"modified":"2022-10-31T13:44:16","modified_gmt":"2022-10-31T05:44:16","slug":"%e5%85%b3%e4%ba%8ephp%e6%ba%90%e7%a0%81%e4%b8%adrefcount%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006770","title":{"rendered":"\u5173\u4e8ephp\u6e90\u7801\u4e2drefcount\u7684\u7591\u95ee"},"content":{"rendered":"<p>\u5728\u6d4f\u89c8<code>PHP<\/code>\u6e90\u7801\u7684\u65f6\u5019\uff0c\u5728\u4f17\u591a\u7684<code>*.stub.php<\/code>\u4e2d\uff0c\u53d1\u73b0\u4e86\u8fd9\u6837\u7684\u6ce8\u91ca\uff0c<strong>@refcount 1<\/strong>\u3002<\/p>\n<p>\u901a\u8fc7\u7ffb\u770b<code>build\/gen_stub.php<\/code>\u6e90\u7801\uff0c\u53d1\u73b0\u4e86\u5728\u89e3\u6790<code>*.stub.php<\/code>\u6587\u4ef6\u65f6\uff0c\u5173\u4e8e\u8fd4\u56de\u4fe1\u606f\u7684\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"php\">&lt;?php\nclass ReturnInfo {\n    const REFCOUNT_0 = &quot;0&quot;;\n    const REFCOUNT_1 = &quot;1&quot;;\n    const REFCOUNT_N = &quot;N&quot;;\n\n    const REFCOUNTS = [\n        self::REFCOUNT_0,\n        self::REFCOUNT_1,\n        self::REFCOUNT_N,\n    ];\n\n    \/\/...\n    \n    private function setRefcount(?string $refcount): void\n    {\n        $type = $this-&gt;phpDocType ?? $this-&gt;type;\n        $isScalarType = $type !== null &amp;&amp; $type-&gt;isScalar();\n\n        if ($refcount === null) {\n            $this-&gt;refcount = $isScalarType ? self::REFCOUNT_0 : self::REFCOUNT_N;\n            return;\n        }\n\n        if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) {\n            throw new Exception(&quot;@refcount must have one of the following values: \/&quot;0\/&quot;, \/&quot;1\/&quot;, \/&quot;N\/&quot;, $refcount given&quot;);\n        }\n\n        if ($isScalarType &amp;&amp; $refcount !== self::REFCOUNT_0) {\n            throw new Exception('A scalar return type of &quot;' . $type-&gt;__toString() . '&quot; must have a refcount of &quot;' . self::REFCOUNT_0 . '&quot;');\n        }\n\n        if (!$isScalarType &amp;&amp; $refcount === self::REFCOUNT_0) {\n            throw new Exception('A non-scalar return type of &quot;' . $type-&gt;__toString() . '&quot; cannot have a refcount of &quot;' . self::REFCOUNT_0 . '&quot;');\n        }\n\n        $this-&gt;refcount = $refcount;\n    }\n    <\/code><\/pre>\n<p>\u660e\u663e\uff0c\u5982\u679c\u8fd4\u56de\u503c\u7c7b\u578b\u662f<code>scalar<\/code>\uff0c\u4e5f\u5c31\u662f\u6807\u91cf\uff08\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6574\u578b\u3001\u6d6e\u70b9\u578b\u3001\u5b57\u7b26\u4e32\u7b49\uff09\uff0c\u90a3\u4e48<code>refcount<\/code>\u6307\u5b9a\u4e3a0\uff0c\u5426\u5219\u4e3aN\u3002\u5982\u679c\u8bbe\u7f6e\u4e86\u6ce8\u91ca\uff0c\u90a3\u4e48\u4ee5\u6ce8\u91ca\u4e3a\u6700\u9ad8\u4f18\u5148\u7ea7\u3002<\/p>\n<p>\u4ee5\u51fd\u6570<code>ob_list_handlers<\/code>\u4e3a\u4f8b:<\/p>\n<pre><code class=\"php\">\/**\n * @return array&lt;int, string&gt;\n * @refcount 1\n *\/\nfunction ob_list_handlers(): array {}<\/code><\/pre>\n<p>\u8fd4\u56de\u503c\u662f<code>array<\/code>\uff0c\u6240\u4ee5\u9ed8\u8ba4\u7684<code>refcount<\/code>\u5e94\u8be5\u662fN\uff0c\u4f46\u7531\u4e8e\u8bbe\u7f6e\u4e86\u6ce8\u91ca<code>@refcount 1<\/code>\uff0c\u6240\u4ee5\u8fd4\u56de\u503c\u7684\u5f15\u7528\u8ba1\u6570\u88ab\u66ff\u6362\u62101\u3002<\/p>\n<blockquote><p>\u8fd9\u4e9b\u903b\u8f91\u6211\u80fd\u770b\u61c2\uff0c\u4f46\u8bbe\u7f6e\u8fd4\u56de\u503c\u5f15\u7528\u8ba1\u6570\u7684\u76ee\u7684\u662f\u4ec0\u4e48\uff1f\u6211\u8fd8\u662f\u4e00\u5934\u96fe\u6c34<\/p><\/blockquote>\n<p>\u6211\u63a5\u7740\u5f80\u4e0b\u6392\u67e5\uff0c\u53d1\u73b0\u901a\u8fc7\u8fd4\u56de\u503c\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5728\u751f\u6210<code>func_info<\/code>\u7684\u65f6\u5019\uff0c\u4f1a\u6709\u4e9b\u4e0d\u540c\u3002\u5982\u679c\u8fd4\u56de\u503c\u5f15\u7528\u8ba1\u6570\u4e3a1\u6216N\uff0c\u5219\u4f1a\u7528\u5bf9\u5e94\u7684\u5b8f\u53bb\u521d\u59cb\u5316<code>func_info<\/code>\u7ed3\u6784\u4f53\u3002\u5982\u679c\u662f0\uff0c\u5219\u4e0d\u8fdb\u5165\u521d\u59cb\u5316\u5217\u8868\u3002<\/p>\n<p>\u4ee5\u4e0a\u7684\u4ee3\u7801\u903b\u8f91\u4f9d\u7136\u53ef\u4ee5\u5728<code>gen_stub.php<\/code>\u4e2d\u627e\u5230\uff0c1393\u884c\uff0c<code>getOptimizerInfo<\/code>\u3002<\/p>\n<pre><code class=\"php\">public function getOptimizerInfo(): ?string {\n        if ($this-&gt;isMethod()) {\n            return null;\n        }\n\n        if ($this-&gt;alias !== null) {\n            return null;\n        }\n\n        if ($this-&gt;return-&gt;refcount !== ReturnInfo::REFCOUNT_1 &amp;&amp; $this-&gt;return-&gt;phpDocType === null) {\n            return null;\n        }\n\n        $type = $this-&gt;return-&gt;phpDocType ?? $this-&gt;return-&gt;type;\n        if ($type === null) {\n            return null;\n        }\n\n        return &quot;\/tF&quot; . $this-&gt;return-&gt;refcount . '(&quot;' . $this-&gt;name-&gt;__toString() . '&quot;, ' . $type-&gt;toOptimizerTypeMask() . &quot;),\/n&quot;;\n    }<\/code><\/pre>\n<p>\u83b7\u53d6\u51fd\u6570\u539f\u578b\u7684<code>refcount<\/code>\uff0c\u751f\u6210\u8bf8\u5982<code>F1()<\/code>\u6216<code>FN()<\/code>\u7684\u4ee3\u7801\uff0c\u751f\u6210\u7684\u5934\u6587\u4ef6\u4f4d\u7f6e\u5728<code>Zend\/Optimizer\/zend_func_infos.h<\/code>\u3002<\/p>\n<pre><code class=\"h\">static const func_info_t func_infos[] = {\n    F1(&quot;zend_version&quot;, MAY_BE_STRING),\n    FN(&quot;func_get_args&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),\n    F1(&quot;get_class_vars&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),\n    F1(&quot;get_class_methods&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;get_included_files&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    FN(&quot;set_error_handler&quot;, 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),\n    FN(&quot;set_exception_handler&quot;, 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),\n    F1(&quot;get_declared_classes&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;get_declared_traits&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;get_declared_interfaces&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;get_defined_functions&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),\n    F1(&quot;get_defined_vars&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),\n    F1(&quot;get_resource_type&quot;, MAY_BE_STRING),\n    F1(&quot;get_loaded_extensions&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;get_defined_constants&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),\n    F1(&quot;debug_backtrace&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),\n    F1(&quot;get_extension_funcs&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),\n    F1(&quot;gc_status&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE),\n    F1(&quot;bcadd&quot;, MAY_BE_STRING),\n    F1(&quot;bcsub&quot;, MAY_BE_STRING),\n    F1(&quot;bcmul&quot;, MAY_BE_STRING),\n    F1(&quot;bcdiv&quot;, MAY_BE_STRING),\n    F1(&quot;bcmod&quot;, MAY_BE_STRING),\n    F1(&quot;bcpowmod&quot;, MAY_BE_STRING),\n    F1(&quot;bcpow&quot;, MAY_BE_STRING),\n    F1(&quot;bcsqrt&quot;, MAY_BE_STRING),\n    FN(&quot;bzopen&quot;, MAY_BE_RESOURCE|MAY_BE_FALSE),\n    F1(&quot;bzerror&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING),\n    F1(&quot;cal_from_jd&quot;, MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL),\n    F1(&quot;cal_info&quot;, 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),\n    F1(&quot;curl_copy_handle&quot;, MAY_BE_OBJECT|MAY_BE_FALSE),\n    \/\/...\n};<\/code><\/pre>\n<p>\u518d\u53bb\u770b\u770b<code>F1<\/code>\u548c<code>FN<\/code>\u7684\u5b8f\u5b9a\u4e49\u3002<\/p>\n<pre><code class=\"c\">typedef struct _func_info_t {\n    const char *name;\n    unsigned    name_len;\n    uint32_t    info;\n    info_func_t info_func;\n} func_info_t;\n\n#define F0(name, info) \/\n    {name, sizeof(name)-1, (info), NULL}\n#define F1(name, info) \/\n    {name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}\n#define FN(name, info) \/\n    {name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}\n#define FC(name, callback) \/\n    {name, sizeof(name)-1, 0, callback}<\/code><\/pre>\n<p>\u4ec5\u4ec5\u662f\u8bbe\u7f6e\u4e86\u4e0d\u540c\u7684<code>type mask<\/code>\uff0c<code>F1<\/code>\u8bbe\u7f6e\u4e86<code>MAY_BE_RC1<\/code>\uff0c<code>FN<\/code>\u8bbe\u7f6e\u4e86<code>MAY_BE_RCN | MAY_BE_RC1<\/code>\u3002<\/p>\n<blockquote><p>\u4f9d\u7136\u4e00\u5934\u96fe\u6c34\uff0c\u4f46\u662f\u901a\u8fc7\u76ee\u5f55\u540d\uff0c\u6211\u4f9d\u7a00\u80fd\u731c\u51fa\u8fd9\u8ddf\u6027\u80fd\u4f18\u5316\u6709\u5173\uff0c\u8ddfJIT\u6709\u5173\u7cfb\u3002\u6211\u51b3\u5b9a\u7ee7\u7eed\u8ffd\u67e5\u4e0b\u53bb\uff0c\u770b\u770b\u8fd9\u4e9b\u521d\u59cb\u5316\u540e\u7684\u7ed3\u6784\u4f53\u5728\u54ea\u91cc\u4f7f\u7528\u8fc7\u3002<\/p><\/blockquote>\n<p>\u6211\u4eec\u5f88\u6e05\u695a\uff0c\u8bbe\u7f6e\u4f4d\u4fe1\u606f\u7528<code>|<\/code>\uff0c\u90a3\u5224\u65ad\u6709\u6ca1\u6709\u8bbe\u7f6e\u80af\u5b9a\u7528<code>&amp;<\/code>\uff0c\u5168\u5c40\u641c\u7d22<code>&amp; MAY_BE_RCN<\/code>\uff0c\u518d\u770b\u770b\u54ea\u4e9b\u4ee3\u7801\u8ddf\u4f18\u5316\u6709\u5173\uff0c\u5b9a\u4f4d\u5230\u4e86\u5982\u4e0b\u4ee3\u7801\uff0c\u5728<code>zend_jit.c<\/code>\u7684530\u884c\uff1a<\/p>\n<pre><code class=\"c\">\n#ifdef ZEND_JIT_USE_RC_INFERENCE\n    \/* Refcount may be increased by RETURN opcode *\/\n    if ((info &amp; MAY_BE_RC1) &amp;&amp; !(info &amp; MAY_BE_RCN)) {\n        for (j = 0; j &lt; ssa-&gt;cfg.blocks_count; j++) {\n            if ((ssa-&gt;cfg.blocks[j].flags &amp; ZEND_BB_REACHABLE) &amp;&amp;\n                ssa-&gt;cfg.blocks[j].len &gt; 0) {\n                const zend_op *opline = op_array-&gt;opcodes + ssa-&gt;cfg.blocks[j].start + ssa-&gt;cfg.blocks[j].len - 1;\n\n                if (opline-&gt;opcode == ZEND_RETURN) {\n                    if (opline-&gt;op1_type == IS_CV &amp;&amp; opline-&gt;op1.var == EX_NUM_TO_VAR(var)) {\n                        info |= MAY_BE_RCN;\n                        break;\n                    }\n                }\n            }\n        }\n    }\n#endif\n<\/code><\/pre>\n<p>\u5982\u679c\u8fd4\u56de\u503c\u7684\u5f15\u7528\u8ba1\u6570\u662f1\uff0c\u800c\u4e0d\u662fN\u7684\u65f6\u5019\uff0c\u5e76\u4e14\u5f00\u542f\u4e86\u8fd4\u56de\u503c\u5f15\u7528\u8ba1\u6570\u63a8\u5bfc\u529f\u80fd\uff0c\u5c31\u8d70\u8fd9\u6bb5\u4ee3\u7801\u3002\u8fd9\u6bb5\u4ee3\u7801\u53c8\u6d89\u53ca\u5230\u6240\u8c13<code>SSA<\/code>\uff0c\u9759\u6001\u5355\u8d4b\u503c\u7684\u7f16\u8bd1\u5668\u8bbe\u8ba1\u65b9\u5f0f\u3002<\/p>\n<blockquote><p>\u5728\u7f16\u8bd1\u5668\u8bbe\u8ba1\u4e2d\uff0c\u9759\u6001\u5355\u4e00\u8d4b\u503c\u5f62\u5f0f\uff08\u901a\u5e38\u7f29\u5199\u4e3aSSA\u5f62\u5f0f\u6216\u7b80\u79f0SSA\uff09\u662f\u4e2d\u95f4\u8868\u793a\uff08IR\uff09\u7684\u5c5e\u6027\uff0c\u5b83\u8981\u6c42\u6bcf\u4e2a\u53d8\u91cf\u53ea\u5206\u914d\u4e00\u6b21\uff0c\u5e76\u4e14\u6bcf\u4e2a\u53d8\u91cf\u5728\u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u3002\u539f\u59cbIR\u4e2d\u7684\u73b0\u6709\u53d8\u91cf\u88ab\u62c6\u5206\u4e3a\u7248\u672c\uff0c\u5728\u6559\u79d1\u4e66\u4e2d\uff0c\u65b0\u53d8\u91cf\u901a\u5e38\u7531\u539f\u59cb\u540d\u79f0\u7528\u4e0b\u6807\u8868\u793a\uff0c\u4ee5\u4fbf\u6bcf\u6b21\u5b9a\u4e49\u90fd\u6709\u81ea\u5df1\u7684\u7248\u672c\u3002\u5728SSA\u5f62\u5f0f\u4e2d\uff0cuse-def\u94fe\u662f\u663e\u5f0f\u7684\uff0c\u6bcf\u4e2a\u5305\u542b\u4e00\u4e2a\u5143\u7d20\u3002<\/p><\/blockquote>\n<p>\u6240\u4ee5\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u662f\u5224\u65adSSA\u7684cfg(control flow graph\u63a7\u5236\u6d41\u56fe)\u7684\u5757\u662f\u4e0d\u662f\u53ef\u8fbe\u7684\uff0c\u5982\u679c\u53ef\u8fbe\uff0c\u6267\u884c\u6761\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<blockquote><p>\u8fd8\u662f\u4e0d\u592a\u901a\u900f\uff0c\u867d\u7136\u80fd\u63a8\u65ad\u51fa\u8bbe\u7f6erefcount\u8ddf\u4f18\u5316\u6709\u5173\uff0c\u8ddf\u9759\u6001\u5355\u4e00\u8d4b\u503c\u6709\u5173\uff0c\u4f46\u5728\u5199\u6269\u5c55\u7684\u65f6\u5019\uff0c\u4ec0\u4e48\u65f6\u5019\u8be5\u7528@refcount 1\uff0c\u8fd8\u662f\u4e0d\u592a\u6e05\u695a\u3002\u5e0c\u671b\u5bf9\u6e90\u7801\u6709\u6df1\u5165\u7814\u7a76\u7684\u670b\u53cb\u53ef\u4ee5\u63d0\u70b9\u6211\u4e00\u4e0b\uff0c\u8c22\u8c22\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042715840\">https:\/\/segmentfault.com\/a\/1190000042715840<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6d4f\u89c8PHP\u6e90\u7801\u7684\u65f6\u5019\uff0c\u5728\u4f17\u591a\u7684*.stub.php\u4e2d\uff0c\u53d1\u73b0\u4e86\u8fd9\u6837\u7684\u6ce8\u91ca\uff0c@refcount 1\u3002 \u901a\u8fc7\u7ffb\u770bb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006770","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006770"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006770\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}