4

Possible Duplicate:
In PHP (>= 5.0), is passing by reference faster?

Say I'm dealing with a large-ish (just how big isn't important) array of data and passing it into a function. It will, of course, be passed by value and not by reference by default. Okay, I know that.

So my question is:

If the data stored in this array were large enough, can worthwhile performance or memory usage gains be experienced by storing the data in an object (say, a stdClass) or by using foo(&$myData) so that the data is passed by reference instead of value?

My understanding is that when it's passed by value PHP will create a whole new pointer to a whole new memory allocation for the data while the passed-by-value variable exists in the function scope. In such an event, might passing a large amount of data by reference be worthwhile for this reason? I don't know if this understanding is correct ... that's part of the question.

Note: this is a hypothetical, so don't bring up anything about "premature optimization", "you'd be better off to minimize disk I/O before worrying about this", "depends on how much RAM you have", "etc."

UPDATE

A further question: say the data is being passed to an object constructor to be stored as one of that object's properties and it's being modified in some small way as it's stored in the new object's property. Will there then be a significant difference in the memory usage aspect of the operation? Is it correct to assume that it will be better to define the data in the destination format to begin with to avoid the cost incurred by altering the data down the line?

Community
  • 1
  • 1
  • PHP5, as far as i know, passes arguments as reference/alias/identifier http://php.net/manual/en/language.oop5.references.php – Joseph Jan 25 '12 at 04:52
  • @rla So if the data were dumped into an object constructor and straight into one of that object's properties, it wouldn't incur any additional overhead as long as the data wasn't changed? –  Jan 25 '12 at 04:59
  • @erisco I actually read that question before posting: I felt mine differed because I was just as interested in the memory aspect and I didn't feel it answered my questions. My title probably should've reflected this facet. –  Jan 25 '12 at 05:01

3 Answers3

6

I cannot find anything official, but from my understanding the Zend Engine will only copy the array when you attempt to write to it. That is, there is no copy made in this following code:

function printFirst($array) { echo $array[0]; }

$array = range(1,1000);
printFirst($array);

However, in this code, a copy will be made, assuming no other optimizations are made (ie: this example function doesn't do anything, so it may be excluded entirely).

function changeFirst($array) { $array[0] = 101; }

$array = range(1,1000);
changeFirst($array);

So to answer your question, no, passing by reference will not make any difference. If you actually need to modify the original, then of course you need a reference either way.

Note, I found a duplicate question here: In PHP (>= 5.0), is passing by reference faster? And here: How does PHP assign and free memory for variables?

Update

This is the code I ran which demonstrated a significant memory usage by the object, though not as much as the array itself (about 2/3).

<?php

class A {

public $data;

public function __construct($data) {
        $this->data = $data;
        // slight change
        $this->data[0] += 1;
}

};

$baseUsage = memory_get_usage();
$array = range(1,1000);
$arrayUsage = memory_get_usage();
echo "Array took: ", $arrayUsage - $baseUsage, "\n";
$a = new A($array);
echo "Object took: ", memory_get_usage() - $arrayUsage;

?>
Community
  • 1
  • 1
erisco
  • 14,154
  • 2
  • 40
  • 45
  • +1 For awesome work on the original question. Any chance I could entice you to address my update in the hopes of an acceptance check? :) –  Jan 25 '12 at 05:15
  • erisco, don't whore yourself out like that! haha. :) Good work. – phpmeh Jan 25 '12 at 06:42
5

From comments on the Array datatype, PHP manual

It is true that "array assignment always involves value copying", but the copy is a "lazy copy". This means that the data of the two variables occupy the same memory as long as no array element changes.

E.g., if you have to pass an array to a function that only needs to read it, there is no advantage at all in passing it by reference.

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Keep in mind that that PHP is indeed.. A platform. With resources and arrays, you can expect PHP to use More actual memory. Arrays in PHP will always take up more resources than your normal c++ integer array, because PHP arrays are always mapped. However, the good news is that PHP always cleans up its resources after runtime.

Its not something to be really worried about. I used a server with only 2GB of ram running a Chat Website and it never gave me any grief.

Adam Fowler
  • 1,750
  • 1
  • 17
  • 18