Before we start, we can look at a simple piece of code:
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:
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:
Let’s slightly change Example 2: What will happen if the value of $copy changes? :
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 codeImplement 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:
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:
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:
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.