In PHP, is it less resource intensive (smarter) to transfer a very large amount of data by reference than by value?

Possible duplicate:
In PHP (> = 5.0) is the link faster?

Let's say that I'm dealing with a large data array (no matter how important) and passing it to a function. Of course, it will be passed by value, and not by default link. OK, I know that.

So my question is:

If the data stored in this array was large enough, performance could stand or memory usage can be obtained by storing data in an object (for example, stdClass ) or using foo (& $ myData) so that the data is passed by reference instead of a value?

I understand that when it passes the value to PHP, it will create a completely new pointer to an all-new memory allocation for the data, while the pass-by-value variable exists in the function area. In this case, you may need to transfer a large amount of data by reference for this reason? I do not know if this understanding is correct ... this part of the question.

Note: this is hypothetical, so don’t tell anything about “premature optimization”, “you would be better off minimizing disk I / O before worrying about it,” it depends on how much RAM you have, ”“ etc. . "

UPDATE

One more question: let's say, the data is transferred to the constructor of the object, which must be saved as one of these properties of the object, and it changes in some small way, since it is stored in a new object. Will there be a significant difference in the aspect of memory usage in the operation? Is it right to assume that it would be better to define the data in the destination format in order to avoid the costs associated with changing the data per row?

+4
source share
3 answers

I can’t find anything official, but in my opinion, the Zend Engine will only copy the array when trying to write to it. That is, there is no copy in this following code:

function printFirst($array) { echo $array[0]; } $array = range(1,1000); printFirst($array); 

However, a copy will be made in this code if no other optimizations are made (i.e., this example function does nothing, so it can be completely eliminated).

 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 really need to change the original, then of course you need a link anyway.

Notice, I found a duplicate question here: In PHP (> = 5.0) is the link faster? And here: How does PHP assign and free memory for variables?

Update

This is the code I ran that demonstrated the significant memory usage of the object, although 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; ?> 
+6
source

From comments on Array type, PHP manual

It is true that "the purpose of an array is always associated with copying a value," but a copy is a "lazy copy." This means that the data of two variables occupy the same memory until the array element changes.

For example, if you need to pass an array to a function that should read only it, there is no advantage in passing it by reference.

+5
source

Keep in mind that PHP really ... Platform. With resources and arrays, you can expect PHP to use more up-to-date memory. Arrays in PHP will always consume more resources than your regular C ++ integer array, because PHP arrays always map to the map. However, the good news is that PHP always cleans its resources after execution.

This is not something to really worry about. I used a server with 2 GB of RAM running on the chat website and it never gave me grief.

0
source

Source: https://habr.com/ru/post/1392838/


All Articles