Fix #2285 : Speed optimizations for sessionproxy

This commit is contained in:
bendikro 2013-02-21 01:11:35 +01:00 committed by Calum Lind
parent d5a3851eef
commit 5503f90473
2 changed files with 25 additions and 10 deletions

View File

@ -89,10 +89,7 @@ class SessionProxyTestCase(unittest.TestCase):
d = self.sp.start() d = self.sp.start()
def do_get_torrents_status(torrent_ids): def do_get_torrents_status(torrent_ids):
inital_keys = [ inital_keys = ['key1']
'queue', 'state', 'name', 'total_wanted', 'progress', 'state',
'download_payload_rate', 'upload_payload_rate', 'eta', 'owner'
]
self.sp.get_torrents_status({'id': torrent_ids}, inital_keys) self.sp.get_torrents_status({'id': torrent_ids}, inital_keys)
d.addCallback(do_get_torrents_status) d.addCallback(do_get_torrents_status)
return d return d

View File

@ -99,18 +99,36 @@ class SessionProxy(component.Component):
""" """
sd = {} sd = {}
keys = set(keys)
keys_len = -1 # The number of keys for the current cache (not the len of keys_diff_cached)
keys_diff_cached = []
for torrent_id in torrent_ids: for torrent_id in torrent_ids:
try: try:
if keys: if keys:
sd[torrent_id] = dict([ sd[torrent_id] = self.torrents[torrent_id][1].copy()
(x, y) for x, y in self.torrents[torrent_id][1].iteritems()
if x in keys # Have to remove the keys that weren't requested
]) if len(sd[torrent_id]) == keys_len:
# If the number of keys are equal they are the same keys
# so we use the cached diff of the keys we need to remove
keys_to_remove = keys_diff_cached
else:
# Not the same keys so create a new diff
keys_to_remove = set(sd[torrent_id].iterkeys()) - keys
# Update the cached diff
keys_diff_cached = keys_to_remove
keys_len = len(sd[torrent_id])
# Usually there are no keys to remove, so it's cheaper with
# this if-test than a for-loop with no iterations.
if keys_to_remove:
for k in keys_to_remove:
del sd[torrent_id][k]
else: else:
sd[torrent_id] = dict(self.torrents[torrent_id][1]) sd[torrent_id] = dict(self.torrents[torrent_id][1])
except KeyError: except KeyError:
continue continue
return sd return sd
def get_torrent_status(self, torrent_id, keys): def get_torrent_status(self, torrent_id, keys):
@ -184,7 +202,7 @@ class SessionProxy(component.Component):
def on_status(result, torrent_ids, keys): def on_status(result, torrent_ids, keys):
# Update the internal torrent status dict with the update values # Update the internal torrent status dict with the update values
t = time.time() t = time.time()
for key, value in result.items(): for key, value in result.iteritems():
try: try:
self.torrents[key][0] = t self.torrents[key][0] = t
self.torrents[key][1].update(value) self.torrents[key][1].update(value)