=== modified file 'includes/path.inc'
--- includes/path.inc	2007-03-26 01:32:22 +0000
+++ includes/path.inc	2007-04-05 02:22:48 +0000
@@ -45,12 +45,12 @@ function drupal_init_path() {
  */
 function drupal_lookup_path($action, $path = '', $path_language = '') {
   global $language;
-  // $map is an array with language keys, holding arrays of Drupal paths to alias relations
+  // $map is an array with language keys, holding arrays of Drupal paths to alias relations.
   static $map = array(), $no_src = array(), $count;
 
   $path_language = $path_language ? $path_language : $language->language;
 
-  // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
+  // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases.
   if (!isset($count)) {
     $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
   }
@@ -60,19 +60,33 @@ function drupal_lookup_path($action, $pa
     $no_src = array();
   }
   elseif ($count > 0 && $path != '') {
+    static $optimize_type, $blacklist, $whitelist; 
+    
+    // Check if we should use the blacklist, the whitelist, or none of both.
+    if (!isset($optimize_type)) {
+      $optimize_type = variable_get('path_optimization_type', 2);
+      if ($optimize_type == 0 && !isset($blacklist)) {
+          $blacklist = variable_get('path_optimization_blacklist', "admin*\n*/add*\*/edit\n*/outline\n*/revisions\n*/track\nadmin\nadmin/*\nbook\ncomment/*\nfilter\nfilter/*\nlogout\npoll\nprofile\ntracker*\nuser\nuser/register\nuser/login");
+      }
+      elseif ($optimize_type == 1 && !isset($whitelist)) {
+          $whitelist = variable_get('path_optimization_whitelist', '');
+      }
+    }
+
     if ($action == 'alias') {
       if (isset($map[$path_language][$path])) {
         return $map[$path_language][$path];
       }
-      // 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));
+      // Get the most fitting result falling back with alias without language.
+      $query = "SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC";
+      $alias = ($optimize_type < 2 || drupal_path_match($path, $whitelist) == $optimize_type) ? db_result(db_query($query, $path, $path_language)) : FALSE;
       $map[$path_language][$path] = $alias;
       return $alias;
     }
     // Check $no_src for this $path in case we've already determined that there
-    // isn't a path that has this alias
+    // isn't a path that has this alias.
     elseif ($action == 'source' && !isset($no_src[$path_language][$path])) {
-      // Look for the value $path within the cached $map
+      // Look for the value $path within the cached $map.
       $src = '';
       if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
         // Get the most fitting result falling back with alias without language
@@ -222,3 +236,23 @@ function drupal_is_front_page() {
   // we can check it against the 'site_frontpage' variable.
   return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
 }
+
+/**
+ * Check if a path matches a set of patterns.
+ *
+ * @param $path
+ *   The path to match.
+ * @param $patterns
+ *   String containing a set of patterns separated by \n, \r or \r\n.
+ *
+ * @return
+ *   Boolean value: TRUE if the path matches a pattern, FALSE if otherwise.
+ */
+function drupal_path_match($path, $patterns) {
+  static $regexps;
+  
+  if (!isset($regexps[$patterns])) {
+    $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
+  }
+  return preg_match($regexps[$patterns], $path);
+}

=== modified file 'modules/block/block.module'
--- modules/block/block.module	2007-03-27 05:13:53 +0000
+++ modules/block/block.module	2007-04-05 02:20:07 +0000
@@ -691,11 +691,10 @@ function block_list($region) {
       if ($block->pages) {
         if ($block->visibility < 2) {
           $path = drupal_get_path_alias($_GET['q']);
-          $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
           // Compare with the internal and path alias (if any).
-          $page_match = preg_match($regexp, $path);
+          $page_match = drupal_path_match($path, $block->pages);
           if ($path != $_GET['q']) {
-            $page_match = $page_match || preg_match($regexp, $_GET['q']);
+            $page_match = $page_match || drupal_path_match($_GET['q'], $block->pages);
           }
           // When $block->visibility has a value of 0, the block is displayed on
           // all pages except those listed in $block->pages. When set to 1, it

=== modified file 'modules/path/path.module'
--- modules/path/path.module	2007-03-27 05:13:53 +0000
+++ modules/path/path.module	2007-04-05 02:20:07 +0000
@@ -27,7 +27,7 @@ function path_help($section) {
       return $output;
     case 'admin/build/path':
     case 'admin/build/path/list':
-      return '<p>'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'</p>';
+      return '<p>'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'</p><p>'. t('Optimization via white/blacklisting URL alias lookups is available on the <a href="@link">performance settings</s> page.', array('@link' => url('admin/settings/performance'))) .'</p>';
     case 'admin/build/path/add':
       return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>';
   }

=== modified file 'modules/system/system.module'
--- modules/system/system.module	2007-03-27 05:13:53 +0000
+++ modules/system/system.module	2007-04-05 02:20:07 +0000
@@ -707,6 +707,46 @@ function system_performance_settings() {
 
   $form['#submit']['system_settings_form_submit'] = array();
   $form['#submit']['drupal_clear_css_cache'] = array();
+  
+  // If path.module is enabled, allow for URL alias optimization via black/whitelist
+  if (module_exists('path')) {
+    $type = variable_get('path_optimization_type', 2);
+
+    $form['optimize'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Path optimization settings'),
+      '#collapsible' => false,
+      '#collapsed' => false,
+    );
+    $form['optimize']['path_optimization_type'] = array(
+      '#type' => 'radios',
+      '#title' => t('Optimization type'),
+      '#default_value' => variable_get('path_optimization_type', 2),
+      '#options' => array(t('Blacklist of Drupal paths'), t('Whitelist of Drupal paths'), t('No optimization')),
+      '#description' => t("Blacklist: Drupal will only look up aliases for paths that don't match paths in the blacklist.<br />Whitelist: Drupal paths must match any of the paths listed in the whitelist, otherwise Drupal won't look up the alias.<br />No optimization: Drupal will look up aliases for any Drupal path."),
+      '#prefix' => t('It is recommended to enable either the blacklist or the whitelist, and to configure it correctly. This will reduce the numer of database queries, saving system resources! For more information see <a href="http://drupal.org/node/133850">the handbook</a>.'),
+    );
+    switch ($type) {
+      case 0:
+        $form['optimize']['path_optimization_blacklist'] = array(
+          '#type' => 'textarea',
+          '#title' => t('Blacklist'),
+          '#default_value' => variable_get('path_optimization_blacklist', "admin*\n*/add*\*/edit\n*/outline\n*/revisions\n*/track\nadmin\nadmin/*\nbook\ncomment/*\nfilter\nfilter/*\nlogout\npoll\nprofile\ntracker*\nuser\nuser/register\nuser/login"),
+          '#rows' => 10,
+          '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",  array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
+        );    
+        break;
+      case 1:
+        $form['optimize']['path_optimization_whitelist'] = array(
+          '#type' => 'textarea',
+          '#title' => t('Whitelist'),
+          '#default_value' => variable_get('path_optimization_whitelist', ''),
+          '#rows' => 10,
+          '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",  array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
+        );    
+        break;
+    }
+  }
 
   return system_settings_form($form);
 }

