Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.243
diff -u -p -r1.243 bootstrap.inc
--- includes/bootstrap.inc	31 Oct 2008 02:18:22 -0000	1.243
+++ includes/bootstrap.inc	31 Oct 2008 22:52:35 -0000
@@ -191,6 +191,12 @@ define('LANGUAGE_NEGOTIATION_DOMAIN', 3)
 define('REQUEST_TIME', $_SERVER['REQUEST_TIME']);
 
 /**
+ * Indicates that _registry_check_code() should write the registry lookup
+ * misses from this request to cache.
+ */
+define('REGISTRY_CHECK_CODE_WRITE_CACHE', -1);
+
+/**
  * @name Title text filtering flags
  * @{
  * Flags for use in drupal_set_title().
@@ -1445,8 +1451,33 @@ function drupal_autoload_class($class) {
 
 /**
  * Helper to check for a resource in the registry.
- */
-function _registry_check_code($type, $name) {
+ *
+ * This function also caches registry lookup misses. If a resource is not
+ * there the first time we look it up for a given registry build, it wont be
+ * there the second time, or the third time. As we have some subsystems
+ * (database driver loading, form handler callbacks) that check for the
+ * existence of a more specific resource without knowing it exists, caching
+ * the failed lookups saves us database queries.
+ */
+function _registry_check_code($type, $name = NULL) {
+  static $misses, $cache_count = 0;
+
+  // Only write this to cache if we have new registry resource lookup misses.
+  if ($type == REGISTRY_CHECK_CODE_WRITE_CACHE && count($misses) > $cache_count) {
+    cache_set('registry_check_code_misses', $misses, 'cache_registry');
+    return;
+  }
+
+  if (!isset($misses)) {
+    $cache = cache_get('registry_check_code_misses', 'cache_registry');
+    $misses = $cache ? $cache->data : array();
+    $cache_count = count($misses);
+  }
+
+  if (isset($misses[$type . '_' . $name])) {
+    return FALSE;
+  }
+
   $file = db_query("SELECT filename FROM {registry} WHERE name = :name AND type = :type", array(
       ':name' => $name,
       ':type' => $type,
@@ -1457,7 +1488,12 @@ function _registry_check_code($type, $na
     registry_mark_code($type, $name);
     return TRUE;
   }
-  return FALSE;
+  else {
+    // Collect registry lookup misses by name and type, so we can cache them
+    // at the end of the request.
+    $misses[$type . '_' . $name] = TRUE;
+    return FALSE;
+  }
 }
 
 /**
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.814
diff -u -p -r1.814 common.inc
--- includes/common.inc	31 Oct 2008 02:23:24 -0000	1.814
+++ includes/common.inc	31 Oct 2008 22:52:37 -0000
@@ -1610,6 +1610,7 @@ function drupal_page_footer() {
 
   module_implements(MODULE_IMPLEMENTS_WRITE_CACHE);
   registry_cache_path_files();
+  _registry_check_code(REGISTRY_CHECK_CODE_WRITE_CACHE);
 }
 
 /**
