473,287 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 28886
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.