473,588 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ 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_els e; // 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 28902
On Mon, 11 Feb 2008 16:03:25 +0100, Xu, Qian <no******@micro soft.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_usag e()."\n"; //55736
$a = str_repeat('a', 1024*1024);
echo memory_get_usag e()."\n"; //1104672
$b = $a;
//memory usage should be altered only slightly:
echo memory_get_usag e()."\n";//1104696
$b = str_replace('fo o','bar',$b);//
echo memory_get_usag e()."\n";//2153344
unset($b);
echo memory_get_usag e()."\n";//1104400
?>

Compared to reference (which will alter BOTH $a & $b):
<?php
echo memory_get_usag e()."\n"; //55520
$a = str_repeat('a', 1024*1024);
echo memory_get_usag e()."\n"; //1104288
$b = &$a;
//memory usage should be altered only slightly:
echo memory_get_usag e()."\n";//1104336
$b = str_replace('fo o','bar',$b);//
echo memory_get_usag e()."\n";//1104400
unset($b);
echo memory_get_usag e()."\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.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:bl og.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.de wrote:
.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:bl og.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
35768
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 is the most pythonic, second looks more like this other
15
21179
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 pointers) and for objects types call their default constructor. Any others points i should know?
3
4461
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;} Base(const Base &){cout << "Base, Copy Ctor" << endl;} };
8
3466
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
3132
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
12345
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
3609
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 copy the same object millions of times. So instead of writing in the copy constructor property1=SourceObject.property1 can't I use memory copy functions to do this faster? Is this too stupid? By the way, I'm a C++ newbie! But don't go easy on me...
12
3075
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
15784
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
4001
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
0
8352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5723
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2367
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 we have to send another system
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.