关于PHP 7 refcount的困惑 [英] Confusion about PHP 7 refcount

查看:205
本文介绍了关于PHP 7 refcount的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

$s = "foobar";

$t = $s;

$u = $s;

echo PHP_VERSION . "\n";

debug_zval_dump($s);

xdebug_debug_zval('s');

在PHP 5.6.16中运行

Run in PHP 5.6.16

在PHP 7.0.2中运行

Run in PHP 7.0.2

我认为结果(PHP 7)应该是:

I think the result (PHP 7) should be:

string(6) "foobar" refcount(4)
s: (refcount=3, is_ref=0)="foobar"

我想知道有什么区别吗?需要一些解释.非常感谢.

I wonder what makes the difference? Need some explanation. Thanks a lot.

------更新------

------ update ------

Nikita Popov的-PHP 7 –内部发生了什么变化? (P41)

Nikita Popov's - PHP 7 – What changed internally? (P41)

http://www.slideshare.net/nikita_ppv/php- 7-内部发生了什么变化

推荐答案

在PHP 7中,zval可以引用计数或不引用计数. zval结构中有一个标志决定了这一点.

In PHP 7 a zval can be reference counted or not. There is a flag in the zval structure which determined this.

有些类型永远不会被引用.这些类型为null,bool,int和double.

There are some types which are never refcounted. These types are null, bool, int and double.

还有其他总是引用的类型.这些是对象,资源和引用.

There are other types which are always refcounted. These are objects, resources and references.

然后有一些类型,有时会被 引用.这些是字符串和数组.

And then there are types, which are sometimes refcounted. Those are strings and arrays.

对于字符串,未引用的变体称为中间字符串".如果您通常使用的是NTS(不是线程安全的)PHP 7构建,则代码中的所有字符串文字都会被保留.这些实习生字符串将进行重复数据删除(即,只有一个实习生字符串具有一定的内容),并保证在请求的整个过程中都存在,因此无需对其使用引用计数.如果您使用opcache,这些字符串将存在于共享内存中,在这种情况下,您不能对其使用引用计数(因为我们的引用计数机制是非原子的).插入的字符串的虚拟引用计数为1,这就是您在此处看到的.

For strings the not-refcounted variant is called an "interned string". If you're using an NTS (not thread-safe) PHP 7 build, which you typically are, all string literals in your code will be interned. These interned strings are deduplicated (i.e. there is only one interned string with a certain content) and are guaranteed to exist for the full duration of the request, so there is no need to use reference counting for them. If you use opcache, these strings will live in shared memory, in which case you can't use reference counting for them (as our refcounting mechanism is non-atomic). Interned strings have a dummy refcount of 1, which is what you're seeing here.

对于数组,未引用的变体称为不可变数组".如果使用opcache,则代码中的常量数组文字将转换为不可变数组.再次,这些驻留在共享内存中,因此不能使用引用计数.不可变数组的虚拟引用计数为2,因为它使我们可以优化某些分离路径.

For arrays the not-refcounted variant is called an "immutable array". If you use opcache, then constant array literals in your code will be converted into immutable arrays. Once again, these live in shared memory and as such must not use refcounting. Immutable arrays have a dummy refcount of 2, as it allows us to optimize certain separation paths.

这篇关于关于PHP 7 refcount的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆