简体   繁体   English

关于PHP 7 refcount的困惑

[英]Confusion about PHP 7 refcount

<?php

$s = "foobar";

$t = $s;

$u = $s;

echo PHP_VERSION . "\n";

debug_zval_dump($s);

xdebug_debug_zval('s');

Run in PHP 5.6.16 在PHP 5.6.16中运行

PHP 5引用计数

Run in PHP 7.0.2 在PHP 7.0.2中运行

PHP 7的refcount

I think the result (PHP 7) should be: 我认为结果(PHP 7)应该是:

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's - PHP 7 – What changed internally? Nikita Popov的-PHP 7 –内部发生了什么变化? (P41) (P41)

http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally http://www.slideshare.net/nikita_ppv/php-7-what-c​​hanged-internally

幻灯片共享

In PHP 7 a zval can be reference counted or not. 在PHP 7中,可以参考计数zval或不参考zval。 There is a flag in the zval structure which determined this. zval结构中有一个标志决定了这一点。

There are some types which are never refcounted. 有些类型永远不会被引用。 These types are null, bool, int and double. 这些类型为null,bool,int和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. 这些是字符串和数组。

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. 如果您通常使用的是NTS(非线程安全)PHP 7构建,则代码中的所有字符串文字都会被保留。 These interned strings are deduplicated (ie 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). 如果您使用opcache,这些字符串将存在于共享内存中,在这种情况下,您将无法对其使用引用计数(因为我们的引用计数机制是非原子的)。 Interned strings have a dummy refcount of 1, which is what you're seeing here. 插入的字符串的虚拟引用计数为1,这就是您在此处看到的。

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. 如果使用opcache,则代码中的常量数组文字将转换为不可变数组。 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. 不可变数组的虚拟引用计数为2,因为它允许我们优化某些分离路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM