PHP variable write-time replication mechanism

Source: Internet
Author: User

This article mainly introduces the use of PHP variables to write the copy mechanism, has a certain reference value, now share to everyone, the need for friends can refer to

Reprinted from LYC's blog

Although the programming thought can be shared, but the difference between the language is more obvious, but the user is not aware of themselves, and understand the differences in the program and grasp the performance is still good. Here we introduce a very important mechanism of PHP copy on write, we first introduced the simplest variable to introduce this mechanism, before saying this, I first introduce how the weak type is implemented.

As we all know, PHP is implemented by C, but C is a strongly typed language, PHP How to do weakly type language. Let's look at the code in the PHP variable at the bottom of the C language,

typedef struct _ZVAL_STRUCT zval;typedef unsigned int zend_uint;ypedef unsigned char zend_uchar;struct _zval_struct {
  
   zvalue_value value;      /* Note here that this is the value of the variable * *    zend_uint refcount__gc;  /* Reference count */    Zend_uchar type;        /* Variable Current data type */    Zend_uchar is_ref__gc;   /* variable refers to */};typedef Union _zvalue_value {    long lval;      Value of integral type in/*php */    double dval;    /*php floating-point value */    struct {             char *val;        int len;    } STR;               /*php String */    HashTable *ht;     /* Array */    zend_object_value obj;  /* Object */} Zvalue_value;
  

I added some comments, we can find that, in fact, we used in PHP variables, the lower layer is a struct zval, the inside of the zvalue_value structure is actually a consortium, this union is actually stored in PHP variable value, Here we use the actual PHP code example to represent the entire work process, note the above reference count. First of all, the C language, first the non-functional part, the function part of the next section

int i = 4;  Alloca mode allocates space in memory, this variable is in the memory of the stack area int j = i;   The Alloca method allocates space in memory and copies the data from the original memory space into the new memory space, which is in the memory's stack area int j = 5;  Do not allocate memory space, modify the data of the stack space where the variable J resides

Look at the PHP section.

$i = 4;   The kernel creates a zval pointer and opens up space for it in heaps, with the pointer pointing to the space, the member reference count in Zval to 1, the type marked as shaping, and the request for a zvalue_value pointer, which also opens up space in a heap. The lval in the Union is assigned a value of 4, and the mapping of variables I and Zval pointers is recorded in the symbal_table hash table $j = $i;   There is no memory space in the application, the reference count in the member of Zval is marked as 2$j = 5;   The kernel re-creates the Zval pointer, repeating the above steps, I will not repeat the instructions, the focus is to mark the old Zval reference count as 1

To find a few important points from this place.

    1. All PHP variables open up the memory space in the heap, whether it is a temporary variable or a global variable, just a temporary variable of PHP is recorded in the Active_symbal_table table, the global variable record in the Symbal_table table

    2. Why is PHP slower than c? do so much more, can not slow it?

    3. When PHP is similar

      I this variable assignment, there is no memory overhead, that is, you assign a value of tens of thousands of, but also only the reference count becomes tens of thousands of, this and the C language is not the same. And when the value of a variable changes, the memory space will be re-opened, this mechanism we call the write-time replication mechanism

In the extra detail section, when the PHP kernel discovers that an int's value overflows, that is, when the range of integers is exceeded, it is automatically converted to float, and interested readers can write a large integer on their own, but not beyond the float value range to see what the Var_dump data type is.

The last part: The PHP object part because the default is the reference way, so is the assignment, then changes the object's member variable, also does not enable the write-time copy, as follows

Class Test {public     $var = 999;} $test 1 = new Test (), $test 2 = $test 1;  Just the reference count plus 1, without opening up new memory space $test2->var = 1000;echo $test 1->var;  The value at this time is also 1000$TEST3 = Clone $test 1;  This is the new memory space being re-opened.

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.