--- includes/path.inc	2009-05-05 10:53:21.000000000 -0400
+++ includes/path.inc.orig	2009-05-05 11:05:51.000000000 -0400
@@ -59,12 +59,32 @@ function drupal_lookup_path($action, $pa
     $map = array();
     $no_src = array();
     $count = NULL;
+
+    $whitelist =  drupal_rebuild_whitelist();
+    // Set the variable so we pull
+    variable_set('path_alias_whitelist', $whitelist);
   }
   elseif ($count > 0 && $path != '') {
+    $whitelist = variable_get('path_alias_whitelist', array());
+    if (count($whitelist) == 0) {
+      // Load the whitelist
+      $whitelist =  drupal_rebuild_whitelist();
+      // Set the variable so we pull
+      variable_set('path_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') {
       if (isset($map[$path_language][$path])) {
         return $map[$path_language][$path];
       }
+      // Check the whitelist, if the top_level is not in it, then
+      // no need to do anything further, it is not in the database
+      if (!isset($whitelist[$top_level])) {
+        return FALSE;
+      }
       // Get the most fitting result falling back with alias without language
       $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language));
       $map[$path_language][$path] = $alias;
@@ -241,3 +261,21 @@ function drupal_match_path($path, $patte
   }
   return preg_match($regexps[$patterns], $path);
 }
+
+/**
+ * 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;
+}
