This doesn't fit the given list of Category selection, I consider it an enhancement request but...

The attached patch helps to remove copying of the node array/object variable and therefore helps to speed up the construction of the node array. This change does force changes to other modules such as blog.module such that it isn't setting the $node variable with the return value of the changed functions. If this patch is accepted I will make the necessary changes to the other modules.

CommentFileSizeAuthor
node_by_ref.diff_0.txt3.29 KBAnonymous (not verified)

Comments

Jaza’s picture

Category: bug » task

Seems like a good idea, that would make things cleaner and more consistent. However, I doubt that this would have much performance improvement (if any), since many PHP experts have consistently stated that passing by ref is not faster than passing by value (in fact, it can often be slower), due to optimisation processes in the PHP interpreter.

chx’s picture

With objects on PHP4, I am unsure. Please run this on PHP4

$a = new stdClass();
$a->x = range(0, 50000);
function x($object, $m) {
  print (memory_get_usage() - $m);
}
$m = memory_get_usage();
x($a, $m);

On PHP5 this prints 152 bytes so in PHP5 there is certainly no point in this patch.

Anonymous’s picture

Both Jaza and Chx, the savings isn't from the entry point of the function. The savings is at the exit point of the function. Currently the $node variable is returned and that value is needed to be stored again.

The pass by ref patch saves the need to return the object and store it again in the calling object.

AjK’s picture

Status: Needs review » Closed (won't fix)

On PHP4 assignment by function call will copy the object (as opposed to bump it's ref count). So, in PHP4 there's a fractional improvement in speed. The speed improvement is, however, hardly worth mentioning.

The benefit this brings is weighted in terms of a greater loss in maintainerbility. Directly equating the return value is far more readable than abstractly passing a reference.

As chx points out, in PHP5 this patch makes no difference, by methods bump the zval refcount by 1 so no gain, just a loss of maintainerbility.

So, purely on the maintainerbility it's a "won't fix".

Anonymous’s picture

So you say

phpinfo();
$a = new stdClass();
function x($object, $m) {
  $object->x = range(0, 50000);
  return $object;
}
$m = memory_get_usage();
print ("$m<br/>");
$a = x($a, $m);
print (memory_get_usage() - $m);

is no different than

phpinfo();
$a = new stdClass();
function x(&$object, $m) {
  $object->x = range(0, 50000);
  return $object;
}
$m = memory_get_usage();
print ("$m<br/>");
x($a, $m);
print (memory_get_usage() - $m);
Anonymous’s picture

Let me restate

So you say

phpinfo();
$a = new stdClass();
function x($object, $m) {
  $object->x = range(0, 50000);
  return $object;
}
$m = memory_get_usage();
print ("$m<br/>");
$a = x($a, $m);
print (memory_get_usage() - $m);

is no different than

phpinfo();
$a = new stdClass();
function x($object, $m) {
  $object->x = range(0, 50000);
}
$m = memory_get_usage();
print ("$m<br/>");
x($a, $m);
print (memory_get_usage() - $m);
chx’s picture

http://php.net/manual/en/language.oop5.basic.php says that

When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it.

.

So, again, as AJK said, while the patch has some merits on PHP4, there is none on PHP5. If these would be in a loop, called several hundreds time on one page, then not doing the in-memory copying for PHP4 could have some impact.

Anonymous’s picture

Objects and arrays are the same I suppose. The fix does save a few bytes and possibly a microsecond or two of time for every node on every server displayed on every page and every module that uses the functions. How many times do these functions get called? I remember some comment that Dries wanted to tweek the resource use bit by bit and I commited this patch in cooperation with that and another patch that was committed to 5 that changed a function to accept the node array with call by ref. This patch was a complement to the patch that was committed.

Crell’s picture

Objects and arrays are the same I suppose.

Well, no. In PHP 4 they behaved the same way. An object is basically an array with methods attached to it. In PHP 5, that's no longer the case. They're more *resources* with methods attached to them. That's how they effectively are "passed by reference" by default. (More specifically, they are passed by value but the value is no longer the object but a pointer to it, just like in Java.)

In either case, though, you should only pass by reference if you want reference-esque functionality. PHP does the performance saving for you if it can. It's nice like that.