Re: [PHP] Return Values Copied? Copied if By Reference?

2006-08-03 Thread Richard Lynch
On Thu, July 27, 2006 1:05 pm, Adam Zey wrote:
 Then how come when I do a foreach on an array (without modifying
 anything within the foreach), it still makes a copy of the array that
 consumes memory? I think it's dangerous to generalize that it's always
 best to let PHP make copies of things. In the foreach situation, the
 preferred solution when memory is a problem is to either use a
 reference, or have foreach iterate over the keys of the array.

I think this is because you can sometimes use  to modify the contents
of the iteratee (is that a word?) so PHP does a copy there blindly,
whether you have  or not, because you might have  there sometimes.

I think Dmitry or Antony or ??? is looking at changing that so the
copy is only done when  is present.

At least, that's how I understand (or not) the upshot of a thread on
Internals.

It's entirely possible, even likely, that I'm completely
misunderstanding both of these threads. :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-27 Thread Robert Cummings
On Thu, 2006-07-27 at 01:35, Larry Garfield wrote:
 On Wednesday 26 July 2006 21:41, Robert Cummings wrote:
 
   I'm working on some code that would be called to generate a cell in a
   possibly large table and therefore a small difference in performance
   may have a significant impact.
 
  PHP uses copy-on-write and so copies are essentially shared until such
  time as you modify one of them. If you don't need references then copies
  are faster than references.
 
 By the same token, then, if I have a function that generates a large string 
 and returns it, is there any benefit to return-by-reference?

Nope. You should only use references if you really need them. Attempting
to improve efficiency by using references instead of copies when you
aren't actually in need of a reference will result in less efficiency
since the engine spends more time creating the reference than the copy.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-27 Thread Adam Zey

Robert Cummings wrote:

On Thu, 2006-07-27 at 01:35, Larry Garfield wrote:

On Wednesday 26 July 2006 21:41, Robert Cummings wrote:


I'm working on some code that would be called to generate a cell in a
possibly large table and therefore a small difference in performance
may have a significant impact.

PHP uses copy-on-write and so copies are essentially shared until such
time as you modify one of them. If you don't need references then copies
are faster than references.
By the same token, then, if I have a function that generates a large string 
and returns it, is there any benefit to return-by-reference?


Nope. You should only use references if you really need them. Attempting
to improve efficiency by using references instead of copies when you
aren't actually in need of a reference will result in less efficiency
since the engine spends more time creating the reference than the copy.

Cheers,
Rob.


Then how come when I do a foreach on an array (without modifying 
anything within the foreach), it still makes a copy of the array that 
consumes memory? I think it's dangerous to generalize that it's always 
best to let PHP make copies of things. In the foreach situation, the 
preferred solution when memory is a problem is to either use a 
reference, or have foreach iterate over the keys of the array.


Regards, Adam.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Return Values Copied? Copied if By Reference?

2006-07-27 Thread KermodeBear
Robert Cummings wrote:

Then how come when I do a foreach on an array (without modifying 
anything within the foreach), it still makes a copy of the array that 
consumes memory? I think it's dangerous to generalize that it's always 
best to let PHP make copies of things. In the foreach situation, the 
preferred solution when memory is a problem is to either use a 
reference, or have foreach iterate over the keys of the array.

Regards, Adam.

-

PHP doesn't seem to make a real copy of data until one of the copies is
modified, making it necessary to create a new set of data. So, it is pretty
smart about that.

Here is a small CLI script that seems to support this:
?php
$a_array = array();
for($i = 0; $i  1; $i++) {
$a_array[] = time(); // Just an arbitrary piece of data
}
echo 'Memory Usage: ' . memory_get_usage() . \n;
echo Making a copy of the array.\n;
$a_copy = $a_array;
echo 'Memory Usage: ' . memory_get_usage() . \n;
echo Modifying the copy:\n;
$a_copy[] = time();
echo 'Memory Usage: ' . memory_get_usage() . \n;
?

On my machine, this displays:
Memory Usage: 640280
Making a copy of the array.
Memory Usage: 640440
Modifying the copy:
Memory Usage: 1106056

-K. Bear

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-27 Thread Adam Zey

KermodeBear wrote:

Robert Cummings wrote:

Then how come when I do a foreach on an array (without modifying 
anything within the foreach), it still makes a copy of the array that 
consumes memory? I think it's dangerous to generalize that it's always 
best to let PHP make copies of things. In the foreach situation, the 
preferred solution when memory is a problem is to either use a 
reference, or have foreach iterate over the keys of the array.


Regards, Adam.

-

PHP doesn't seem to make a real copy of data until one of the copies is
modified, making it necessary to create a new set of data. So, it is pretty
smart about that.

Here is a small CLI script that seems to support this:
?php
$a_array = array();
for($i = 0; $i  1; $i++) {
$a_array[] = time(); // Just an arbitrary piece of data
}
echo 'Memory Usage: ' . memory_get_usage() . \n;
echo Making a copy of the array.\n;
$a_copy = $a_array;
echo 'Memory Usage: ' . memory_get_usage() . \n;
echo Modifying the copy:\n;
$a_copy[] = time();
echo 'Memory Usage: ' . memory_get_usage() . \n;
?

On my machine, this displays:
Memory Usage: 640280
Making a copy of the array.
Memory Usage: 640440
Modifying the copy:
Memory Usage: 1106056

-K. Bear


I note that your example doesn't use a foreach loop, which is what my 
post was about.


Some testing shows my original premise was wrong, but it's still 
possible to get PHP to copy the array when it doesn't need to. In this 
case, using a reference can save memory even when a reference isn't 
needed. If you want to modify the original array in the foreach, then it 
is better to step over the array yourself and save memory, or pass the 
foreach a reference to the array to stop it from doing a copy.


I'm just trying to say, I don't think it's always a good idea to rely on 
the language deciding when to copy. Sometimes it's a good idea to use a 
reference to stop the copy if memory is a concern.


Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-27 Thread Robert Cummings
On Thu, 2006-07-27 at 14:48, Adam Zey wrote:
 KermodeBear wrote:
  Robert Cummings wrote:
  
  Then how come when I do a foreach on an array (without modifying 
  anything within the foreach), it still makes a copy of the array that 
  consumes memory? I think it's dangerous to generalize that it's always 
  best to let PHP make copies of things. In the foreach situation, the 
  preferred solution when memory is a problem is to either use a 
  reference, or have foreach iterate over the keys of the array.
  
  Regards, Adam.
  
  -
  
  PHP doesn't seem to make a real copy of data until one of the copies is
  modified, making it necessary to create a new set of data. So, it is pretty
  smart about that.
  
  Here is a small CLI script that seems to support this:
  ?php
  $a_array = array();
  for($i = 0; $i  1; $i++) {
  $a_array[] = time(); // Just an arbitrary piece of data
  }
  echo 'Memory Usage: ' . memory_get_usage() . \n;
  echo Making a copy of the array.\n;
  $a_copy = $a_array;
  echo 'Memory Usage: ' . memory_get_usage() . \n;
  echo Modifying the copy:\n;
  $a_copy[] = time();
  echo 'Memory Usage: ' . memory_get_usage() . \n;
  ?
  
  On my machine, this displays:
  Memory Usage: 640280
  Making a copy of the array.
  Memory Usage: 640440
  Modifying the copy:
  Memory Usage: 1106056
  
  -K. Bear
 
 I note that your example doesn't use a foreach loop, which is what my 
 post was about.
 
 Some testing shows my original premise was wrong, but it's still 
 possible to get PHP to copy the array when it doesn't need to. In this 
 case, using a reference can save memory even when a reference isn't 
 needed. If you want to modify the original array in the foreach, then it 
 is better to step over the array yourself and save memory, or pass the 
 foreach a reference to the array to stop it from doing a copy.
 
 I'm just trying to say, I don't think it's always a good idea to rely on 
 the language deciding when to copy. Sometimes it's a good idea to use a 
 reference to stop the copy if memory is a concern.

PHP creates a real copy when one of the original arrays changes. This is
the principle of copy-on-write. If you are getting a copy then one of
the arrays changed, so now you really have to choose between a copy and
reference knowing that a reference will affect all arrays to which you
made a reference or a copy which will only affect the copy to which the
change was made. So this returns to my original assertion that you
should only use references when a reference is necessary.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-26 Thread Robert Cummings
On Wed, 2006-07-26 at 22:29, Michael B Allen wrote:
 Is a function return value copied? If the value is an integer I suppose
 it is but what about a string or an array? If you pass by reference is
 the return value still copied?
 
 For example, is this:
 
   function foo($arr) {
   $arr[] = bar;
   }
 
 faster than this?
 
   function foo($arr) {
   $arr[] = bar;
   return $arr; // is this copied?
   }
 
 I'm working on some code that would be called to generate a cell in a
 possibly large table and therefore a small difference in performance
 may have a significant impact.

PHP uses copy-on-write and so copies are essentially shared until such
time as you modify one of them. If you don't need references then copies
are faster than references.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Return Values Copied? Copied if By Reference?

2006-07-26 Thread Larry Garfield
On Wednesday 26 July 2006 21:41, Robert Cummings wrote:

  I'm working on some code that would be called to generate a cell in a
  possibly large table and therefore a small difference in performance
  may have a significant impact.

 PHP uses copy-on-write and so copies are essentially shared until such
 time as you modify one of them. If you don't need references then copies
 are faster than references.

By the same token, then, if I have a function that generates a large string 
and returns it, is there any benefit to return-by-reference?

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php