drupal_static and XHProf I'm using D6 but stole drupal_static() from D7 for a high demand site I'm currently working on.

I'm currently optimising some of the slower pages with XHProf which has been fine, but I'm getting a strange result with drupal_static().

It doesn't matter whether I use:

$registry = &amp;drupal_static(__FUNCTION__, array());


Or the fast static method:

static $registry;
if (!isset($registry)) {
  $registry = &amp;drupal_static(__FUNCTION__, array());
}


The number of calls to drupal_static() is given as the same in XHProf.

Am I missing something?
You should use either static declaration or drupal_static(), but not both for the same variable. The issue here is that the reference is lost because references are not stored statically.

From https://www.drupal.org/node/619666#comment-2211696


  So unfortunately this is not possible (declarations of static variables don't accept expressions):


static $implementations = &amp;drupal_static(__FUNCTION__, array());



  An this neither (it'll lose the reference):


static $implementations;
if (!isset($implementations)) {
  $implementations = &amp;drupal_static(__FUNCTION__, array());
}



  But this is:


static $cache = array();
if (!isset($cache['implementations'])) {
  $cache['implementations'] = &amp;drupal_static(__FUNCTION__, array());
} 
$implementations = &amp;$cache['implementations'];


So you should do this:

static $cache;
if (!isset($cache)) {
  $cache['registry'] = &amp;drupal_static(__FUNCTION__, array());
}
$registry = &amp;$cache['registry'];