? path.patch
Index: includes/path.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/path.inc,v
retrieving revision 1.4.2.4
diff -u -p -r1.4.2.4 path.inc
--- includes/path.inc	13 Sep 2006 07:10:51 -0000	1.4.2.4
+++ includes/path.inc	20 Dec 2006 18:31:01 -0000
@@ -39,42 +39,46 @@ function drupal_init_path() {
  *   found.
  */
 function drupal_lookup_path($action, $path = '') {
-  static $map = array();
+  // $map keys are Drupal paths and the values are the corresponding aliases
+  static $map = array(), $no_src = array();
   static $count = NULL;
 
+  // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
   if ($count === NULL) {
     $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
   }
 
   if ($action == 'wipe') {
     $map = array();
+    $no_src = array();
   }
   elseif ($count > 0 && $path != '') {
     if ($action == 'alias') {
       if (isset($map[$path])) {
         return $map[$path];
       }
-      if ($alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path))) {
-        $map[$path] = $alias;
-        return $alias;
-      }
-      else {
-        $map[$path] = $path;
+      if (!$alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path))) {
+        $alias = FALSE;
       }
+      $map[$path] = $alias;
+      return $alias;
     }
-    elseif ($action == 'source') {
-      if ($alias = array_search($path, $map)) {
-        return $alias;
-      }
-      if (!isset($map[$path])) {
+    // Check $no_src for this $path in case we've already determined that there
+    // isn't a path that has this alias
+    elseif ($action == 'source' && !isset($no_src[$path])) {
+      // Look for the value $path within the cached $map
+      if (!$src = array_search($path, $map)) {
         if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
           $map[$src] = $path;
-          return $src;
         }
         else {
-          $map[$path] = FALSE;
+          // We can't record anything into $map because we do not have a valid
+          // index and there is no need because we have not learned anything
+          // about any Drupal path. Thus cache to $no_src.
+          $no_src[$path] = TRUE;
         }
       }
+      return $src;
     }
   }
 
