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

Reply via email to