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

Introduction to Copy On Write in PHP_PHP Tutorial

Jul 13, 2016 am 10:29 AM

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

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.

What is the difference between include, require, include_once, require_once? What is the difference between include, require, include_once, require_once? Apr 05, 2025 am 12:07 AM

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

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.

See all articles