Home > Backend Development > PHP Tutorial > Introduction to Copy On Write in PHP_PHP Tutorial

Introduction to Copy On Write in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:29:48
Original
1003 people have browsed it

Before we start, we can look at a simple piece of code:

Copy code The code is as follows:

$foo = 1;
$bar = $foo;
echo $foo + $bar;
?>

Executing this code will print out the number 2. From a memory perspective, this code "may" be executed like this: allocate a piece of memory to the foo variable, store a 1 in it; allocate a piece of memory to the bar variable, also store a 1, and finally calculate the result output. In fact, we found that because the foo and bar variables have the same value, they can use the same memory. In this way, the memory usage is saved by 1, and the calculation overhead of allocating memory and managing memory addresses is also eliminated. That's right, many systems involving memory management have implemented this same-value shared memory strategy: copy-on-write

Many times, we will have an incomprehensible fear of concepts because of some terminology, but in fact, their basic principles are often very simple. This section will introduce the implementation of the copy-on-write strategy in PHP:

Copy on Write (also abbreviated as COW) has many application scenarios. For example, the optimization of memory usage in process copy in Linux can be found in various programming languages, such as C++’s STL, etc. Similar applications. COW is a commonly used optimization method and can be classified into: delayed allocation of resources. Resources are only occupied when they are actually needed. Copy-on-write can usually reduce resource usage.

Note: To save space, COW will be used to mean "copy on write" below;

Deferred optimization of memory copy

As mentioned before, COW in PHP can be simply described as: when assigning a value to a variable, it will not apply for new memory to store the value saved by the new variable, but simply share the memory through a counter. , only when the value of the variable pointed to by one of the references changes, new space is allocated to save the value content to reduce memory usage. In many scenarios, PHP uses COW to optimize memory. For example: multiple assignments of variables, passing function parameters, and modifying actual parameters in the function body, etc.

Let’s look at an example of viewing memory, which will make it easier to see the obvious role of COW in memory usage optimization:

Copy code The code is as follows:

$j = 1;
var_dump(memory_get_usage());

$tipi = array_fill(0, 100000, 'php-internal');
var_dump(memory_get_usage());

$tipi_copy = $ tipi;
var_dump(memory_get_usage());

foreach($tipi_copy as $i){
$j += count($i);
}
var_dump(memory_get_usage ());

//-----Execution result-----
$ php t.php
int(630904)
int(10479840)
int (10479944)
int(10480040)

The above code typically highlights the role of COW. When the array variable $tipi is assigned to $tipi_copy, the memory usage does not immediately increase by half, and there is no significant change when the array variable $tipi_copy is looped through. Here, the data of $tipi_copy and $tipi variables both point to the same memory without copying.

That is to say, even if we do not use references, after a variable is assigned a value, as long as we do not change the value of the variable, no new memory will be allocated to store the data. Based on this, we can easily think of some scenarios where COW can very effectively control memory usage: variables are only used for calculations and rarely modified, such as the transfer of function parameters, the copying of large arrays, etc., and no changes are required. variable value.

Copy separated changed values

Multiple variables with the same value sharing the same memory does save memory space, but the value of the variable will change. If in the above example, the value pointing to the same memory changes (or may change) , it is necessary to "separate" the changed values. This "separation" operation is "copying".

In PHP, in order to distinguish whether the same zval address is shared by multiple variables, the Zend engine introduces two variables, ref_count and is_ref, for identification:

Copy code The code is as follows:

ref_count and is_ref are defined in the zval structure (see Chapter 1, Section 1)
is_ref identifies whether the user uses & as a mandatory reference;
ref_count is a reference count, used to identify How many variables are referenced by this zval, that is, the automatic reference of COW, it will be destroyed when it is 0;
For more information about these two variables, jump to read: Chapter 3, Section 6: Assignment and Destruction of Variables realization.
Note: It can be seen that there is no difference in PHP's use of memory between $a=$b; and $a=&$b; (when the value does not change);

Let’s slightly change Example 2: What will happen if the value of $copy changes? :

Copy code The code is as follows:

//$tipi = array_fill( 0, 3, 'php-internal');
//array_fill is no longer used here to fill, why?
$tipi[0] = 'php-internal';
$tipi[1] = 'php-internal';
$tipi[2] = 'php-internal';
var_dump( memory_get_usage());

$copy = $tipi;
xdebug_debug_zval('tipi', 'copy');
var_dump(memory_get_usage());

$copy[ 0] = 'php-internal';
xdebug_debug_zval('tipi', 'copy');
var_dump(memory_get_usage());

//-----Execution result-- ---
$ php t.php
int(629384)
tipi: (refcount=2, is_ref=0)=array (0 => (refcount=1, is_ref=0)=' php-internal',
1 => (refcount=1, is_ref=0)='php-internal',
2 =&g t; (refcount=1, is_ref=0)='php-internal' )
copy: (refcount=2, is_ref=0)=array (0 => (refcount=1, is_ref=0)='php-internal',
is_ref=0)='php-internal',
                                                                                                                                                                                                                                        . , is_ref=0)=array (0 => (refcount=1, is_ref=0)='php-internal',
, 2 = & gt; (Refcount = 2, is_ref = 0) = 'php-internaly')
Copy: (Refcount = 1, is_ref = 0) = Array (0 = & gt; is_ref=0)='php-internal',
                                                  2 => (refcount=2, is_ref=0) ='php-internal')
int(630088)



In this example, we can find the following features:
$copy = $tipi; This basic assignment operation will trigger COW's memory "sharing" and will not produce memory copy;

The granularity of COW is the zval structure. All variables in PHP are based on zval, so the scope of COW is all variables. For collections composed of zval structures (such as arrays and objects, etc.), when the memory needs to be copied, Break down complex objects into the smallest granularity for processing. This allows you to modify a certain part of a complex object in memory without having to "separately copy" all the elements of the object into a memory copy;

Copy code

The code is as follows:

array_fill() also uses the COW strategy when filling the array, which may affect the demonstration of this example. Interested readers can read: PHP_FUNCTION(array_fill) in $PHP_SRC/ext/standard/array.c accomplish.

xdebug_debug_zval() is a function in the xdebug extension, used to output the reference information of variables inside zend. If you do not have the xdebug extension installed, you can use debug_zval_dump() instead. Reference: http://www.php.net/manual/zh/function.debug-zval-dump.php

Implement copy-on-write

After reading the above three examples, I believe you can also understand the implementation principle of COW in PHP: COW in PHP is implemented based on reference counting ref_count and is_ref. If there is an additional variable pointer, ref_count will be increased by 1, and vice versa. 1, it will be destroyed when it is reduced to 0; similarly, if there is one more mandatory reference &, is_ref will be increased by 1, otherwise it will be reduced by 1.

Here is a typical example:

Copy code The code is as follows:

$foo = 1;
xdebug_debug_zval('foo');
$bar = $foo;
xdebug_debug_zval('foo');
$bar = 2;
xdebug_debug_zval('foo'); ;
//-----Execution Result-----
foo: (refcount=1, is_ref=0)=1
foo: (refcount=2, is_ref=0)=1
foo: (refcount=1, is_ref=0)=1

After the previous introduction to the variable chapter, we know that when $foo is assigned a value, the value of the $foo variable is only pointed to by the $foo variable. When the value of $foo is assigned to $bar, PHP does not copy the memory to $bar, but points $foo and $bar to the same address. At the same time, the reference count increases by 1, which is the new 2. Subsequently, we change the value of $bar. At this time, if the memory pointed to by the $bar variable is directly needed, the value of $foo will also change accordingly. This is not the result we want. Therefore, the PHP kernel copies the memory and updates its value to the assigned value: 2 (this operation is also called a variable separation operation). At the same time, the memory pointed to by the original $foo variable is only pointed to by $foo, so the reference count is updated to :refcount=1.

It seems simple, but due to the existence of the & operator, the actual situation is much more complicated. See example below:



Figure 6.6 Memory copy separation caused by & operator>
From this example, we can see a problem-prone handling of the & operator in PHP: when $beauty=&$pan;, both variables essentially become reference types, resulting in seemingly ordinary variables. $pan, behaves the same as &$pan in some internal processing, especially when using reference variables within array elements, which can easily cause problems. (see last example)

Most of PHP’s work is text processing, and variables are carriers. The use of different types of variables runs through the life cycle of PHP. The COW strategy of variables also reflects the Zend engine’s processing of variables and their memory. Specifically You can refer to the source code file related content:

Copy code The code is as follows:
Zend/zend_execute.c
========== ==============================
zend_assign_to_variable_reference();
zend_assign_to_variable();
zend_assign_to_object() ;
zend_assign_to_variable();

//and the use of the following macro definitions
Zend/zend.h
================== =======================
#define Z_REFCOUNT(z) Z_REFCOUNT_P(&(z))
#define Z_SET_REFCOUNT(z, rc) Z_SET_REFCOUNT_P(&(z), rc)
#define Z_ADDREF(z) Z_ADDREF_P(&(z))
#define Z_DELREF(z) Z_DELREF_P(&(z))
#define Z_ISREF(z) Z_ISREF_P(&(z))
#define Z_SET_ISREF(z) Z_SET_ISREF_P(&(z))
#define Z_UNSET_ISREF(z) Z_UNSET_ISREF_P(&(z)) #define Z_SET_ISRE F_TO(z, isref) Z_SET_ISREF_TO_P(&(z), isref)

Finally, please use quotes with caution&

References and the reference counts of variables mentioned earlier are not the same thing as references in PHP. References are similar to pointers in C language. They can access the same content through different identifiers, but in PHP References are just simple variable aliases, without the flexibility and restrictions of C instructions.

There are a lot of unexpected behaviors in PHP. Some of them choose not to fix them for the time being due to historical reasons that cannot destroy compatibility, or some have relatively few usage scenarios. In PHP, you can only try to avoid these traps. Take for example the following example.

Since the reference operator will lead to the optimization of PHP's COW strategy, using references requires a clear understanding of the behavior of references to avoid misuse and avoid some difficult-to-understand bugs. If you think you know enough about references in PHP, try explaining this example:

Copy code The code is as follows:

$foo['love'] = 1;
$bar = &$foo['love'];
$tipi = $foo;
$tipi['love'] = '2';
echo $foo['love'];

This example will output 2 in the end. Everyone will be very surprised at how $tipi affects the reference operation of $foo and $bar variables, turning $foo['love'] pollution into a reference, so Zend does not pollute $ The modification of tipi['love'] produces a copy separation of memory.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769673.htmlTechArticleBefore we start, we can look at a simple code: Copy the code as follows: ?php //Example 1 $foo = 1; $bar = $foo; echo $foo + $bar; ? Executing this code will print out the number...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template