Perf: don't allocate hashes OVER and OVER in a loop, its bad

This commit is contained in:
Sam 2013-09-09 16:02:13 +10:00
parent c59c2a4bb3
commit 030d2260a7

View file

@ -11,11 +11,28 @@ class AvatarLookup
private
def self.lookup_columns
@lookup_columns ||= [:id,
:email,
:username,
:use_uploaded_avatar,
:uploaded_avatar_template,
:uploaded_avatar_id]
end
def users
@users ||= User.where(:id => @user_ids)
.select([:id, :email, :username, :use_uploaded_avatar, :uploaded_avatar_template, :uploaded_avatar_id])
.inject({}) do |hash, user|
hash.merge({user.id => user})
end
@users ||= user_lookup_hash
end
def user_lookup_hash
# adding tap here is a personal taste thing
hash = {}
User
.where(:id => @user_ids)
.select(AvatarLookup.lookup_columns)
.each{|user|
hash[user.id] = user
}
hash
end
end