Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning by reference

In the PHP documentation it says:

Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own.

I wish to return a reference to an array (which is a property of my class). How does PHP optimize this, because the array is not an object?

If the array has 1 billion entries, won't I get two arrays with 1 billion entries stored in memory if I don't pass it by reference?

like image 716
Alex Avatar asked May 26 '12 19:05

Alex


1 Answers

PHP uses copy on write. That means that if you pass the huge array as a function parameter and only read from it (such as a foreach), you won't be writing to it so it doesn't need to make a copy.

$count = count($huge_array); // read the reference at the bottom
for($i = 0; $i < $count $i++) {
    $value = 2*$huge_array[$i]/15;
    if($value > 3)
        $sub_array []= $value;
}

The $subarray which should be smaller (it is a subset of the huge array), will contain only the needed (changed) data.

If you do not intend on changing the values of the original $huge_array it will never get copied so no extra memory is used.

If you intend on changing the original values of the array, you need to pass it by reference.

If you intend on making and returning an altered version of the original array then you do need the extra memory PHP is allocating for you.

If you intend on making and returning an altered smaller subset of the original array then you will create a new empty array into which you will copy some of data from the huge array and should be careful not to overwrite values in the $huge_array, so you'll avoid writing from $huge_array and emphasise on reading from it.

This link explains that PHP was optimised for pass by value use cases.

Copy on write only works if the variable isn't a reference being passed to a function expecting a value, if it is, passing it around triggers a copy.

That makes PHP native functions that expected an argument to be passed by value and received a referenced variable copy the value of the reference.

function foo(&$data) {
    for ($i = 0; $i < strlen($data); $i++) {
        do_something($data{$i});
    }
}

$string = "... looooong string with lots of data .....";
foo(string);

Executing strlen in C would imply iterating over the entire string to count it. In PHP strings have an attached length value. So strlen returns the value read from there (fast).

But if you give it a referenced variable it will have to copy it before reading it's length so it will iterate over the value to copy it into it's argument list, read and return the length (and subsequently release the memory for the freshly copied string).

like image 138
Mihai Stancu Avatar answered Oct 10 '22 16:10

Mihai Stancu