Cache user_load or do something smaller...

Morbus Iff - February 26, 2009 - 23:38
Project:RealName
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

One of my biggest issues with this module is that it does a full user load *for every username* you see on any given page. This is a huge, huge, cost, just to load in "Morbus Iff" - you're calling all the hooks, modules can potentially be making more SQL calls, and so forth. To make matters worse, users are not cached, so if I appear 3 times on a page, the whole thing happens three times. Pretty rough.

You minimize this in some situations, like in hook_nodeapi() where you use a static cache, but not in others (hook_comment() where, bizarrely, you're only caring about the homepage, and not the namesake). Just like Drupal's global $user (representing the browsing visitor) isn't a full user_load(), I hesitate to suggest that retrieving the user's real name probably doesn't require a full user_load() either.

Is there something I'm missing where this doesn't look as bad as it is?

#1

NancyDru - February 27, 2009 - 03:06

I had static caching in there in the beginning but someone said it was wrong; maybe it was at that place. I will certainly look at putting it back in. I will also look at whether or not I can go to a query against the user table now since I load the profile differently now. Originally, the user_load was to force teh core profile table to load stuff as well, but since I put in support for the Content_Profile module, the profile is loaded separately. I just didn't think about going back and changing the user_load. Thank you for reminding me.

Since I saw this post, I've also thought about possibly creating a table with a converted RealName, and keep it up to date with hook_user, but I have to figure out how to do the same for the Content_Profile module (probably hook_nodeapi).

#2

NancyDru - March 3, 2009 - 16:14

Morbus, which version are you looking at?

function realname_make_name(&$account) {
  static $fields, $pattern_saved, $homepage, $use_theme, $type, $module, $profile_privacy;
  static $users = array();
  static $links = array();
  static $edit = array();

  // Return anonymous user right away.
  if ($account->uid == 0) {
    return $account->name;
  }

  if (isset($users[$account->uid])) {
    $account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
    return $users[$account->uid];
  }

If the user has already been seen before on this page request, the static cache should return the cached username.

#3

Morbus Iff - March 3, 2009 - 17:09

I've been talking about hook_nodeapi and hook_comment.

#4

NancyDru - March 3, 2009 - 17:30

I have changed hook_comment to query only three columns from the users table because that's all that are needed. I have also set hook_comment to statically cache the constructed account object. I am still looking at the hook_form_alter where some I call user_load.

Realname_make_name was already statically cached (and is where most of the work is done).

#5

NancyDru - March 3, 2009 - 18:21
Status:active» needs review

Okay, no more user_loads at all.

AttachmentSize
realname_6x_05.patch 3.38 KB

#6

roball - March 4, 2009 - 16:56

Morbus, could you please test if Nancy's patch at #5 solves the problem you've reported?

#7

NancyDru - March 4, 2009 - 20:19

I will commit this in 3 hours if I have not heard otherwise because I have another feature I need to get added.

#8

NancyDru - March 6, 2009 - 20:11
Status:needs review» fixed

Committed to 6.x only.

#9

NancyDru - March 18, 2009 - 13:42
Status:fixed» closed
 
 

Drupal is a registered trademark of Dries Buytaert.