Share the code instance of CopyOnWrite in PHP

Source: Internet
Author: User
First, let's look at the assignment and reference questions in PHP. What is your answer to the question? Before today, my answer is to allocate memory space for B. Because I understand this as follows: & amp; assignment Problem introduction

First, let's take a look at the assignment and reference problems in PHP.

 

What is your answer to the question in the middle? Before today, my answer is to allocate memory space for B. Because I understand it as follows:
& When assigning values, an alias is defined as a variable and a reference to memory space is added. Changing one of them will affect other references. When unset () is used, the reference to the variable memory space is disconnected, and the memory space is not released.
While the value = is different, it will re-open a copy of the memory space to store the original variable. Modifications between the two will not affect each other.

The following program confirms this point:

 

If

$ B = $ a; // after that, neither a nor B changes and remains consistent.

There is such a problem. If = is assigned, neither of the two variables has changed. if there are two copies, isn't it a waste of memory?
PHP actually avoids this situation.
When a variable is assigned to a new variable in PHP, the memory space is not allocated to the new variable immediately, but the reference to the memory space is added. When the original or new variables change, a memory space is allocated for the new variables.

 

Each php variable is stored in a variable container named "zval. A zval variable container not only contains the type and value of the variable, but also contains two bytes of additional information. The first is "is_ref", which is a bool value to identify whether the variable belongs to the reference set (referenceset ). With this byte, the php engine can distinguish between common variables and reference variables. because php allows users to use & to use custom references, the zval variable container also has an internal reference counting mechanism, to optimize memory usage. The second extra byte is "refcount", used to indicate the number of variables (also known as symbol) pointing to the zval variable container. When the value of "refcount" is 1, the value of "is_ref" is always FALSE.

After xdebug is installed, use xdebug_debug_zval () to view the zval structure:
As follows:

 

Output:

b:   (refcount=2, is_ref=0),   int    1b:   (refcount=1, is_ref=0),   int    1

From the above results, we can see that before a changes, the reference count is 2. after a changes, the reference count of B changes to 1 because B allocates space again.
The phenomenon described above is replication at write time.

Copy at write time

  Copy at write time(Copy-on-Write, Also abbreviated as COW), as the name suggests, is to copy a copy of the memory for modification only when writing. COW was first applied to the optimization of thread and memory usage in * nix systems, and is widely used in various programming languages, such as C ++ STL. In the PHP kernel, COW is also the main memory optimization method. In the previous discussions on variables and memory, the reference count plays a vital role in the destruction and collection of variables. The significance of reference counting is to make COW work properly and optimize memory usage.
   Advantages of replication during write: When a value is assigned to a variable, the system does not apply for a new memory to store the value saved by the new variable. Instead, it simply uses a counter to share the memory, you can apply for a new space to save the value content only when one of the references points to a variable value change to reduce memory usage.

  Based on the basic data structure of PHP

Ref_count and is_ref are defined in the zval struct;
Is_ref indicates whether the user uses & mandatory reference;
Ref_count is the reference count used to identify the number of variables that zval is referenced by, that is, the automatic reference that is copied during writing, and the value 0 is destroyed.

Problem introduction

First, let's take a look at the assignment and reference problems in PHP.

 

What is your answer to the question in the middle? Before today, my answer is to allocate memory space for B. Because I understand it as follows:
& When assigning values, an alias is defined as a variable and a reference to memory space is added. Changing one of them will affect other references. When unset () is used, the reference to the variable memory space is disconnected, and the memory space is not released.
While the value = is different, it will re-open a copy of the memory space to store the original variable. Modifications between the two will not affect each other.

The following program confirms this point:

 

If

$ B = $ a; // after that, neither a nor B changes and remains consistent.

There is such a problem. If = is assigned, neither of the two variables has changed. if there are two copies, isn't it a waste of memory?
PHP actually avoids this situation.
When a variable is assigned to a new variable in PHP, the memory space is not allocated to the new variable immediately, but the reference to the memory space is added. When the original or new variables change, a memory space is allocated for the new variables.

 

Each php variable is stored in a variable container named "zval. A zval variable container not only contains the type and value of the variable, but also contains two bytes of additional information. The first is "is_ref", which is a bool value to identify whether the variable belongs to the reference set (referenceset ). With this byte, the php engine can distinguish between common variables and reference variables. because php allows users to use & to use custom references, the zval variable container also has an internal reference counting mechanism, to optimize memory usage. The second extra byte is "refcount", used to indicate the number of variables (also known as symbol) pointing to the zval variable container. When the value of "refcount" is 1, the value of "is_ref" is always FALSE.

After xdebug is installed, use xdebug_debug_zval () to view the zval structure:
As follows:

 

Output:

b:   (refcount=2, is_ref=0),   int    1b:   (refcount=1, is_ref=0),   int    1

From the above results, we can see that before a changes, the reference count is 2. after a changes, the reference count of B changes to 1 because B allocates space again.
The phenomenon described above is replication at write time.

Copy at write time

  Copy at write time(Copy-on-Write, Also abbreviated as COW), as the name suggests, is to copy a copy of the memory for modification only when writing. COW was first applied to the optimization of thread and memory usage in * nix systems, and is widely used in various programming languages, such as C ++ STL. In the PHP kernel, COW is also the main memory optimization method. In the previous discussions on variables and memory, the reference count plays a vital role in the destruction and collection of variables. The significance of reference counting is to make COW work properly and optimize memory usage.
   Advantages of replication during write: When a value is assigned to a variable, the system does not apply for a new memory to store the value saved by the new variable. Instead, it simply uses a counter to share the memory, you can apply for a new space to save the value content only when one of the references points to a variable value change to reduce memory usage.

  Based on the basic data structure of PHP

Ref_count and is_ref are defined in the zval struct;
Is_ref indicates whether the user uses & mandatory reference;
Ref_count is the reference count used to identify the number of variables that zval is referenced by, that is, the automatic reference that is copied during writing, and the value 0 is destroyed.

The above is the details shared by the Copy On Write code instance in PHP. For more information, see other related articles in the first PHP community!

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.