Req #69321 [NEW]: Please clarify when users should design functions for pass-by-reference
| From: | php at richardneill dot org | Date: | Sat, 28 Mar 2015 19:33:31 +0000 |
| Subject: | Req #69321 [NEW]: Please clarify when users should design functions for pass-by-reference | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-12058@lists.php.net to get a copy of this message | ||
From: php at richardneill dot org
Operating system:
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Feature/Change Request
Bug description:Please clarify when users should design functions for pass-by-reference
Description:
------------
I think it would be worth clarifying when it is necessary to use
pass-by-reference vs pass-by-value. The script below shows that: PHP
already optimises pass-by-value function calls to do zero-copy (or
copy-on-write) regardless of whether the function definition contains a
by-reference instruction.
Also, it would be helpful to explain why call-time-pass-by-reference is
not allowed [I can see cases where a function that works normally
by-value could sometimes be called by-reference.] Lastly, why can't we
have the "&" in the function call, even if it is mere syntactic sugar?
It's otherwise a source of code-unreadability.
Test script:
---------------
#!/usr/bin/php
<?php
function sum($array,$max){ //For Reference, use: "&$array"
$sum=0;
for ($i=0; $i<2; $i++){
#$array[$i]++; //Uncomment this line to modify the array
within the function.
$sum += $array[$i];
}
return ($sum);
}
$max = 1E7 //10 M data points.
$data = range(0,$max,1);
$start = microtime(true);
for ($x = 0 ; $x < 100; $x++){
$sum = sum($data, $max);
}
$end = microtime(true);
echo "Time: ".($end - $start)." s\n";
?>
Actual result:
--------------
Measured Run times:
# PASS BY MODIFIED? Time
- ------- --------- ----
1 value no 56 us
2 reference no 58 us
3 valuue yes 129 s
4 reference yes 66 us
Conclusions:
1. PHP is already smart about zero-copy / copy-on-write. A function call
does NOT copy the data unless it needs to; the data is
only copied on write. That's why #1 and #2 take similar times,
whereas #3 takes 2 million times longer than #4.
[You never need to use &$array to ask the compiler to do a zero-copy
optimisation; it can work that out for itself.]
2. You do use &$array to tell the compiler "it is OK for the function
to over-write my argument in place, I don't need the original
any more." This can make a huge difference to performance when we
have large amounts of memory to copy.
(This is the only way it is done in C, arrays are always passed as
pointers)
3. The other use of & is as a way to specify where data should be
*returned*. (eg as used by exec() ).
(This is a C-like way of passing pointers for outputs, whereas PHP
functions normally return complex types, or multiple answers
in an array)
4. It's unhelpful that only the function definition has &. The caller
should have it, at least as syntactic sugar. Otherwise
it leads to unreadable code: because the person reading the function
call doesn't expect it to pass by reference. At the moment,
it's necessary to write a function call with a comment, thus:
$sum = sum($data,$max); //warning, $data passed by reference, and may
be modified.
5. Sometimes, pass by reference could be at the choice of the caller,
NOT the function definition. PHP doesn't allow it, but it
would be meaningful for the caller to decide to pass data in as a
reference. i.e. "I'm done with the variable, it's OK to stomp
on it in memory".
--
Edit bug report at https://bugs.php.net/bug.php?id=69321&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69321&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69321&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69321&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69321&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69321&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69321&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69321&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69321&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69321&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69321&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69321&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69321&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69321&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69321&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69321&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69321&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69321&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69321&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69321&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69321&r=mysqlcfg