Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51317 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85394 invoked from network); 19 Jan 2011 08:03:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2011 08:03:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=landeholm@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=landeholm@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.54 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.218.54 mail-yi0-f54.google.com Received: from [209.85.218.54] ([209.85.218.54:63737] helo=mail-yi0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/A0-16073-5EA963D4 for ; Wed, 19 Jan 2011 03:03:50 -0500 Received: by yie19 with SMTP id 19so224697yie.13 for ; Wed, 19 Jan 2011 00:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=P6eBjzJuIjoeyAeKFIYR3fHsKoml8IQ5kXSszosmhh4=; b=pbPp1uCzQXRSvV8XuGrAjULaWtgX+nhH9sasl1GkgONqpxBzKnboyrRJ/5WQtBz9Mr Ls8yhS04/O8mgvVFH/LVURzGgSM0/0XvuNRyqXD+saMVY0XNrjhG2fTJMTYwT5AZt8vj ZO9tTC24zgOA2TuPiMYYNuobxVdWfMeWN1ieQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=sHdgm6NBxUg9FHrXxJKTaWgZXC7MjQeqqTrP7l6Vv/b8wRLq0V2Ammq2XQ4pXzYyaW rfvr1rKEGI2rK0rx+KnsiSaB/MCFGFa/QmWfUzGQGDJTmiMSKbLh1Z1wHn+22JOwTS4I CGoKWpGek/AbneaNCF4SvOFWX+vezzUr1yWrM= MIME-Version: 1.0 Received: by 10.100.252.20 with SMTP id z20mr280280anh.104.1295424227305; Wed, 19 Jan 2011 00:03:47 -0800 (PST) Received: by 10.101.101.2 with HTTP; Wed, 19 Jan 2011 00:03:47 -0800 (PST) In-Reply-To: <4D369473.4040608@yahoo.com.au> References: <201101190045.31781.larry@garfieldtech.com> <4D368C58.2040903@yahoo.com.au> <201101190109.52250.larry@garfieldtech.com> <4D369473.4040608@yahoo.com.au> Date: Wed, 19 Jan 2011 09:03:47 +0100 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] How deep is copy on write? From: landeholm@gmail.com (Hannes Landeholm) Using references does not speed up PHP. It does that already internally, if I'm not mistaken. The point of my post was that assigning values to tree arrays are in general faster than a full array copy. Hannes On 19 January 2011 08:36, Ben Schmidt wrote= : > Yep. PHP does clock up memory very quickly for big arrays, objects with l= ots > of members and/or lots of small objects with large overheads. There are a > LOT of zvals and zobjects and things around the place, and their overhead > isn't all that small. > > Of course, if you go to the trouble to construct arrays using references, > you can avoid some of that, because a copy-on-write will just copy the > reference. It does mean you're passing references, though. > > $bar['baz'] =3D 1; > $poink['narf'] =3D 1; > $a['foo']['bar'] =3D& $bar; > $a['foo']['poink'] =3D& $poink; > > Then if you test($a), $bar and $poink will be changed, since they are > 'passed by reference'--no copying needs to be done. It's almost as if $b > were passed by reference, but setting $b['blip'] wouldn't show up in $a, > because $a itself would be copied in that case, including the references, > which would continue to refer to $bar and $poink. So a much quicker copy, > but obviously not the same level of isolation that you might expect or > desire. Unless you did some jiggerypokery like $b_bar=3D$b['bar']; > $b['bar']=3D$b_bar; which would break the reference and make a copy of ju= st > that part of the array. But this is a pretty nasty caller-callee > co-operative kind of thing. Just a thought to throw into the mix, though. > > Disclaimer: I'm somewhat out of my depth here. But I'm sure someone will > jump on me if I'm wrong. > > Ben. > > > > On 19/01/11 6:09 PM, Larry Garfield wrote: >> >> That's what I was afraid of. =A0So it does copy the entire array. =A0Cra= p. :-) >> >> Am I correct that each level in the array represents its own ZVal, with >> the >> additional memory overhead a ZVal has (however many bytes that is)? >> >> That is, the array below would have $a, foo, bar, baz, bob, narf, poink, >> poink/narf =3D 8 ZVals? =A0(That seems logical to me because each its it= s own >> variable that just happens to be an array, but I want to be sure.) >> >> --Larry Garfield >> >> On Wednesday, January 19, 2011 1:01:44 am Ben Schmidt wrote: >>> >>> It does the whole of $b. It has to, because when you change 'baz', a >>> reference in 'bar' needs to change to point to the newly copied 'baz', = so >>> 'bar' is written...and likewise 'foo' is written. >>> >>> Ben. >>> >>> On 19/01/11 5:45 PM, Larry Garfield wrote: >>>> >>>> Hi folks. =A0I have a question about the PHP runtime that I hope is >>>> appropriate for this list. =A0(If not, please thwap me gently; I bruis= e >>>> easily.) >>>> >>>> I know PHP does copy-on-write. =A0However, how "deeply" does it copy w= hen >>>> dealing with nested arrays? >>>> >>>> This is probably easiest to explain with an example... >>>> >>>> $a['foo']['bar']['baz'] =3D 1; >>>> $a['foo']['bar']['bob'] =3D 1; >>>> $a['foo']['bar']['narf'] =3D 1; >>>> $a['foo']['poink']['narf'] =3D 1; >>>> >>>> function test($b) { >>>> >>>> =A0 =A0// Assume each of the following lines in isolation... >>>> >>>> =A0 =A0// Does this copy just the one variable baz, or the full array? >>>> =A0 =A0$b['foo']['bar']['baz'] =3D 2; >>>> >>>> =A0 =A0// Does this copy $b, or just $b['foo']['poink']? >>>> =A0 =A0$b['foo']['poink']['stuff'] =3D 3; >>>> >>>> =A0 =A0return $b; >>>> >>>> } >>>> >>>> // I know this is wasteful; I'm trying to figure out just how wasteful= . >>>> $a =3D test($a); >>>> >>>> test() in this case should take $b by reference, but I'm trying to >>>> determine how much of a difference it is. =A0(In practice my use case = has >>>> a vastly larger array, so any inefficiencies are multiplied.) >>>> >>>> --Larry Garfield >> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >