Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.165
diff -u -p -r1.165 module.inc
--- includes/module.inc	5 Nov 2009 16:19:25 -0000	1.165
+++ includes/module.inc	7 Nov 2009 03:48:58 -0000
@@ -69,21 +69,10 @@ function module_list($refresh = FALSE, $
       // locations in the file system. The ordering here must also be
       // consistent with the one used in module_implements().
       if ($bootstrap) {
-        $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, name ASC");
+        $list = system_list('bootstrap');
       }
       else {
-        $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, name ASC");
-      }
-      foreach ($result as $module) {
-        if (file_exists($module->filename)) {
-          // First call drupal_get_filename() to prime the static cache for
-          // later lookups of the module path. Since we've already queried for
-          // the filename and can pass that in as an argument, this avoids a
-          // database hit for every module when drupal_get_filename() is
-          // subsequently called by drupal_load().
-          drupal_get_filename('module', $module->name, $module->filename);
-          $list[$module->name] = $module->name;
-        }
+        $list = system_list('module');
       }
     }
   }
@@ -98,6 +87,51 @@ function module_list($refresh = FALSE, $
 }
 
 /**
+ * Build a list of bootstrap modules and enabled modules and themes.
+ *
+ * @param $type
+ *   The type of list to return, either 'module', 'bootstrap', or 'theme'.
+ *
+ * @return
+ *   An associative array of modules or themes, keyed by name, with the minimum
+ *   data required to bootstrap.
+ *
+ * @see module_list()
+ * @see list_themes()
+ */
+function system_list($type) {
+  $lists = &drupal_static(__FUNCTION__);
+
+  if (!isset($lists)) {
+    $lists = array('bootstrap' => array(), 'module' => array(), 'theme' => array());
+    $result = db_query("SELECT * FROM {system} WHERE status = 1 AND type IN ('module', 'theme') ORDER BY weight ASC, name ASC");
+    foreach ($result as $record) {
+      // Build a list of all enabled modules.
+      if ($record->type == 'module') {
+        $lists['module'][$record->name] = $record->name;
+        // Build a separate array of modules required for bootstrap.
+        if ($record->bootstrap) {
+          $lists['bootstrap'][$record->name] = $record->name;
+        }
+      }
+      // Build a list of enabled themes.
+      if ($record->type == 'theme') {
+        $lists['theme'][$record->name] = $record;
+      }
+
+      // Additionally prime drupal_get_filename() with the filename and type
+      // for each record, this prevents subsequent database lookups when
+      // drupal_get_filename() is called without the 'file' argument.
+      if (file_exists($record->filename)) {
+        drupal_get_filename($record->type, $record->name, $record->filename);
+      }
+    }
+  }
+
+  return $lists[$type];
+}
+
+/**
  * Find dependencies any level deep and fill in required by information too.
  *
  * @param $files
@@ -236,6 +270,7 @@ function module_enable($module_list, $di
 
   if (!empty($invoke_modules)) {
     // Refresh the module list to exclude the disabled modules.
+    drupal_static_reset('system_list');
     module_list(TRUE);
     module_implements('', FALSE, TRUE);
     // Force to regenerate the stored list of hook implementations.
@@ -296,6 +331,7 @@ function module_disable($module_list) {
 
   if (!empty($invoke_modules)) {
     // Refresh the module list to exclude the disabled modules.
+    drupal_static_reset('system_list');
     module_list(TRUE);
     module_implements('', FALSE, TRUE);
     // Invoke hook_modules_disabled before disabling modules,
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.548
diff -u -p -r1.548 theme.inc
--- includes/theme.inc	5 Nov 2009 16:19:25 -0000	1.548
+++ includes/theme.inc	7 Nov 2009 03:48:59 -0000
@@ -562,6 +562,7 @@ function list_themes($refresh = FALSE) {
 
   if ($refresh) {
     $list = array();
+    drupal_static_reset('system_list');
   }
 
   if (empty($list)) {
@@ -570,8 +571,7 @@ function list_themes($refresh = FALSE) {
     // Extract from the database only when it is available.
     // Also check that the site is not in the middle of an install or update.
     if (db_is_active() && !defined('MAINTENANCE_MODE')) {
-      $result = db_query("SELECT * FROM {system} WHERE type = :theme", array(':theme' => 'theme'));
-      foreach ($result as $theme) {
+      foreach (system_list('theme') as $theme) {
         if (file_exists($theme->filename)) {
           $theme->info = unserialize($theme->info);
           $themes[] = $theme;
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.415
diff -u -p -r1.415 system.install
--- modules/system/system.install	24 Oct 2009 23:19:21 -0000	1.415
+++ modules/system/system.install	7 Nov 2009 03:49:00 -0000
@@ -1567,8 +1567,7 @@ function system_schema() {
     ),
     'primary key' => array('filename'),
     'indexes' => array(
-      'modules' => array('type', 'status', 'weight', 'name'),
-      'bootstrap' => array('type', 'status', 'bootstrap', 'weight', 'name'),
+      'system_list' => array('status', 'weight', 'name'),
       'type_name' => array('type', 'name'),
     ),
   );
@@ -2196,14 +2195,15 @@ function system_update_7017() {
 }
 
 /**
- * Shorten the {system}.type column and add an index on type and name.
+ * Shorten the {system}.type column and modify indexes.
  */
 function system_update_7018() {
   db_drop_index('system', 'modules');
   db_drop_index('system', 'type_name');
+  db_drop_index('system', 'bootstrap');
   db_change_field('system', 'type', 'type', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''));
-  db_add_index('system', 'modules', array('type', 'status', 'weight', 'name'));
   db_add_index('system', 'type_name', array('type', 'name'));
+  db_add_index('system', 'system_list', array('status', 'weight', 'name'));
 }
 
 /**
