Home > Backend Development > PHP Tutorial > Code example sharing of Copy On Write in PHP

Code example sharing of Copy On Write in PHP

黄舟
Release: 2023-03-06 21:44:01
Original
2414 people have browsed it

Introduction to the problem

First let’s take a look at the assignment and reference in PHPProblem

<?php$a = 10;//将常量值赋给变量,会为a分配内存空间 
$b = $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? 
$c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。?>
Copy after login
Copy after login

What is your answer to the middle question? Before today, my answer was that memory space would be allocated for b. Because this is what I understand:

When assigning a value, it is regarded as an alias defined for a variable, and a reference to the memory space is added. Changing one of them will affect the other references. When using unset(), the reference to the variable memory space is only disconnected, and the memory space will not be released.
The = assignment is different. It will re-open a memory space to store a copy of the original variable. Modifications between the two will not affect each other.

The following program confirms this:

<?php$a = 10;   //将常量值赋给变量,会为a分配内存空间 
$b = $a; //变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? 
$c = &$a; //引用是不会为c分配空间的,c和a是共用一份空间的。 
$a = 5;echo $c;   //输出5,因为a和c 是指向同一个内存空间echo PHP_EOL;
echo $b;   //由于b是副本,对a的操作不会影响b,输出10?>
Copy after login

Then if

$b = $a;//之后a  和  b 都不做任何改变,保持一致
Copy after login
Copy after login

There is such a problem, if = After the assignment, neither variable has changed. If Isn't it a waste of memory if there are two copies?

This situation is actually avoided in PHP.
When assigning a variable to a new variable in PHP, memory space will not be allocated immediately for the new variable, but a reference to the memory space will be added. When any changes are made to the original variable or the new variable, a memory space will be allocated for the new variable.

<?php$a = 1;$b = $a; 
echo $a;//在此之前,b都是和a共用内存空间的。 $a = 2;//a作出了改变,此时b才会有自己的空间?>
Copy after login
Copy after login

Each

php variable exists in a variable container called "zval". A zval variable container contains, in addition to the type and value of the variable, two bytes of additional information. The first one is "is_ref", which is a bool value used to identify whether this variable belongs to the reference set (referenceset). Through this byte, the PHP engine can distinguish ordinary variables from reference variables. Since PHP allows users to use custom references by using &, there is also an internal reference counting mechanism in the zval variable container to optimize memory usage. The second extra byte is "refcount", which is used to indicate the number of variables (also called symbols) pointing to this zval variable container. When the value of "refcount" is 1, the value of "is_ref" is always FALSE.

After installing xdebug, use xdebug_debug_zval(), you can see the zval structure:

As follows:

<?php
$a = 1;
$b = $a;
echo $a;//在此之前,b都是和a共用内存空间的。xdebug_debug_zval(&#39;b&#39;);
$a = 2;//a作出了改变,此时b才会有自己的空间xdebug_debug_zval(&#39;b&#39;);?>
Copy after login
Copy after login

Output:

b: 
  (refcount=2, is_ref=0), 
  int  
  1
b: 
  (refcount=1, is_ref=0), 
  int  
  1
Copy after login
Copy after login
As you can see from the above results, before a is changed, the reference count is 2. After a is changed, b's reference count becomes 1. It's because b reallocates space.

The phenomenon described above is copy-on-write.

Copy-on-Write

 

Copy-on-WriteCopy-on-Write, also Abbreviated as COW), as the name suggests, it actually copies a copy of the memory for modification when writing. COW was first used in *nix systems to optimize thread and memory usage, and was later widely used in various programming languages, such as C++'s STL, etc. In the PHP kernel, COW is also the main memory optimization method. In the previous discussion about variables and memory, reference counting plays a crucial identification role in the destruction and recycling of variables. The purpose of reference counting is to enable COW to operate normally, thereby achieving optimal use of memory.  
Advantages of copy-on-write: When assigning a value to a variable, it does not apply for new memory to store the value saved by the new variable. Instead, it simply shares the memory through a counter. Only when 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.

 

From the perspective of the underlying basic data structure of PHP

##ref_count and is_ref are defined in the zval
structure

; The is_ref identifier is whether the user uses & as a mandatory reference; Ref_count is a reference count, used to identify how many variables this zval is referenced, that is, an automatic reference copied when writing. When it is 0, it will be destroyed.

问题引入

  首先来看看PHP中的赋值与引用问题

<?php$a = 10;//将常量值赋给变量,会为a分配内存空间 
$b = $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? 
$c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。?>
Copy after login
Copy after login

  对于中间的那个问题,你的答案是什么呢?在今天之前,我的答案是会为b分配内存空间。因为我是这么理解的:
  &赋值的时候,视为一个变量定义了一个别名,增加了一个对内存空间的引用。改变其中一个,会影响其他的引用。而使用unset()时,只是断开了对变量内存空间的引用,内存空间不会释放。
  而 = 赋值则不同,它会重新开辟一份内存空间存储原变量的副本。两者之间的修改不会相互影响。

  而下面的程序则印证了这一点:

<?php
$a = 10;   //将常量值赋给变量,会为a分配内存空间 
$b = $a; //变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? 
$c = &$a; //引用是不会为c分配空间的,c和a是共用一份空间的。 
$a = 5;echo $c;   //输出5,因为a和c 是指向同一个内存空间echo PHP_EOL;
echo $b;   //由于b是副本,对a的操作不会影响b,输出10?>
Copy after login

  那如果

$b = $a;//之后a  和  b 都不做任何改变,保持一致
Copy after login
Copy after login

  有这么一个问题,如果 = 赋值之后,两个变量都不曾改变,如果是两份副本,岂不是太浪费内存?
  PHP中实际上避免了这种情况。
  PHP中将一个变量赋值给新变量时,不会立即为新变量分配内存空间,只是增加了对内存空间的引用。当原变量或者新变量作出任何改变时,才会为新变量 分配一块内存空间。

<?php$a = 1;$b = $a; 
echo $a;//在此之前,b都是和a共用内存空间的。 $a = 2;//a作出了改变,此时b才会有自己的空间?>
Copy after login
Copy after login

  每个php变量存在一个叫”zval”的变量容器中。一个zval变量容器,除了包含变量的类型和值,还包括两个字节的额外信息。第一个是”is_ref”,是个bool值,用来标识这个变量是否是属于引用集合(referenceset)。通过这个字节,php引擎才能把普通变量和引用变量区分开来,由于php允许用户通过使用&来使用自定义引用,zval变量容器中还有一个内部引用计数机制,来优化内存使用。第二个额外字节是”refcount”,用以表示指向这个zval变量容器的变量(也称符号即symbol)个数。当”refcount”的值是1时,”is_ref”的值总是FALSE.

  安装xdebug之后,利用xdebug_debug_zval(),可以看到zval结构:
  如下:

<?php
$a = 1;
$b = $a;
echo $a;//在此之前,b都是和a共用内存空间的。xdebug_debug_zval(&#39;b&#39;);
$a = 2;//a作出了改变,此时b才会有自己的空间xdebug_debug_zval(&#39;b&#39;);?>
Copy after login
Copy after login

 输出:

b: 
  (refcount=2, is_ref=0), 
  int  
  1
b: 
  (refcount=1, is_ref=0), 
  int  
  1
Copy after login
Copy after login

  由上面的结果可以看到,在a作出改变之前,引用计数是2 ,当a作出改变之后,b的引用计数变为1,是因为b重新分配了空间。
  上面说描述的现象就是写时复制。

写时复制

  写时复制Copy-on-Write,也缩写为COW),顾名思义,就是在写入时才真正复制一份内存进行修改。 COW最早应用在*nix系统中对线程与内存使用的优化,后面广泛的被使用在各种编程语言中,如C++的STL等。 在PHP内核中,COW也是主要的内存优化手段。 在前面关于变量和内存的讨论中,引用计数对变量的销毁与回收中起着至关重要的标识作用。 引用计数存在的意义,就是为了使得COW可以正常运作,从而实现对内存的优化使用。
   写时复制优点:是通过赋值的方式赋值给变量时不会申请新内存来存放新变量所保存的值,而是简单的通过一个计数器来共用内存,只有在其中的一个引用指向变量的值发生变化时才申请新空间来保存值内容以减少对内存的占用。

  从PHP底层基础数据结构来看

ref_count和is_ref是定义于zval结构体中;
  is_ref标识是不是用户使用 & 的强制引用;
 ref_count是引用计数,用于标识此zval被多少个变量引用,即写时复制的自动引用,为0时会被销毁。

The above is the detailed content of Code example sharing of Copy On Write in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template