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