PHP Bottom-up analysis: On-write replication Cow cow's plural win color forum ww7349cow Japan Cow Milk Stone

Source: Internet
Author: User
To get to the ceiling of technology, the learning process lies in knowing it and knowing why.

Today, let's talk about PHP's underlying write-time replication (also called split at write time).

First, let's take a look at some code:

The output of the two pieces of code I'm sure you all know, but let's talk about what's going on here today.

Is the structure of the PHP storage variable (for ease of interpretation has written comments), zend.h in the Zend directory.

As you can see, the struct stores information about variable values, several variables pointing to the struct, the type of the variable, and whether it is a reference variable.

So what happened to the first print? The information for the variable enters a struct, related to the following:

$name = ' 8:30 P.M. ';
$myName = $name;

At this time $name and $myname share a structure, REFCOUNT__GC is 2,

We find that $myName = $name; This process does not actively become a two structure (this is also a PHP internal implementation of the optimization, only one structure, saving memory).

Then when the code runs to $myName = ' gzchen '; , how does the structure change? Since the first output is a two-variable shared struct, will changing one of the variables at this point cause two values to change together? Purely from the logic of the structure, it is possible, after all, we share the structure of the body.

So let's see how the second print is going, and the changes are as follows:

And did not according to what we think will $name and $myname at the same time to change to ' Gzchen ', but to copy more than a structure, two structures corresponding to the $name and $myname respectively.

This is the write-time copy (Copy-on-write,cow) in the mischief, he did not at $myname = $name; When assigning a value, it splits into two structures, but it happens when we rewrite one of the variables, which is a slow copy (also known as slow splitting).

The pseudo code is as follows:

Let's look at another piece of code:

The output is ' B ', what happened midway?

In fact, the foreach traversal process, not directly manipulate $arr (the original array), but will be copied $arr a $arrcopy (actually a copy, I here to replace the $arrcopy), foreach in the process of operation is actually $arrcopy, The approximate process is this:

And the example above is actually a truth, we can see that the beginning ($arr = $arrcopy) or a common structure, but $arr[$k] = $v again assigned, the copy occurs when the write, the structure is split.

Then the previous said that the foreach operation is $arrcopy, so the $ARR structure pointer is stuck in the first position (because the structure is different, $arrcopy can not be synchronized to $arr assignment).

In fact, this kind of technology is usually only used in the interview, the daily development will use this type of person after all is still a few, temporarily do not understand the friends do not care too much, as long as they know there is "copy when writing" This situation appears on the line.

The above describes the PHP bottom-up analysis: On the copy cow, including the cow,php aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.