News for hackers. Stuff that matters.
Re:OK, here's the thing: this is only the entrance of the ra... by lanstein on Monday October 18, 2010 @11:04PM (#1806043)
The real problems with PHP crop up when you get to the margins, like when you're trying to tune various routines, particular wrt memory usage. Sometimes, it's more efficient to take arguments as (&$foo), and sometimes, it's not. This is why I've written (PHP 5.2.x no less) abortions like:

    public function catalogue($str, &$str2, &$str3) {
depending on whether the parameter is a frequently referenced string, and the type of the parameter altogether. It seems like it's completely trial and error with these sorts of things, and when you note that it takes (iirc) ~70MB to store a million one-char elements in an array, it really matters.

Also, sometimes, PHP will patch through returning results of a function, but sometimes, it's actually cheaper to create a temporary, a la

  $str = strtolower($foo);
  return $str;
These are just a couple of gripes off the top of my head.
Re:OK, here's the thing: this is only the entrance of the ra... by wvenable on Monday October 18, 2010 @11:35PM (#1806103)
PHP has copy-on-write semantics for strings and arrays -- your sort of reference optimizations seem a little strange to me. That is, unless you're not intending to making copies of structures when you change them inside of functions, in that case using references is not a strange optimization but just the correct choice.
Re:OK, here's the thing: this is only the entrance of the ra... by lanstein on Monday October 18, 2010 @11:52PM (#1806138)
I know PHP uses COW, so theoretically, reading from a variable should have no impact on memory usage whatsoever. This has not proven to be the case, I've called get_peak_memory_usage() and played with passing the params explicitly by reference or not. I assume it has something to do with the fact that strings/arrays are not passed by reference, and explicitly declaring the reference is cheaper sometimes and more expensive sometimes depending on refcount(maybe?)
Re:OK, here's the thing: this is only the entrance of the ra... by wvenable on Tuesday October 19, 2010 @12:19AM (#1806195)
I'm not entirely sure how much I'd trust get_peak_memory_usage() for this kind of debugging. I'd be more inclined to profile this with xdebug before making these kinds of code changes.