Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.1008
diff -u -p -r1.1008 node.module
--- modules/node/node.module	31 Dec 2008 12:02:22 -0000	1.1008
+++ modules/node/node.module	3 Jan 2009 12:59:50 -0000
@@ -2132,15 +2147,24 @@ function node_search_validate($form, &$f
  * @param $account
  *   Optional, a user object representing the user for whom the operation is to
  *   be performed. Determines access for a user other than the current user.
+ * @param $reset
+ *   Whether to reset the internal node_access cache.
+ *
  * @return
  *   TRUE if the operation may be performed.
  */
-function node_access($op, $node, $account = NULL) {
+function node_access($op, $node, $account = NULL, $reset = NULL) {
   global $user;
+  static $count, $rights = array();
 
   if (!$node) {
     return FALSE;
   }
+
+  if ($reset) {
+    $rights = array();
+  }
+
   // Convert the node to an object if necessary:
   if ($op != 'create') {
     $node = (object)$node;
@@ -2149,6 +2173,11 @@ function node_access($op, $node, $accoun
   if (empty($account)) {
     $account = $user;
   }
+  // Returned cached value if it exists.
+  if (isset($node->nid) && isset($rights[$account->uid][$node->nid][$op])) {
+    return $rights[$account->uid][$node->nid][$op];
+  }
+
   // If the node is in a restricted format, disallow editing.
   if ($op == 'update' && !filter_access($node->format)) {
     return FALSE;
@@ -2167,12 +2196,21 @@ function node_access($op, $node, $accoun
   $base = node_get_types('base', $node);
   $access = module_invoke($base, 'access', $op, $node, $account);
   if (!is_null($access)) {
+    if (isset($node->nid)) {
+      $rights[$account->uid][$node->nid][$op] = $access;
+    }
     return $access;
   }
 
+  // Use $count to avoid looking up access rights if there is only the
+  // standard Drupal access.
+  if (!isset($count)) {
+    $count = db_query('SELECT COUNT(*) FROM {node_access}')->fetchField();
+  }
+
   // If the module did not override the access rights, use those set in the
   // node_access table.
-  if ($op != 'create' && $node->nid && $node->status) {
+  if ($count > 1 && $op != 'create' && $node->nid && $node->status) {
     $grants = array();
     foreach (node_access_grants($op, $account) as $realm => $gids) {
       foreach ($gids as $gid) {
@@ -2186,12 +2224,14 @@ function node_access($op, $node, $accoun
     }
 
     $sql = "SELECT COUNT(*) FROM {node_access} WHERE (nid = 0 OR nid = %d) $grants_sql AND grant_$op >= 1";
-    $result = db_query($sql, $node->nid);
-    return (db_result($result));
+    $result = (db_result(db_query($sql, $node->nid)));
+    $rights[$account->uid][$node->nid][$op] = $result;
+    return $result;
   }
 
   // Let authors view their own nodes.
   if ($op == 'view' && $account->uid == $node->uid && $account->uid != 0) {
+    $rights[$account->uid][$node->nid][$op] = TRUE;
     return TRUE;
   }
 
