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 private
def self.lookup_columns
@lookup_columns ||= [:id,
:email,
:username,
:use_uploaded_avatar,
:uploaded_avatar_template,
:uploaded_avatar_id]
end
def users def users
@users ||= User.where(:id => @user_ids) @users ||= user_lookup_hash
.select([:id, :email, :username, :use_uploaded_avatar, :uploaded_avatar_template, :uploaded_avatar_id]) end
.inject({}) do |hash, user|
hash.merge({user.id => user}) def user_lookup_hash
end # 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
end end