Analysis of the principle of PHP variable separation/Reference (VariablesSeparation) _ PHP Tutorial

Source: Internet
Author: User
Analysis of the principle of PHP variable separation reference (VariablesSeparation ). First, let's review the structure of zval: Copy the code as follows: struct_zval_struct {* Variableinformation * zvalue_valuevalue; * value * zend_uintrefcount; zend_uchar first, let's review the structure of zval:

The code is as follows:


Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount;
Zend_uchar type;/* active type */
Zend_uchar is_ref;
};


We have never introduced the refcount and is_ref fields. we know that PHP is a long-running server-side script interpreter. So for it, efficiency and resource usage are a very important measure, that is, PHP must introduce the memory usage as much as possible, consider the following code:

The code is as follows:


$ Var = "laruence ";
$ Var_dup = $ var;
Unset ($ var );
?>


The first line of code creates a string variable, applies for a 9-byte memory, and saves the string "laruence" and the end of a NULL (/0.
The second row defines a new string variable and copies the var value to the new variable.
The third row unsets the variable var.
Such code is very common in our scripts. If PHP re-allocates the memory and copies the data for every variable assignment, therefore, the above code is required to apply for 18 bytes of memory space, and we can easily see that the above code does not need to apply for two copies of space at all, PHP developers also see:
As we have mentioned before, the variables in PHP are implemented by a symbol name stored in symbol_table corresponding to a zval. for example, for the first line of code above, A value "var" is stored in symbol_table, and a pointer pointing to a zval structure. the variable value "laruence" is stored in this zval, which is hard to imagine, for the above code, we can make the pointers corresponding to "var" and "var_dup" point to the same zval.
PHP does the same. in this case, we need to introduce the refcount field in the zval structure that we have never introduced before.
Refcount, as its name implies, records the number of referenced zval values.
For example, for the code:

The code is as follows:


$ Var = 1;
$ Var_dup = $ var;
?>


The first line creates an integer variable with a value of 1. In this case, the refcount of the zval that stores integer 1 is 1.
In the second row, a new integer variable is created. the variable also points to the created zval and adds the refcount of this zval to 1. at this time, the refcount of this zval is 2.
PHP provides a function to help us understand the debug_zval_dump process:

The code is as follows:


$ Var = 1;
Debug_zval_dump ($ var );
$ Var_dup = $ var;
Debug_zval_dump ($ var );
?>

Output:
Long (1) refcount (2)
Long (1) refcount (3


What if you are surprised that var's refcount should be 1?
We know that for simple variables, PHP uses parameters in the form of passing values. That is to say, when debug_zval_dump ($ var) is executed, $ var will be passed to debug_zval_dump by passing the value, that is, it will cause var refcount to add 1, so as long as we can see, after a variable is assigned to a variable, the fact that zval's refcount is added to 1 is sufficient.
Now let's look back at the code at the beginning of the article. what will happen after the last unset ($ var) line is executed? Yes, both the refcount minus 1, the code above:

The code is as follows:


$ Var = "laruence ";
$ Var_dup = $ var;
Unset ($ var );
Debug_zval_dump ($ var_dup );
?>

Output:
String (8) "laruence" refcount (2


But what about the following code?

The code is as follows:


$ Var = "laruence ";
$ Var_dup = $ var;
$ Var = 1;
?>


Obviously, after this code is executed, the value of $ var_dup should be "laruence". how can this be implemented?
This is the copy on write mechanism of PHP:
Before modifying a variable, PHP will first check the refcount of the variable. if refcount is greater than 1, PHP will execute a separation routine. for the above code, when the third line is executed, PHP finds that the refcount of zval pointed to by $ var is greater than 1, so PHP will copy a new zval and reduce the refcount of the original zval by 1, modify symbol_table to separate $ var from $ var_dup (Separation ). This mechanism is called copy on write (copy at write time ).
Code test:

The code is as follows:


$ Var = "laruence ";
$ Var_dup = $ var;
$ Var = 1;
Debug_zval_dump ($ var );
Debug_zval_dump ($ var_dup );
?>

Output:
Long (1) refcount (2)
String (8) "laruence" refcount (2


Now we know that when using variable replication, PHP does not actually replicate internally, but uses the same structure to save as much overhead as possible. So how can we implement the reference in PHP?

The code is as follows:


$ Var = "laruence ";
$ Var_ref = & $ var;
$ Var_ref = 1;
?>


After this code is complete, $ var is indirectly changed to 1. this process is called (change on write: change at write time ). So how does ZE know that Separation is not required for this replication?
In this case, the is_ref field in zval is used:
For the above code, after the second line is executed, the refcount of zval represented by $ var changes to 2 and is_ref is set to 1 at the same time.
To the third line, PHP first checks the is_ref field of zval represented by var_ref. if it is 1, it is not separated. the general logic is shown as follows:

The code is as follows:


If (* val)-> is_ref | (* val)-> refcount <2 ){
// Do not execute Separation
...; // Process
}


However, the problem arises again. what will happen to the following code?

The code is as follows:


$ Var = "laruence ";
$ Var_dup = $ var;
$ Var_ref = & $ var;
?>


For the above code, there is a pair of copy on write variables $ var and $ var_dup, and a pair of change on write mechanism variables for $ var and $ var_ref, how does this happen?
When the second line is executed, $ var_dup and $ var point to the same zval, and refcount is 2.
When the third line is executed, PHP finds that the refcount of zval to be operated is greater than 1, PHP will execute Separation and separate $ var_dup, and associate $ var and $ var_ref with change on write. That is, refcount = 2, is_ref = 1;
Based on this analysis, we can let debug_zval_dump the result with refcount 1:

The code is as follows:


$ Var = "laruence ";
$ Var_dup = & $ var;
Debug_zval_dump ($ var );
?>

Output:
String (8) "laruence" refcount (1


For detailed reasons, you only need to make a little analysis, and I will not go beyond the limit .;)
This time we introduced the PHP variable separation mechanism. next time I will continue to introduce how to receive and transmit parameters in the PHP script in the extension.

The compile code is as follows: struct _ zval_struct {/* Variable information */zvalue_value value;/* value */zend_uint refcount; zend_uchar...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.