Connecting Tech Pros Worldwide Help | Site Map

Copy-on-Write semantic

  #1  
Old February 11th, 2008, 03:15 PM
Xu, Qian
Guest
 
Posts: n/a
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)
  #2  
Old February 11th, 2008, 04:15 PM
Rik Wasmus
Guest
 
Posts: n/a

re: Copy-on-Write semantic


On Mon, 11 Feb 2008 16:03:25 +0100, Xu, Qian <no_reply@microsoft.com
wrote:
Quote:
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...
Quote:
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
  #3  
Old February 11th, 2008, 04:25 PM
Michael Fesser
Guest
 
Posts: n/a

re: Copy-on-Write semantic


..oO(Rik Wasmus)
Quote:
>On Mon, 11 Feb 2008 16:40:42 +0100, Michael Fesser <netizen@gmx.dewrote:
>>
>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
  #4  
Old February 11th, 2008, 04:35 PM
Rik Wasmus
Guest
 
Posts: n/a

re: Copy-on-Write semantic


On Mon, 11 Feb 2008 17:22:56 +0100, Michael Fesser <netizen@gmx.dewrote:
Quote:
.oO(Rik Wasmus)
>
Quote:
>On Mon, 11 Feb 2008 16:40:42 +0100, Michael Fesser <netizen@gmx.de
>wrote:
>>
Quote:
>>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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
avoid implicit cctor call Olaf answers 9 July 27th, 2007 09:05 PM
CSS and plain text Michael Bulatovich answers 29 September 13th, 2006 04:45 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com answers 26 September 1st, 2006 10:25 PM
function call by value and by reference mitchellpal@gmail.com answers 13 February 6th, 2006 09:45 AM
Javascript Copy Text Boxes..I'm being stupid! @sh answers 6 December 14th, 2005 03:15 AM