Original: http://www.huamanshu.com/blog/2014-05-18.html
Origin
Copy the English name is called "Copy-on-write", referred to as "cow", also known as "rely on"
Today checked the "cow", originally originated in the *nix memory management mechanism, and later widely used in other development languages, C + + STL, and PHP, I believe that many people understand this copy, often with others kan very happy.
$foo = ‘foo‘; $bar = $foo;
No
When you first write such PHP code, it is often asked, this will not be copied because of the use of more memory, next to someone said, No. Of course, you do not know what he said "No", refers to "do not occupy", or "I do not know." Later, after learning about memory_get_usage() This function, personally testing the validation, did not exponentially increase.
<?php var_dump(memory_get_usage()); $foo = ‘foooooooooooooooooooooooooooooo‘; var_dump(memory_get_usage()); $bar = $foo; var_dump(memory_get_usage()); ------ 结果 ------- int(121704) int(121840) // 与前者相差 echo $((840-704)) = 136 int(121888) // 与前者相差 echo $((888-840)) = 48
Xdebug Glimpse
The magic is that no matter $foo how big the variable, $foo when copied to $bar , memory consumption only increased by 48, what is the 48 thing? This time, you have to ask Xdebug to understand what happened.
<?php $foo = ‘foo‘; xdebug_debug_zval(‘foo‘); $bar = $foo; xdebug_debug_zval(‘foo‘); xdebug_debug_zval(‘bar‘); $bar = ‘bar‘; xdebug_debug_zval(‘foo‘); xdebug_debug_zval(‘bar‘); ------ 结果 ------- foo: (refcount=1, is_ref=0)=‘foo‘ // copy foo before foo: (refcount=2, is_ref=0)=‘foo‘ // copy foo after bar: (refcount=2, is_ref=0)=‘foo‘ // write bar befor foo: (refcount=1, is_ref=0)=‘foo‘ // write bar after bar: (refcount=1, is_ref=0)=‘bar‘ // write bar after
Note that the value of RefCount is changed at copy foo and write bar, which is the PHP write-time copy. Well, as I said, I told my high school teacher that this is just as boring as the law of conservation of energy.
PHP Variable Structure body
In fact, refcount in the PHP kernel, is a member of the structure of the variable storage, the full name refcount__gc , the above xdebug print out the Is_ref is also a member of the variable structure is_ref__gc , view its full definition can view the PHP source code zend/zend.h
struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount__gc; zend_uchar type; /* active type */ zend_uchar is_ref__gc; };
refcount__gcIs the record variable reference number, when $bar = $oo , when the refcount of Foo plus one, because bar and Foo is the same point to a variable, the reference count is the same as 2. When the bar value is overwritten, a write-time copy occurs, and the inner block pointed to by the struct cannot support two values at the same time, so the kernel makes a copy of Foo out to bar and sets its value to bar. is_ref__gcis used to identify whether it is a reference, which means it comes in handy when we reference the variable.
Let's start by looking at how the PHP kernel is created to create a variable for us, copy variables and reference variables. PHP is a weakly typed variable language, there is no type, but PHP is written by C, C type definition and use have strict requirements.
Creating variables
<?php $foo = ‘FOO‘ // PHP内核创建$foo变量 zval *foovar; MAKE_STD_ZVAL(foovar); // 内核专门封装为变量申请内存空间的宏 ZVAL_STRING(fooval, "FOO", 1); // type => ZVAL_STRING, value => "FOO" ZEND_SET_SYMBOL(EG(active_symbol_table), "foo", foovar); // 把$foo加入当前作用域的符号表中,使之在后面可以在symbo_table中可以检索出$foo,也就是PHP代码中要使用$foo的时候。
Now it seems that we have simply written a $foo = ‘FOO‘; , the kernel is to give us the bottom of the matter, we do not have to worry about how much memory should be allocated, but also to release, PHP is not very human, let PHP developers easy to get started to see the world of PHP.
Copy variables
<?php $foo = ‘FOO‘; $bar = $foo; // PHP内核创建$foo变量 zval *foovar; MAKE_STD_ZVAL(foovar); ZVAL_STRING(fooval, "FOO", 1); // type => ZVAL_STRING, value => "FOO" ZEND_SET_SYMBOL(EG(active_symbol_table), "foo", foovar); // ZVAL_ADDREF(foovar); //显式的增加了foovar结构体的refcount__gc zend_hash_add(EG(active_symbol_table), "bar", sizeof("bar"),&foovar, sizeof(zval*), NULL);
Reference variable
<?php $foo = ‘FOO‘; $bar = &$foo; // PHP内核创建$foo变量 zval *foovar; MAKE_STD_ZVAL(foovar); ZVAL_STRING(fooval, "FOO", 1); // type => ZVAL_STRING, value => "FOO" zend_hash_add(EG(active_symbol_table), "bar", sizeof("bar"),&foovar, sizeof(zval*), NULL); zend_hash_add(EG(active_symbol_table), "bar", sizeof("bar"),&foovar, sizeof(zval*), NULL); // $bar、$foo同指向一个内在地址
Copy, reference multiple combinations
<?php $foo = ‘foo‘; xdebug_debug_zval(‘foo‘); $bar = $foo; xdebug_debug_zval(‘foo‘); xdebug_debug_zval(‘bar‘); // 增加一个引用 $hua = &$foo; xdebug_debug_zval(‘foo‘); xdebug_debug_zval(‘bar‘); xdebug_debug_zval(‘hua‘); ------ 结果 ------- 1.foo: (refcount=1, is_ref=0)=‘foo‘ // copy foo befor 2.foo: (refcount=2, is_ref=0)=‘foo‘ // copy foo after 3.bar: (refcount=2, is_ref=0)=‘foo‘ 4.foo: (refcount=2, is_ref=1)=‘foo‘ // ref foo after 5.bar: (refcount=1, is_ref=0)=‘foo‘ 6.hua: (refcount=2, is_ref=1)=‘foo‘
All know when quoting, change foo,? Hua both values, and the other variable changes. summed up is
- When only copy, refcount++ (see result 2)
- When only references are made, Is_ref will be rewritten from 0 to 1.
- When a variable is both copied and referenced, the reference is consistent with the referenced refcount, is_ref, copy variable refcount=1, is_ref=0 (see result 4-6)
Copy and reference multiple combinations in writing
<?php $foo[‘hua‘] = ‘man‘; $bar = &$foo[‘hua‘]; $huamanshu = $foo; $huamanshu[‘hua‘] = ‘shu‘; echo $foo[‘hua‘];
Guess what the result is? All said is the abnormal version, how to follow common sense to think about the answer? The correct answer is: ' Shu '. This $huamanshu isn't about a $foo half-penny relationship with us, is it?
<?php $foo [' hua '] = ' man '; Xdebug_debug_zval (' foo '); $bar = & $foo [' Hua ']; Xdebug_debug_zval (' foo '); x Debug_debug_zval (' Bar '); $huamanshu = $foo; Xdebug_debug_zval (' foo '); Xdebug_debug_zval (' Huamanshu '); $huamanshu [' hua '] = ' shu '; Xdebug_debug_zval (' foo '); Xdebug_debug_zval (' Huamanshu '); Xdebug_debug_zval (' Bar '); $ PHP y.php foo: (Refcount=1, is_ref=0) =array (' Hua ' = (refcount=1, is_ref=0) = ' man ') foo: (Refcount=1, is_ref=0) =arra Y (' hua ' = (refcount=2, is_ref=1) = ' man ') Bar: (refcount=2, is_ref=1) = ' Mans ' foo: (refcount=2, is_ref=0) =array (' Hua ' =& Gt (refcount=2, is_ref=1) = ' man ') Huamanshu: (refcount=2, is_ref=0) =array (' Hua ' = (refcount=2, is_ref=1) = ' man ') Foo: ( Refcount=1, is_ref=0) =array (' Hua ' = (refcount=3, is_ref=1) = ' shu ') Huamanshu: (Refcount=1, is_ref=0) =array (' Hua ' =& Gt (refcount=3, is_ref=1) = ' shu ') Bar: (refcount=3, is_ref=1) = ' shu '
The value of a person's feeling $foo[‘hua‘] refcount $huamanshu[‘hua‘] = ‘shu‘; should be changed from 2 to 0 instead of adding one.
Of course, there will be a lot of students will be guilty of such a problem:
$array = [ 1, 2, 3, 4 ]; foreach ($array as &$value) {} echo implode(‘,‘, $array), PHP_EOL; // unset($value); 解决办法之一 foreach ($array as $value) {} echo implode(‘,‘, $array), PHP_EOL; // echo 1,2,3,4 1,2,3,3
Because the reference remains after the first one, the foreach $value value is $value changed, which is $value $array the last element value, when it is involved. Each time the second one is foreach changing the $value value, just to the third time to rewrite the $value value, the fourth time just repeat the third time the value of the overlay. Therefore, in the optimization of PHP code, foreach最好写不一样的 $v ', or ' $item ' such names, to avoid the above by reference and lead to rewriting, it is not possible to use unset off the citation count.
In fact, PHP is very clever, of course every language is, especially C, well deserved! The PHP kernel also specifically defines 8 types to support all the types we create in PHP code, which makes us lazy. Reading this list of types will have a deeper understanding of the PHP kernel implementation.
- Is_null
- The first use of the variable if not initialized, it will be automatically assigned to this constant, of course, we can also in the PHP language through the null constant to give the variable null type value.
- The value of this type is only one, which is null, and 0 is different from false.
- Is_bool
- A variable of type Boolean has a value of two, true or false.
- In the PHP language, the while, if, and other statements automatically turn the value of the expression into this type.
- Is_long
- The integer type in the PHP language is represented in the kernel by the signed Long data type of the operating system in which it resides. In the most common 32-bit operating system, it can store any integer from 2147483648 to +2147483647 in the range
- If the integer variable in the PHP language exceeds the maximum or minimum value, it does not overflow directly, but instead is converted to a value of type is_double by the kernel and then participates in the calculation
- Because signed long is used as a vector, this also explains why the integer data in the PHP language is signed.
- Example: $a = 2147483647; $a + +; The echo $a;//will output 2147483648 correctly;
- Is_double
- Floating-point data in PHP is stored through the signed double variable in the C language, which ultimately depends on the floating-point implementation of the operating system in which it resides.
- As a program ape, we should know that the computer is not accurate representation of floating-point numbers, but the use of scientific notation to save a certain precision floating point number, the computer to handle floating point is a nightmare.
- Is_string
- The most commonly used data type in PHP--a string, in-memory storage and C, is a piece of memory that can drop all the characters in the variable, and in the Zval implementation of the variable, a pointer to the memory is saved.
- Unlike C, the PHP kernel also holds the actual length of the string in the Zval structure, which allows PHP to embed the ' character ' in the string, and also makes the PHP string binary safe and can safely store binary data! In the plain living style, the kernel only requests its length +1 memory for the string, the last byte is the ' character ', so we can use it as usual in the C language without the need for a binary security operation.
- Is_array
- An array is a very special type of data, and its only function is to aggregate other variables. In the C language, an array can only host one type of data, and the PHP language array is much more flexible, it can host any type of data, all of which is Hashtable credit.
- The elements in each hashtable are made up of two parts: the index and the value, each of which is a separate zval (that is, a pointer to a zval, exactly).
- Is_object
- and arrays, objects are also used to store composite data, but unlike arrays, objects need to hold the following information: methods, access permissions, class constants, and other processing logic.
- Is_resource
- Some of the content of the data may not be directly presented to the PHP user, such as a link to a MySQL server, or the direct rendering is meaningless. But the user also needs this kind of data, so PHP provides a data type called resource (Resource).
8 types quoted from www.walu.cc
Original: http://www.huamanshu.com/blog/2014-05-18.html
PHP Write-time replication