=== modified file 'includes/common.inc'
--- includes/common.inc	2010-07-24 17:28:25 +0000
+++ includes/common.inc	2010-07-25 06:59:45 +0000
@@ -4940,6 +4940,13 @@ function drupal_pre_render_markup($eleme
 function drupal_render_page($page) {
   $main_content_display = &drupal_static('system_main_content_added', FALSE);
 
+  // menu_execute_active_handler() initializes the theme before invoking the
+  // page callback, but it's possible that drupal_render_page() is called for
+  // an error page (MENU_NOT_FOUND, MENU_ACCESS_DENIED) or during hook_init().
+  // Even in these situations, the theme may implement hook_page_alter() and
+  // other alter hooks that get called during hook_page_build().
+  drupal_theme_initialize();
+
   // Allow menu callbacks to return strings or arbitrary arrays to render.
   // If the array returned is not of #type page directly, we need to fill
   // in the page with defaults.

=== modified file 'includes/menu.inc'
--- includes/menu.inc	2010-07-19 21:57:49 +0000
+++ includes/menu.inc	2010-07-25 06:59:45 +0000
@@ -473,6 +473,12 @@ function menu_execute_active_handler($pa
         if ($router_item['include_file']) {
           require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
         }
+        // Initialize the theme so that its hook_*_alter() implementations get
+        // called during page callback execution, even ahead of when rendering
+        // starts. Retrieving the theme registry, even from cache, is slow, so
+        // we delay that until it is needed, so that pages that don't require
+        // anything to be themed (e.g., */autocomplete) are served faster.
+        drupal_theme_initialize(FALSE);
         $page_callback_result = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
       }
       else {

=== modified file 'includes/theme.inc'
--- includes/theme.inc	2010-07-25 02:45:27 +0000
+++ includes/theme.inc	2010-07-25 07:26:35 +0000
@@ -67,12 +67,26 @@ function _drupal_theme_access($theme) {
 
 /**
  * Initialize the theme system by loading the theme.
+ *
+ * @param $load_registry
+ *   When set to FALSE, the theme registry is not loaded, but the theme name is
+ *   set, stylesheets and scripts are loaded and the engine is initialized.
+ *   This saves loading and unserializing a huge array.
  */
-function drupal_theme_initialize() {
+function drupal_theme_initialize($load_registry = TRUE) {
   global $theme, $user, $theme_key;
 
+  // Not drupal_static(), because the theme system does not support
+  // uninitializing an initialized theme or initializing a second theme during
+  // the same page request.
+  static $registry_loaded = FALSE;
+
   // If $theme is already set, assume the others are set, too, and do nothing
   if (isset($theme)) {
+    if (!$registry_loaded && $load_registry) {
+      $registry_loaded = TRUE;
+      _theme_load_registry($GLOBALS['theme_info'], $GLOBALS['base_theme_info'], $GLOBALS['theme_engine']);
+    }
     return;
   }
 
@@ -98,7 +112,17 @@ function drupal_theme_initialize() {
     $base_theme[] = $new_base_theme = $themes[$themes[$ancestor]->base_theme];
     $ancestor = $themes[$ancestor]->base_theme;
   }
-  _drupal_theme_initialize($themes[$theme], array_reverse($base_theme));
+
+  // Allow the loading of the registry to be delayed.
+  if (!$registry_loaded && $load_registry) {
+    $registry_loaded = TRUE;
+    $registry_callback = '_theme_load_registry';
+  }
+  else {
+    $registry_callback = NULL;
+  }
+
+  _drupal_theme_initialize($themes[$theme], array_reverse($base_theme), $registry_callback);
 
   // Themes can have alter functions, so reset the drupal_alter() cache.
   drupal_static_reset('drupal_alter');
@@ -1999,7 +2023,7 @@ function theme_indentation($variables) {
 
 /**
  * Returns HTML output for a single table cell for theme_table().
- * 
+ *
  * @param $cell
  *   Array of cell information, or string to display in cell.
  * @param bool $header

=== modified file 'modules/simpletest/tests/theme.test'
--- modules/simpletest/tests/theme.test	2010-04-29 05:22:06 +0000
+++ modules/simpletest/tests/theme.test	2010-07-25 07:13:23 +0000
@@ -182,3 +182,30 @@ class ThemeHookInitUnitTest extends Drup
     $this->assertRaw('garland/style.css', t("The default theme's CSS appears on the page when the theme system is initialized in hook_init()."));
   }
 }
+
+/**
+ * Tests autocompletion not loading registry.
+ */
+class ThemeFastTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Theme fast',
+      'description' => 'Test autocompletion does not load the registry.',
+      'group' => 'Theme'
+    );
+  }
+
+  function setUp() {
+    parent::setUp('theme_test');
+    $this->account = $this->drupalCreateUser(array('access user profiles'));
+  }
+
+  /**
+   * Tests access to user autocompletion and verify the correct results.
+   */
+  function testUserAutocomplete() {
+    $this->drupalLogin($this->account);
+    $this->drupalGet('user/autocomplete/' . $this->account->name);
+    $this->assertText('registry not initialized', t('The registry was not initialized'));
+  }
+}

=== modified file 'modules/simpletest/tests/theme_test.module'
--- modules/simpletest/tests/theme_test.module	2010-04-29 05:22:06 +0000
+++ modules/simpletest/tests/theme_test.module	2010-07-25 07:08:54 +0000
@@ -24,15 +24,26 @@ function theme_test_menu() {
  * Implements hook_init().
  */
 function theme_test_init() {
-  // First, force the theme registry to be rebuilt on this page request. This
-  // allows us to test a full initialization of the theme system in the code
-  // below.
-  drupal_theme_rebuild();
-  // Next, initialize the theme system by storing themed text in a global
-  // variable. We will use this later in theme_test_hook_init_page_callback()
-  // to test that even when the theme system is initialized this early, it is
-  // still capable of returning output and theming the page as a whole.
-  $GLOBALS['theme_test_output'] = theme('more_link', array('url' => url('user'), 'title' => 'Themed output generated in hook_init()'));
+  if (arg(0) == 'theme-test') {
+    // First, force the theme registry to be rebuilt on this page request. This
+    // allows us to test a full initialization of the theme system in the code
+    // below.
+    drupal_theme_rebuild();
+    // Next, initialize the theme system by storing themed text in a global
+    // variable. We will use this later in theme_test_hook_init_page_callback()
+    // to test that even when the theme system is initialized this early, it is
+    // still capable of returning output and theming the page as a whole.
+    $GLOBALS['theme_test_output'] = theme('more_link', array('url' => url('user'), 'title' => 'Themed output generated in hook_init()'));
+  }
+}
+
+/**
+ * Implements hook_exit().
+ */
+function theme_test_exit() {
+  if (arg(0) == 'user') {
+    print theme_get_registry() ? 'registry initialized' : 'registry not initialized';
+  }
 }
 
 /**

