OSDN Git Service

shogi_server/league.rb: Improved performance of Persistance#get_players.
authordaigo <beatles@users.sourceforge.jp>
Sun, 11 Jul 2010 09:34:23 +0000 (18:34 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 11 Jul 2010 09:36:23 +0000 (18:36 +0900)
It took about 20 seconds for a large size of players (5,300 lines in
players.yaml). Now it completes in an instance.

changelog
shogi_server/league/persistent.rb

index 5789bf0..18fb40c 100644 (file)
--- a/changelog
+++ b/changelog
@@ -3,6 +3,10 @@
        * [shogi-server]
          - shogi_server/player.rb: Set initial values to BasicPlayer's
            instance variables.
        * [shogi-server]
          - shogi_server/player.rb: Set initial values to BasicPlayer's
            instance variables.
+         - shogi_server/league.rb: Improved performance of
+           Persistant#get_players. It took about 20 seconds for a large
+           size of players (5,300 lines in players.yaml). Now it completes in an
+           instance.
 
 2010-06-22  Daigo Moriwaki <daigo at debian dot org>
 
 
 2010-06-22  Daigo Moriwaki <daigo at debian dot org>
 
index 8d5ab98..1671abe 100644 (file)
@@ -58,25 +58,32 @@ class League
       return unless hash
 
       # a current user
       return unless hash
 
       # a current user
-      player.name          = hash['name']
-      player.rate          = hash['rate'] || 0
-      player.modified_at   = hash['last_modified']
-      player.rating_group  = hash['rating_group']
-      player.win           = hash['win']  || 0
-      player.loss          = hash['loss'] || 0
+      set_player_values(player, hash)
     end
 
     def get_players
       players = []
       each_group(true) do |group, players_hash|
     end
 
     def get_players
       players = []
       each_group(true) do |group, players_hash|
-        players << players_hash.keys
-      end
-      return players.flatten.collect do |player_id|
-        p = BasicPlayer.new
-        p.player_id = player_id
-        load_player(p)
-        p
+        players_hash.each do |key, value|
+          bp = BasicPlayer.new
+          bp.player_id = key
+          set_player_values(bp, value)
+          players << bp
+        end
       end
       end
+      return players
+    end
+
+    private
+    def set_player_values(player, hash)
+      return if player.nil? || hash.nil?
+
+      player.name          = hash['name']
+      player.rate          = hash['rate'] || 0
+      player.modified_at   = hash['last_modified']
+      player.rating_group  = hash['rating_group']
+      player.win           = hash['win']  || 0
+      player.loss          = hash['loss'] || 0
     end
   end # class Persistent
 
     end
   end # class Persistent