? .project
? module_implements_cache_rerolled-2.patch
? module_implements_cache_rerolled.patch
? sites/default/files
? sites/default/private
? sites/default/settings.php
Index: update.php
===================================================================
RCS file: /cvs/drupal/drupal/update.php,v
retrieving revision 1.302
diff -u -r1.302 update.php
--- update.php	7 Sep 2009 15:43:54 -0000	1.302
+++ update.php	13 Sep 2009 12:04:21 -0000
@@ -291,6 +291,9 @@
   drupal_load('module', 'system');
   drupal_load('module', 'filter');
 
+  // Reset the module_implements() cache.
+  module_implements('', FALSE, TRUE);
+
   // Set up $language, since the installer components require it.
   drupal_language_initialize();
 
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.988
diff -u -r1.988 common.inc
--- includes/common.inc	11 Sep 2009 15:12:29 -0000	1.988
+++ includes/common.inc	13 Sep 2009 12:04:24 -0000
@@ -2403,6 +2403,7 @@
 
   _registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
   drupal_cache_system_paths();
+  module_implements_write_cache();
 }
 
 /**
Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.157
diff -u -r1.157 module.inc
--- includes/module.inc	24 Aug 2009 00:14:18 -0000	1.157
+++ includes/module.inc	13 Sep 2009 12:04:24 -0000
@@ -14,7 +14,6 @@
   foreach (module_list(TRUE, $bootstrap) as $module) {
     drupal_load('module', $module);
   }
-  module_implements('', FALSE, TRUE);
 }
 
 /**
@@ -344,19 +343,38 @@
  *   An array with the names of the modules which are implementing this hook.
  */
 function module_implements($hook, $sort = FALSE, $refresh = FALSE) {
-  static $implementations;
-
+  $implementations = &drupal_static(__FUNCTION__, array());
   if ($refresh) {
     $implementations = array();
+    variable_set('module_implements', array());
     return;
   }
+  if (empty($implementations)) {
+    // Since the size of the module_implements() cache is relatively small
+    // and is needed on every page, we store the information in a variable
+    // rather than using cache_set() and cache_get().
+    $implementations = variable_get('module_implements_cache', array());
+  }
 
   if (!isset($implementations[$hook])) {
     $implementations[$hook] = array();
     $list = module_list(FALSE, FALSE, $sort);
     foreach ($list as $module) {
       if (module_hook($module, $hook)) {
-        $implementations[$hook][] = $module;
+        $implementations[$hook][$module] = $module;
+        // We added  something to the cache, so write it when we're done.
+        $GLOBALS['module_implements_cache_write'] = TRUE;
+      }
+    }
+  }
+  else {
+    foreach ($implementations[$hook] as $module) {
+      if (!module_hook($module, $hook)) {
+        // Ensure no undefined function errors for calling functions.
+        unset($implementations[$hook][$module]);
+        // Set the module_implements cache to an empty array, this will
+        // force a fresh rebuild in module_implements_write_cache().
+        variable_set('module_implements_cache', array());
       }
     }
   }
@@ -371,6 +389,17 @@
 }
 
 /**
+ * Write to the module_implements() cache.
+ */
+function module_implements_write_cache() {
+  $implementations = &drupal_static('module_implements');
+  // Check wether we need to write the cache.
+  if (isset($GLOBALS['module_implements_cache_write'])) {
+    variable_set('module_implements_cache', $implementations);
+  }
+}
+
+/**
  * Invoke a hook in a particular module.
  *
  * @param $module
