Confusion about PHP 7 refcount

半城伤御伤魂 提交于 2019-12-18 12:33:14

问题


<?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

Run in PHP 7.0.2

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's - PHP 7 – What changed internally? (P41)

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


回答1:


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

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.

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.

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.



来源:https://stackoverflow.com/questions/34764119/confusion-about-php-7-refcount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!