Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 refcount

Run in PHP 7.0.2

php 7 refcount

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

slideshare

like image 416
Lynn Avatar asked Jan 13 '16 10:01

Lynn


1 Answers

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.

like image 136
NikiC Avatar answered Oct 23 '22 08:10

NikiC