Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51319 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26253 invoked from network); 19 Jan 2011 14:23:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2011 14:23:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=martinscotta@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=martinscotta@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: martinscotta@gmail.com X-Host-Fingerprint: 209.85.216.42 mail-qw0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:43366] helo=mail-qw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B4/23-08631-7F3F63D4 for ; Wed, 19 Jan 2011 09:23:52 -0500 Received: by qwj8 with SMTP id 8so883152qwj.29 for ; Wed, 19 Jan 2011 06:23:49 -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:cc:content-type; bh=2GHAsispMWVajg7OQdLU7t8gUtIsRjulTZ53e3C7MTs=; b=OerD/MhbJa4xWvgkq6l05Ey/as4Du5zAahwEGToRPp1YuPqw5+XkWI8Pco1+v2B4Xr oupyHQTqF0esgS6wP+L/UJVLaNQ/JOJ97gYxOash8CqCw2kVOlFxDDB0wmkYdHlGqWkP mnSrrfzlwP3+iHhoZz0QVsPIF4CXe5q3kTS4w= 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 :cc:content-type; b=TOzI+2MYLIt/xAslMzdD4mnlp//y1P94HhnS0yjzNPxvmiOP+7ggOp5O7kAJLYi0Rk iVZ+Kc8jvah3hLdC8PtxdFt1nb9JLzKfUNpfn+Ebu3PSFM9PnKgQkIhwXl8gAjZnhxIH vNsI4TB007gfMvveVI9TVtsZtN15DKxYdxx0A= MIME-Version: 1.0 Received: by 10.224.80.136 with SMTP id t8mr755223qak.113.1295447029477; Wed, 19 Jan 2011 06:23:49 -0800 (PST) Received: by 10.220.179.136 with HTTP; Wed, 19 Jan 2011 06:23:49 -0800 (PST) In-Reply-To: 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 11:23:49 -0300 Message-ID: To: Hannes Landeholm Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=0015175cd598047293049a33c471 Subject: Re: [PHP-DEV] How deep is copy on write? From: martinscotta@gmail.com (Martin Scotta) --0015175cd598047293049a33c471 Content-Type: text/plain; charset=ISO-8859-1 What about objects? class Foo { public $foo; } function test($o) { $o->foo->foo->foo = 2; } $bar = new Foo; $bar->foo = new Foo; $bar->foo->foo = new Foo; test( $bar ); --- Also... is it better to pass an object as a parameter rather than many values? function withValues($anInteger, $aBool, $aString) { var_dump($anInteger, $aBool, $aString); } function withObject(ParamOject $o) { var_dump( $o->theInteger(), $o->theBool(), $o->theString() ); } Martin Scotta On Wed, Jan 19, 2011 at 5:03 AM, Hannes Landeholm wrote: > 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 > lots > > 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'] = 1; > > $poink['narf'] = 1; > > $a['foo']['bar'] =& $bar; > > $a['foo']['poink'] =& $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=$b['bar']; > > $b['bar']=$b_bar; which would break the reference and make a copy of just > > 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. So it does copy the entire array. Crap. > :-) > >> > >> 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 = 8 ZVals? (That seems logical to me because each its its > 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. I have a question about the PHP runtime that I hope is > >>>> appropriate for this list. (If not, please thwap me gently; I bruise > >>>> easily.) > >>>> > >>>> I know PHP does copy-on-write. However, how "deeply" does it copy > when > >>>> dealing with nested arrays? > >>>> > >>>> This is probably easiest to explain with an example... > >>>> > >>>> $a['foo']['bar']['baz'] = 1; > >>>> $a['foo']['bar']['bob'] = 1; > >>>> $a['foo']['bar']['narf'] = 1; > >>>> $a['foo']['poink']['narf'] = 1; > >>>> > >>>> function test($b) { > >>>> > >>>> // Assume each of the following lines in isolation... > >>>> > >>>> // Does this copy just the one variable baz, or the full array? > >>>> $b['foo']['bar']['baz'] = 2; > >>>> > >>>> // Does this copy $b, or just $b['foo']['poink']? > >>>> $b['foo']['poink']['stuff'] = 3; > >>>> > >>>> return $b; > >>>> > >>>> } > >>>> > >>>> // I know this is wasteful; I'm trying to figure out just how > wasteful. > >>>> $a = test($a); > >>>> > >>>> test() in this case should take $b by reference, but I'm trying to > >>>> determine how much of a difference it is. (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 > > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --0015175cd598047293049a33c471--