From: Robert Cummings on
Peter Lind wrote:
> This is one example where references actually decrease memory usage.
> The main reason is the recursive nature of the function. Try

BTW, it's not the recursive nature of the function causing the problem.
It's the movement of the internal pointer within the array. When it
moves the COW realizes the copy's pointer has moved and splits off the
copy. You can verify this by echoing the memory usage when you first
enter carray(). The spike occurs inside the foreach loop.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
From: Peter Lind on
Hmm, will probably have to look inside PHP for this ... the foreach
loop will copy each element as it loops over it (without actually
copying, obviously), however there's no change happening to the
element at any point and so there's nothing to suggest to the
copy-on-write to create a new instance of the sub-array.

It should look like this:
$a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
$b = $a[3];
doStuffs($b);

Whether or not you loop over $a and thus move the internal pointer,
you don't change (well, shouldn't, anyway) $b as that's a subarray
which has it's own internal pointer, that isn't touched.

Or maybe I've gotten this completely backwards ...

Regards
Peter

On 16 March 2010 17:12, Robert Cummings <robert(a)interjinn.com> wrote:
> Peter Lind wrote:
>>
>> This is one example where references actually decrease memory usage.
>> The main reason is the recursive nature of the function. Try
>
> BTW, it's not the recursive nature of the function causing the problem. It's
> the movement of the internal pointer within the array. When it moves the COW
> realizes the copy's pointer has moved and splits off the copy. You can
> verify this by echoing the memory usage when you first enter carray(). The
> spike occurs inside the foreach loop.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
From: Robert Cummings on
Peter Lind wrote:
> Hmm, will probably have to look inside PHP for this ... the foreach
> loop will copy each element as it loops over it (without actually
> copying, obviously), however there's no change happening to the
> element at any point and so there's nothing to suggest to the
> copy-on-write to create a new instance of the sub-array.
>
> It should look like this:
> $a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
> $b = $a[3];
> doStuffs($b);
>
> Whether or not you loop over $a and thus move the internal pointer,
> you don't change (well, shouldn't, anyway) $b as that's a subarray
> which has it's own internal pointer, that isn't touched.
>
> Or maybe I've gotten this completely backwards ...

I'm not sure of the exact reason... PHP has the following comment:

Note: Unless the array is referenced, foreach operates on a copy
of the specified array and not the array itself. foreach
has some side effects on the array pointer. Don't rely on
the array pointer during or after the foreach without
resetting it.

http://php.net/manual/en/control-structures.foreach.php

I've always found the foreach loop to a be a bit on the bizarre size,
this is probably just another one of those bizarrities :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
From: Rene Veerman on
maybe you should be foreach()ing with references?
php.net : search "foreach" :


As of PHP 5, you can easily modify array's elements by preceding
$value with &. This will assign reference instead of copying the
value.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
This is possible only if iterated array can be referenced (i.e. is variable),



On Tue, Mar 16, 2010 at 5:43 PM, Robert Cummings <robert(a)interjinn.com> wrote:
> Peter Lind wrote:
>>
>> Hmm, will probably have to look inside PHP for this ... the foreach
>> loop will copy each element as it loops over it (without actually
>> copying, obviously), however there's no change happening to the
>> element at any point and so there's nothing to suggest to the
>> copy-on-write to create a new instance of the sub-array.
>>
>> It should look like this:
>> $a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
>> $b = $a[3];
>> doStuffs($b);
>>
>> Whether or not you loop over $a and thus move the internal pointer,
>> you don't change (well, shouldn't, anyway) $b as that's a subarray
>> which has it's own internal pointer, that isn't touched.
>>
>> Or maybe I've gotten this completely backwards ...
>
> I'm not sure of the exact reason... PHP has the following comment:
>
>    Note: Unless the array is referenced, foreach operates on a copy
>          of the specified array and not the array itself. foreach
>          has some side effects on the array pointer. Don't rely on
>          the array pointer during or after the foreach without
>          resetting it.
>
>    http://php.net/manual/en/control-structures.foreach.php
>
> I've always found the foreach loop to a be a bit on the bizarre size, this
> is probably just another one of those bizarrities :)
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
From: Robert Cummings on

Rene Veerman wrote:
> maybe you should be foreach()ing with references?
> php.net : search "foreach" :
>
>
> As of PHP 5, you can easily modify array's elements by preceding
> $value with &. This will assign reference instead of copying the
> value.
> <?php
> $arr = array(1, 2, 3, 4);
> foreach ($arr as &$value) {
> $value = $value * 2;
> }
> // $arr is now array(2, 4, 6, 8)
> unset($value); // break the reference with the last element
> ?>
> This is possible only if iterated array can be referenced (i.e. is variable),

References in foreach don't work the way you think they work. You will
still incur the copy. At least I did when I tested earlier today :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP