Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1201
diff -u -p -r1.1201 common.inc
--- includes/common.inc	5 Aug 2010 08:36:08 -0000	1.1201
+++ includes/common.inc	5 Aug 2010 16:13:53 -0000
@@ -4577,10 +4577,17 @@ function _drupal_bootstrap_full() {
 
   // Initialize $_GET['q'] prior to invoking hook_init().
   drupal_path_initialize();
+
   // Set a custom theme for the current page, if there is one. We need to run
   // this before invoking hook_init(), since any modules which initialize the
   // theme system will prevent a custom theme from being correctly set later.
   menu_set_custom_theme();
+
+  // Initialize the theme so that its hook_*_alter() implementations get
+  // called during page callback execution, even ahead of when rendering
+  // starts.
+  drupal_theme_initialize();
+
   // Let all modules take action before menu system handles the request
   // We do not want this while running update.php.
   if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.604
diff -u -p -r1.604 theme.inc
--- includes/theme.inc	30 Jul 2010 02:47:27 -0000	1.604
+++ includes/theme.inc	5 Aug 2010 16:13:53 -0000
@@ -221,8 +221,8 @@ function _drupal_theme_initialize($theme
     }
   }
 
-  if (function_exists($registry_callback)) {
-    $registry_callback($theme, $base_theme, $theme_engine);
+  if (isset($registry_callback)) {
+    _theme_registry_callback($registry_callback, array($theme, $base_theme, $theme_engine));
   }
 }
 
@@ -233,29 +233,33 @@ function _drupal_theme_initialize($theme
  *   The theme registry array if it has been stored in memory, NULL otherwise.
  */
 function theme_get_registry() {
-  return _theme_set_registry();
-}
-
-/**
- * Store the theme registry in memory.
- *
- * @param $registry
- *   A registry array as returned by _theme_build_registry()
- *
- * @return
- *   The theme registry array stored in memory
- */
-function _theme_set_registry($registry = NULL) {
   static $theme_registry = NULL;
 
-  if (isset($registry)) {
-    $theme_registry = $registry;
+  if (!isset($theme_registry)) {
+    list($callback, $arguments) = _theme_registry_callback();
+    $theme_registry = call_user_func_array($callback, $arguments);
   }
 
   return $theme_registry;
 }
 
 /**
+ * Set the callback that will be used by theme_get_registry() to fetch the registry.
+ *
+ * @param $callback
+ *   The name of the callback function.
+ * @param $arguments
+ *   The arguments to pass to the function.
+ */
+function _theme_registry_callback($callback = NULL, array $arguments = array()) {
+  static $stored;
+  if (isset($callback)) {
+    $stored = array($callback, $arguments);
+  }
+  return $stored;
+}
+
+/**
  * Get the theme_registry cache from the database; if it doesn't exist, build it.
  *
  * @param $theme
@@ -271,7 +275,6 @@ function _theme_load_registry($theme, $b
   $cache = cache_get("theme_registry:$theme->name", 'cache');
   if (isset($cache->data)) {
     $registry = $cache->data;
-    _theme_set_registry($registry);
   }
   else {
     // If not, build one and cache it.
@@ -280,9 +283,9 @@ function _theme_load_registry($theme, $b
    // complete set of theme hooks.
     if (module_load_all(NULL)) {
       _theme_save_registry($theme, $registry);
-      _theme_set_registry($registry);
     }
   }
+  return $registry;
 }
 
 /**
Index: includes/theme.maintenance.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.maintenance.inc,v
retrieving revision 1.63
diff -u -p -r1.63 theme.maintenance.inc
--- includes/theme.maintenance.inc	8 Jul 2010 03:41:26 -0000	1.63
+++ includes/theme.maintenance.inc	5 Aug 2010 16:13:54 -0000
@@ -88,8 +88,7 @@ function _drupal_maintenance_theme() {
  * This builds the registry when the site needs to bypass any database calls.
  */
 function _theme_load_offline_registry($theme, $base_theme = NULL, $theme_engine = NULL) {
-  $registry = _theme_build_registry($theme, $base_theme, $theme_engine);
-  _theme_set_registry($registry);
+  return _theme_build_registry($theme, $base_theme, $theme_engine);
 }
 
 /**
Index: modules/simpletest/tests/theme.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/theme.test,v
retrieving revision 1.16
diff -u -p -r1.16 theme.test
--- modules/simpletest/tests/theme.test	29 Apr 2010 05:22:06 -0000	1.16
+++ modules/simpletest/tests/theme.test	5 Aug 2010 16:13:54 -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 initialization',
+      'description' => 'Test that 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'));
+  }
+}
Index: modules/simpletest/tests/theme_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/theme_test.module,v
retrieving revision 1.3
diff -u -p -r1.3 theme_test.module
--- modules/simpletest/tests/theme_test.module	1 Aug 2010 00:18:20 -0000	1.3
+++ modules/simpletest/tests/theme_test.module	5 Aug 2010 16:13:54 -0000
@@ -24,15 +24,36 @@ 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' => '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' => 'user', 'title' => 'Themed output generated in hook_init()'));
+  }
+}
+
+/**
+ * Implements hook_exit().
+ */
+function theme_test_exit() {
+  if (arg(0) == 'user') {
+    // Register a fake registry loading callback. If it gets called by
+    // theme_get_registry(), the registry has not been initialized yet.
+    _theme_registry_callback('_theme_test_load_registry', array());
+    print theme_get_registry() ? 'registry initialized' : 'registry not initialized';
+  }
+}
+
+/**
+ * Fake registry loading callback.
+ */
+function _theme_test_load_registry() {
+  return array();
 }
 
 /**
