PHP Write-time copy

Source: Internet
Author: User
This article is mainly to share with you the PHP copy of the details, we start with an example, we hope to help everyone.

<?php$foo = 1; $bar = $foo; echo $foo + $bar;

Variable $foo assigned to the variable $bar, the two variables have the same value, there is no need to new request memory space, they can share the same block of memory. in many scenarios PHP's COW is optimized for memory. For example: multiple assignments of variables, function parameter transfer, and in the function body to modify the arguments and so on.

What is "copy"

This is a part of the blog from the bird's example, said more clearly, directly affixed to the.

<?php   $var = "Laruence";   $var _dup = $var;   $var = 1;? >

Obviously after this code execution, $var _dup value should still be "laruence", then how is this implemented? This is the copy on write mechanism of PHP:

PHP before modifying a variable, will first look at the refcount of this variable, if the RefCount is greater than 1,php will execute a separate routine, for the above code, when executing to the third row, PHP found that the $var point to the Zval refcount greater than 1, Then PHP will copy a new zval out, the original zval refcount minus 1, and modify the symbol_table, so that $var and $var _dup separation (separation). This mechanism is called copy on write (when copying).

Copy application scenarios on write

There are a lot of scenarios for copy on Write, also abbreviated as cow, for example, in Linux, which optimizes memory usage in process replication, and has similar applications in various programming languages such as STL for C + +, and so on. Cow is a commonly used optimization method that can be categorized as: Resource delay allocation. Resources are used only when resources are really needed, and write-time replication typically reduces resource usage.

An example that proves PHP COW optimized memory footprint:

<?php$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 ());

Operation Result:

$ PHP T. Phpint (630904) int (10479840) int (10479944) int (10480040)

Memory has not improved significantly.

"Copy on Write" principle

Multiple variables of the same value share the same block of memory does save memory space, but the value of the variable will change, if in the above example, point to the same memory value has changed (or may change), you need to change the value of "separate", the "separate" operation is "copy".

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

Ref_count and is_ref are defined in the ZVAL structure

Is_ref identifies whether the user uses & 's mandatory reference;

Ref_count is a reference count that identifies how many variable references to this zval, that is, the auto-reference of cow, which is destroyed when 0;

Note: This shows that $a = $b; $b with $a =&; There is no difference in the use of memory in PHP (value does not change);

I believe you can also understand the implementation of PHP COW principle: PHP COW based on the reference count Ref_count and is_ref implementation, more than one variable pointer, will ref_count plus 1, minus 1, minus 0 to destroy; Similarly, one more mandatory reference & , the is_ref will be added 1, and vice versa minus 1.

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.