Attached you can find a patch for a lite performance improvement for menu_get_item(). menu_get_item() is frequently called.

Following code has been moved just before db_query_range():

$part = array_slice($original_map, 0, MENU_MAX_PARTS));
$ancestors = menu_get_ancestors($parts);

Resulting following code:

function menu_get_item($path = NULL, $router_item = NULL) {
  $router_items = &drupal_static(__FUNCTION__);
  if (!isset($path)) {
    $path = $_GET['q'];
  }
  if (isset($router_item)) {
    $router_items[$path] = $router_item;
  }
  if (!isset($router_items[$path])) {
    $original_map = arg(NULL, $path);
    
    // Since there is no limit to the length of $path, use a hash to keep it
    // short yet unique.
    $cid = 'menu_item:' . hash('sha256', $path);
    if ($cached = cache_get($cid, 'cache_menu')) {
      $router_item = $cached->data;
    }
    else {
      $ancestors = menu_get_ancestors(array_slice($original_map, 0, MENU_MAX_PARTS));
      $router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc();
      cache_set($cid, $router_item, 'cache_menu');
    }

 [...]
CommentFileSizeAuthor
menu_get_item_101107.patch1.12 KBjpsoto

Comments

jpsoto’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, menu_get_item_101107.patch, failed testing.

jpsoto’s picture

Status: Needs work » Closed (duplicate)