In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian to share with you the sample analysis of copying in PHP, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
Before we start, we can look at a simple code:
Executing this code will print the number 2. From the memory point of view to analyze this code "may" be executed like this: allocate a block of memory to the foo variable, which stores a 1; allocate another block of memory to the bar variable, which also stores a 1; and finally calculate the result output. In fact, we found that foo and bar variables can use the same block of memory because they have the same value, thus saving a 1 in memory usage and eliminating the computational overhead of allocating memory and managing memory addresses. Yes, many systems involved in memory management implement this strategy of sharing memory with the same value: copy on write.
Many times, we are afraid of concepts because of some terminology, but in fact, their basic principles are often very simple. This section describes the implementation of this copy-on-write strategy in PHP:
Copy on Write (also abbreviated as COW) has many application scenarios, such as optimization of memory use in process copying in Linux, and similar applications in various programming languages, such as STL of C++. COW is a common optimization method, which can be classified into: delayed resource allocation. Resources are consumed only when they are actually needed, and copy-on-write usually reduces resource consumption.
Note: To save space, COW will be used consistently below to denote "copy-on-write";
Defer optimization of memory replication
As mentioned earlier, COW in PHP can be simply described as: if you assign to a variable by way of assignment, you don't request new memory to store the value of the new variable, but simply share memory through a counter, and only request new space to store the value content when one of the references to the variable changes to reduce the memory consumption. PHP COW optimizes memory in many scenarios. For example: multiple assignment of variables, function parameter transfer, and modification of arguments in the function body.
Let's look at an example of looking at memory to make it easier to see the obvious effect of COW on memory usage optimization:
//----Implementation results----
foo: (refcount=1, is_ref=0)=1
foo: (refcount=2, is_ref=0)=1
foo: (refcount=1, is_ref=0)=1
From the previous chapter on variables, we know that when $foo is assigned, 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 memory to $bar, but instead points $foo and $bar to the same address. At the same time, the reference count is incremented by 1, which is the new 2. Then, we change the value of $bar, so if we directly need the memory pointed to by the $bar variable, the value of $foo will also change. This is not the outcome we want. The PHP kernel then copies out a copy of memory and updates its value to the assigned value: 2 (this operation is also known as the variable separation operation), while the original $foo variable points to memory that only $foo points to, so the reference count is updated to: refcount=1.
It looks simple, but the actual situation is much more complicated due to the presence of the & operator. See the following examples:
Figure 6.6 & Operator-induced memory copy separation>
This example shows a problematic handling of the & operator in PHP: when $beauty=&$pan;, both variables essentially become reference types, resulting in the seemingly ordinary variable $pan, which behaves the same as &$pan in some internal processing, especially when using reference variables in array elements, which can easily cause problems. (See final example)
Most of PHP's work is text processing, and variables are carriers. The use of different types of variables runs through the PHP life cycle. The COW strategy of variables also reflects the Zend engine's handling of variables and their memory. For details, please refer to the relevant content of the source code file:
The copy 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 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_ISREF_TO(z, isref) Z_SET_ISREF_TO_P(&(z), isref)
Finally, be careful with references to &
References are not the same thing as references in PHP. References are similar to pointers in C. They can all be accessed through different tags, but PHP references are simply variable aliases without the flexibility and limitations of C instructions.
PHP has a lot of unexpected behavior, some because of historical reasons, can not break compatibility and choose not to fix temporarily, or some use scenarios are relatively small. In PHP you can only avoid these traps as much as possible. For example, the following example.
Because reference operators lead to PHP COW policy optimization, the use of references also requires a clear understanding of the behavior of references to avoid misuse and avoid bringing some more difficult bugs. If you think you know enough about references in PHP, try explaining the following example:
The copy code is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.