Index: includes/path.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/path.inc,v
retrieving revision 1.37
diff -u -F^f -r1.37 path.inc
--- includes/path.inc	26 May 2009 09:12:28 -0000	1.37
+++ includes/path.inc	30 May 2009 02:33:54 -0000
@@ -66,8 +66,23 @@ function drupal_lookup_path($action, $pa
     $count = NULL;
     $system_paths = array();
     $no_aliases = array();
+
+    $whitelist =  drupal_rebuild_whitelist();
+    // Set the variable so we pull
+    variable_set('alias_whitelist', $whitelist);
   }
   elseif ($count > 0 && $path != '') {
+    $whitelist = variable_get('alias_whitelist', array());
+    if (count($whitelist) == 0) {
+      // Load the whitelist
+      $whitelist =  drupal_rebuild_whitelist();
+      // Set the variable so we pull
+      variable_set('alias_whitelist', $whitelist);
+    }
+    // And derive the top level component of the path
+    $pos = strpos($path, '/');
+    $top_level = ($pos) ? substr($path, 0, $pos) : $path;
+
     if ($action == 'alias') {
       // During the first call to drupal_lookup_path() per language, load the
       // expected system paths for the page from cache.
@@ -93,6 +108,11 @@ function drupal_lookup_path($action, $pa
       if (isset($map[$path_language][$path])) {
         return $map[$path_language][$path];
       }
+      elseif (!isset($whitelist[$top_level])) {
+      // Check the whitelist, if the top_level is not in it, then
+      // no need to do anything further, it is not in the database
+        return FALSE;
+      }
       // For system paths which were not cached, query aliases individually.
       else if (!isset($no_aliases[$path_language][$path])) {
         // Get the most fitting result falling back with alias without language
@@ -347,3 +367,21 @@ function drupal_match_path($path, $patte
 function current_path() {
   return $_GET['q'];
 }
+
+/**
+ * Rebuild a white list of top level paths, depending on what
+ * is stored in the url_alias table for this site.
+ *
+ */
+function drupal_rebuild_whitelist() {
+  $whitelist = array();
+
+  // For each alias in the database, get the top level (i.e. the portion before the first /).
+  // Using GROUP BY is faster than DISTINCT, at least for MyISAM.
+  $result = db_query("SELECT SUBSTRING_INDEX(src, '/', 1) AS path FROM {url_alias} GROUP BY path");
+  while ($row = db_fetch_object($result)) {
+    $whitelist[$row->path] = TRUE;
+  }
+
+  return $whitelist;
+}
