473,461 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Copy-on-Write semantic

Hi All,

is there a Copy-on-Write semantic behind PHP compiler?

$a = long_string_1mb;
$b = $a; // Compiler will allocates another 1MB for this variable.

I have to use "$b =& $a;" to avoid memory waste.
I think this should be done by compiler itself. For instance:

$a = lang_string_1mb; // Compiler will allocate a memory block and alter
variable $a point to this address. And set reference count of this
memory block to 1.
$b = $a; // Compiler will alter variable $b to the same memory block and
increment reference count to 2.
$b = some_string_else; // Compiler will allocate another memory block
for $b and decrement the previous reference count to 1.

This will be more efficient than managing referencing by ourselves.

--
Xu, Qian (stanleyxu)
Feb 11 '08 #1
3 28891
On Mon, 11 Feb 2008 16:03:25 +0100, Xu, Qian <no******@microsoft.com
wrote:
Hi All,

is there a Copy-on-Write semantic behind PHP compiler?

$a = long_string_1mb;
$b = $a; // Compiler will allocates another 1MB for this variable.
Not yet...
I have to use "$b =& $a;" to avoid memory waste.
I think this should be done by compiler itself. For instance:
It is done. Only if you change either $a or $b will they be copied/doubled
(see memory usage on a test run here as a comment after the line):

<?php
echo memory_get_usage()."\n"; //55736
$a = str_repeat('a',1024*1024);
echo memory_get_usage()."\n"; //1104672
$b = $a;
//memory usage should be altered only slightly:
echo memory_get_usage()."\n";//1104696
$b = str_replace('foo','bar',$b);//
echo memory_get_usage()."\n";//2153344
unset($b);
echo memory_get_usage()."\n";//1104400
?>

Compared to reference (which will alter BOTH $a & $b):
<?php
echo memory_get_usage()."\n"; //55520
$a = str_repeat('a',1024*1024);
echo memory_get_usage()."\n"; //1104288
$b = &$a;
//memory usage should be altered only slightly:
echo memory_get_usage()."\n";//1104336
$b = str_replace('foo','bar',$b);//
echo memory_get_usage()."\n";//1104400
unset($b);
echo memory_get_usage()."\n";//1104400
?>

In short, as the manual allready states, don't use references as a
premature optimizer. The interpreter is smart enough to do it. Only use
references if you need the reference in your code.
--
Rik Wasmus
Feb 11 '08 #2
..oO(Rik Wasmus)
>On Mon, 11 Feb 2008 16:40:42 +0100, Michael Fesser <ne*****@gmx.dewrote:
>Some more details:

http://blog.libssh2.org/index.php?/a...-lied-to..html

Hmm, interested in the link, seems dead here though (times out even after
5 minutes of waiting...)
Works here.

Try the Google cache:

<http://www.google.com/search?q=cache:OYf5tIVEHfEJ:blog.libssh2.org/index.php%3F/archives/51-Youre-being-lied-to..html>

Micha
Feb 11 '08 #3
On Mon, 11 Feb 2008 17:22:56 +0100, Michael Fesser <ne*****@gmx.dewrote:
.oO(Rik Wasmus)
>On Mon, 11 Feb 2008 16:40:42 +0100, Michael Fesser <ne*****@gmx.de
wrote:
>>Some more details:

http://blog.libssh2.org/index.php?/a...-lied-to..html

Hmm, interested in the link, seems dead here though (times out even
after
5 minutes of waiting...)

Works here.

Try the Google cache:

<http://www.google.com/search?q=cache:OYf5tIVEHfEJ:blog.libssh2.org/index.php%3F/archives/51-Youre-being-lied-to..html>
Ah, that works. Nothing new for me, but clearly explained stuff. I'll keep
it bookmarked for future reference.
--
Rik Wasmus
Feb 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas Kuntzagk | last post by:
Hi, There are three ways to (shallow)copy a list l I'm aware of: >>> l2=list(l) >>> l2=l >>> l2.copy.copy(l) Are there any differences? Are there more (reasonable) ways? I think the first...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
3
by: mescaline | last post by:
//Consider the simple program with inheritance, plain init for A, copy ctr for B #include <iostream> using namespace std; class Base{ public: Base(){cout << "Base, default" << endl;}...
8
by: Joe Cipale | last post by:
I have a class defined as follows: public: char Date; char weight; char Workout; char Event; TDaily* nxt_Daily; I have defined my constructor/copy methods as shown:
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
24
by: rdc02271 | last post by:
Hello! Is this too crazy or not? Copy constructor: why can't I copy objects as if they were structs? I have a set of simple objects (no string properties, just integers, doubles) and I have to...
12
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.