Home > Article > Backend Development > Reference detailed explanation of PHP7 kernel
Problem
Reference (REFERENCE) was a flag in PHP5, but after PHP7 we turned it into a New type: IS_REFERNCE. However, references are a very common application, so this change has brought a lot of changes, and it has also caused us to accidentally forget to handle this type when developing PHP7, which has caused many problems. There are a lot of bugs.
The simplest case is when dealing with various types. From now on, we have to consider this new type more. For example, in PHP7, this code form becomes very confusing. Common:
try_again:swtich (Z_TYPE_P(zv)) { case IS_TRING: break; case IS_ARRAY: break; ... case IS_REFERENCE: zv = Z_REFVAL_P(zv); // Dereference goto try_again; break ;}
If you write your own extensions and forget to consider this new type, it will cause problems.
Why?
So since this new type will bring so many problems, why did we use it to turn the reference into a type? Why not just use a flag?
In a word, it’s us I have to do this. -_
#As mentioned earlier, Hashtable directly stores zval, so in the symbol table, how can two zval share a value? It is okay for complex types such as strings, we seem to You can add a flag bit to the zend_refcounted structure to indicate that it is a reference to solve the problem. However, this will also encounter copying caused by Change On Write, but we know that in PHP7, some types are directly stored in zval, such as IS_LONG, But reference types require reference counting, so how to express a zval that is IS_LONG and IS_REFERNCE?
For this reason, we created this new type:
As shown in the figure, Reference is a new type: zend_reference. For zval of IS_REFERNCE type, zval.value.ref is a pointer to zend_reference, which contains a reference count and a zval. The specific value of zval is stored in zval.value.ref- >val in .
So for the reference of IS_LONG, use a zval of type IS_REFERNCE, which points to a zend_reference, and this zend_reference->val is a zval of type IS_LONG.
Change On Write
PHP uses reference counting to do simple garbage collection. Consider the following code:
<?php1. $val = "laruence";2. $ref = &$val;3. $copy = $val;?>
$ref and $val point to the same For the reference of zval, in PHP5, we represented this situation through a reference count of 2 and a reference flag of 1. When copying $val to $copy(line 3), we found that $val is A reference with a count greater than 1, so a Change on write, that is, separation, is required. So we need to copy this zval.
In PHP7, the situation becomes much simpler. First, assign the reference to $ref(line 2), an IS_REFERNCE type is generated, and because there are two variables referencing it at this time, the reference count of the zend_reference structure zval.value.ref->gc.refcount is 2.
Then it is subsequently assigned to $ When copying (line 3), it is found that $val is a reference, so $copy points to zval.value.ref->val, which is the zval whose string value is laruence, and then counts the reference of zval by 1. That is, zval.value.ref->val.value.str.gc.refcount is 2. No copying occurs.
This effectively solves the classic problem of PHP5 mentioned in the previous chapter. , For example, when we run the problem in the previous chapter under PHP7, the result we get is:
$ php-7.0/sapi/cli/php /tmp/1.phpUsed 0.00021380008539Used 0.00020173048281
It can be seen that no copying occurs, so there will be no performance problems.
Recommended tutorial :《PHP》
The above is the detailed content of Reference detailed explanation of PHP7 kernel. For more information, please follow other related articles on the PHP Chinese website!