diff options
author | bendikro <bendikro@gmail.com> | 2013-02-21 01:11:35 +0100 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2013-05-09 12:10:02 +0100 |
commit | 5503f90473f523a87502166ee5188d0728729e95 (patch) | |
tree | 88a84216b2e5087990efb1d6e5265ff62aae8148 | |
parent | d5a3851eeff1ef3f1220070c7bb718791bd902f1 (diff) | |
download | deluge-5503f9047.tar.gz deluge-5503f9047.tar.bz2 deluge-5503f9047.zip |
Fix #2285 : Speed optimizations for sessionproxy
-rw-r--r-- | deluge/tests/test_sessionproxy.py | 5 | ||||
-rw-r--r-- | deluge/ui/sessionproxy.py | 30 |
2 files changed, 25 insertions, 10 deletions
diff --git a/deluge/tests/test_sessionproxy.py b/deluge/tests/test_sessionproxy.py index bd1c9c1a1..7f92dfb09 100644 --- a/deluge/tests/test_sessionproxy.py +++ b/deluge/tests/test_sessionproxy.py @@ -89,10 +89,7 @@ class SessionProxyTestCase(unittest.TestCase): d = self.sp.start() def do_get_torrents_status(torrent_ids): - inital_keys = [ - 'queue', 'state', 'name', 'total_wanted', 'progress', 'state', - 'download_payload_rate', 'upload_payload_rate', 'eta', 'owner' - ] + inital_keys = ['key1'] self.sp.get_torrents_status({'id': torrent_ids}, inital_keys) d.addCallback(do_get_torrents_status) return d diff --git a/deluge/ui/sessionproxy.py b/deluge/ui/sessionproxy.py index 652ebbf87..2ba8f1cea 100644 --- a/deluge/ui/sessionproxy.py +++ b/deluge/ui/sessionproxy.py @@ -99,18 +99,36 @@ class SessionProxy(component.Component): """ 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: try: if keys: - sd[torrent_id] = dict([ - (x, y) for x, y in self.torrents[torrent_id][1].iteritems() - if x in keys - ]) + sd[torrent_id] = self.torrents[torrent_id][1].copy() + + # 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: sd[torrent_id] = dict(self.torrents[torrent_id][1]) except KeyError: continue - return sd def get_torrent_status(self, torrent_id, keys): @@ -184,7 +202,7 @@ class SessionProxy(component.Component): def on_status(result, torrent_ids, keys): # Update the internal torrent status dict with the update values t = time.time() - for key, value in result.items(): + for key, value in result.iteritems(): try: self.torrents[key][0] = t self.torrents[key][1].update(value) |