I have seen different options to make drupal_lookup_path() method faster, but no one was fast enough for me. On my hosting, Dreamhost currently, DB queries take too long when server has a lot of visits and simple queries as looking for a path take between 10 and 40 miliseconds, which is a lot if you have to check hundreds of paths.

Since my site doesn't have a high number of url aliases (currently less than 100) I have modified the code to load the whole table the first time this method is called, so subsequent calls won't go to DB at all.

Based on Devel module, I have reduced the amount of queries from 300 to 91 on an average forum page. This means the same page is being generated now in less than one second instead of 3 or 4 seconds it was taking before.

Here is the code:

function drupal_lookup_path($action, $path = '') {
  static $map = array();

  if ($action == 'wipe')
  {
    $map = array();
    return FALSE;
  }

  if ($path != '')
  {
    // Initial load from DB
    if (count($map) == 0) {
    	$result = db_query("SELECT src, dst FROM {url_alias}");
    	while ($url = db_fetch_object($result)) {
    		$map[$url->src] = $url->dst;
    		$map[$url->dst] = $url->src;
    	}
    }
    // $action == 'alias' or $action == 'source'
    if (isset($map[$path])) return $map[$path];
  }

  return FALSE;
}

I load the array in both ways, from src to dst and dst to src which means the array will have twice the records your alias table has.

BTW, my website is www.spaniards.es.

Comments

moshe weitzman’s picture

Version: 4.7.x-dev » 7.x-dev
Status: Active » Closed (won't fix)

4.6 drupal did "all in one array" and it was very slow after a couple thousand aliases. we need something in the middle.

treksler’s picture

Status: Closed (won't fix) » Active

um so
why not (optionally) use this method in drupal 5/6 if url alias count is less than 2000???

kbahey’s picture

There are patches in the queue that does the threshold thing.

There is another that does a white list approach (does not attempt to lookup in the database, unless the path is in the white list).

Please search the queue for those patches and test them.

Also, for Drupal 5 and 6, it is too late. If we can agree on something it has a chance to go in D7.

dave reid’s picture

Status: Active » Closed (won't fix)

Much better patch landed in D7 for drupal_lookup_path: #456824: drupal_lookup_path() speedup - cache system paths per page.. Marking this as won't fix.