Introduction to Copy On Write in PHP_PHP Tutorial
Before we start, we can look at a simple piece of code:
$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:
$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:
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? :
//$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:
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 codearray_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:
$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
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:
========== ==============================
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:
$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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
