Albright Posted May 5, 2007 Share Posted May 5, 2007 I've recently taken a new job working on a pre-existing site for a financial services company which already has a lot of code and a massive database. In addition to writing new code, I've also been going through some of the pre-existing stuff and tweaking it for performance or convenience. Whoever wrote the bulk of the existing code apparently wasn't familiar with references; none of the function declarations that I've come across so far had any ampersands in them, even when things like database handlers were being passed. So I've been going through and fixing that as I've come across it. My boss has noticed as he has been looking through diffs. He didn't explicitly tell me to stop, but he told me he heard that explicitly using references in PHP, though it may save RAM, actually causes more computational overhead than passing copies and that PHP will actually be smart enough to pass variables to functions by reference anyway if they're not going to be modified -- or something like that. I was skeptical, but conceded that perhaps PHP differs from other languages in regards to referencing and that I would investigate the matter over the weekend. I've looked through References Explained in the PHP manual, and it doesn't seem to align with what he was talking about. But I want to make sure I'm not missing something, since the current site has some slowness issues (though it's really more due to the massive database full of seventeen years' worth of data on mutual funds than the PHP code). So does anyone know for certain if my boss's concern in this matter is justified, or can I go on using ampersands with impunity? Thanks in advance for any light you can shed on this. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/ Share on other sites More sharing options...
Nameless12 Posted May 5, 2007 Share Posted May 5, 2007 Your boss is correct, it is only worth using a reference if you plan on modifying the referenced value. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-245949 Share on other sites More sharing options...
Albright Posted May 5, 2007 Author Share Posted May 5, 2007 I appreciate your feedback, but if that is true, could you at least explain why? It is counter-intuitive. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-245987 Share on other sites More sharing options...
Nameless12 Posted May 5, 2007 Share Posted May 5, 2007 its just to do with the way php is optimized. $a = 'string'; $b = $a; if you do something like this then $b will reference $a until one of them is modified. The reason this is slightly faster is just because it is optimized this way. Remember this is only faster if you are not modifying the value. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-246032 Share on other sites More sharing options...
448191 Posted May 5, 2007 Share Posted May 5, 2007 Overusing references can slow your scripts by a factor of ten. In a perfect world, the difference with a regular value is limited to a single extra bit (struct member is_ref). But at some point you'll need to use the value, pass it to a function that uses it. That function may not accept a reference (as most build in functions don't), requiring a copy to be made. Internally, the link to the original value is lost when the value is assigned by reference; instead it operates on a copy (since the value has changed: is_ref now holds 1). To illustrate this, consider the following example: <?php $a = 1; $b = $a; $c = &$a; ?> $a and $b reference to the same internal value. Next, a reference value is created for $a and $c (1), but $b maintains association with the value of 1. Two versions of the value come into life: a 'copy on write' version associated with $b, and a full reference version associated with $a and $c. So in fact you don't reference to a variable, but to an internal value. And that's a good thing, otherwise one might end up with a cascade of redirections, not unlike asking for FTP access in an enterprise size company (sorry little personal joke - hah I'm so funny ). Sanity check: <?php $a = 1; $b = $a; $c = &$a; echo (++$b).'<br/>'; echo $c.'<br/>'; ?> Outputs 2 and 1, as you would expect. That wouldn't be possible with only one internal value. Sure, if $b was assigned $a by reference, only a single (reference) value would exist, but it is an example of how referencing can actually cause value duplication (as said, many functions force you to pass a regular, 'copy on write' value - or more accurately: they copy a value, regardless if it was a reference variable or not). More information about php's memory allocation is provided here. Overall, be sensible when using references. Remember that php is already very cheap when it comes to allocating memory. Forget about the performance implications and focus on the usage of a value to decide if you should pass it by reference. I.e. do I want the identity of the value to persist? Objects in php4 answer to this question with a wholehearted "yes", they have an identity and want to persist (with the exception of Value Objects). The identity of the other compound datatype, the array, may in some cases also need to persist (although I personally tend to use objects instead whenever this is the case). Scalar values' identities generally do not need to persist as their identity (if you can even say they really have one) is irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-246129 Share on other sites More sharing options...
Albright Posted May 5, 2007 Author Share Posted May 5, 2007 Okay, interesting. Thanks for the info. So this also holds true for passing vars by reference into functions as well as assigning them linearly as in your and that link's examples? And it's great that PHP is trying to save on overhead by handling all this for us, but I still don't see where the slowness will come in by explicitly passing by reference. Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-246140 Share on other sites More sharing options...
448191 Posted May 5, 2007 Share Posted May 5, 2007 Functions usually 'copy' values. This works the same as copying them implicitly. If you need to hear from Zeev himself, checkout this bug report and read the final comment: http://bugs.php.net/bug.php?id=14237 Quote Link to comment https://forums.phpfreaks.com/topic/50091-passing-by-reference-speed-concerns/#findComment-246144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.