Attempt to reduce the number of updates to the TorrentView by caching
the previous status batch dictionary and only updating the difference.
This commit is contained in:
parent
44c82db9a8
commit
dd395b8c9c
|
@ -121,6 +121,7 @@ class TorrentView(listview.ListView, component.Component):
|
|||
self.load_state("torrentview.state")
|
||||
|
||||
self.status_signal_received = True
|
||||
self.previous_batched_status = {}
|
||||
|
||||
# Register the columns menu with the listview so it gets updated
|
||||
# accordingly.
|
||||
|
@ -294,37 +295,63 @@ class TorrentView(listview.ListView, component.Component):
|
|||
"""Callback function for get_torrents_status(). 'status' should be a
|
||||
dictionary of {torrent_id: {key, value}}."""
|
||||
status = pickle.loads(status)
|
||||
# Extract differences in this batch against the previous one.
|
||||
# This is to prevent updating stuff we don't need to and should save
|
||||
# GTK from redrawing needlessly.
|
||||
new_status = {}
|
||||
for torrent_id in status.keys():
|
||||
if torrent_id in self.previous_batched_status.keys():
|
||||
old = self.previous_batched_status[torrent_id]
|
||||
new = status[torrent_id]
|
||||
diff = {}
|
||||
for key in new.keys():
|
||||
# There is a difference, so lets add it to our new dict
|
||||
if new[key] != old[key]:
|
||||
diff[key] = new[key]
|
||||
if len(diff.keys()) > 0:
|
||||
new_status[torrent_id] = diff
|
||||
else:
|
||||
# The torrent_id is not in the previous status
|
||||
new_status[torrent_id] = status[torrent_id]
|
||||
|
||||
self.previous_batched_status = status
|
||||
|
||||
row = self.liststore.get_iter_first()
|
||||
while row != None:
|
||||
torrent_id = self.liststore.get_value(
|
||||
row, self.columns["torrent_id"].column_indices[0])
|
||||
if torrent_id in status.keys():
|
||||
if torrent_id in new_status.keys():
|
||||
# Set values for each column in the row
|
||||
for column in self.columns_to_update:
|
||||
column_index = self.get_column_index(column)
|
||||
if type(column_index) is not list:
|
||||
# We only have a single list store column we need to
|
||||
# update
|
||||
try:
|
||||
self.liststore.set_value(row,
|
||||
column_index,
|
||||
status[torrent_id][
|
||||
self.columns[column].status_field[0]])
|
||||
except (TypeError, KeyError), e:
|
||||
log.warning("Unable to update column %s: %s",
|
||||
column, e)
|
||||
if self.columns[column].status_field[0] in \
|
||||
new_status[torrent_id]:
|
||||
try:
|
||||
self.liststore.set_value(row,
|
||||
column_index,
|
||||
new_status[torrent_id][
|
||||
self.columns[column].status_field[0]])
|
||||
except (TypeError, KeyError), e:
|
||||
log.warning("Unable to update column %s: %s",
|
||||
column, e)
|
||||
else:
|
||||
# We have more than 1 liststore column to update
|
||||
for index in column_index:
|
||||
# Only update the column if the status field exists
|
||||
try:
|
||||
self.liststore.set_value(row,
|
||||
index,
|
||||
status[torrent_id][
|
||||
self.columns[column].status_field[
|
||||
column_index.index(index)]])
|
||||
except:
|
||||
pass
|
||||
if self.columns[column].status_field[
|
||||
column_index.index(index)] in \
|
||||
new_status[torrent_id]:
|
||||
try:
|
||||
self.liststore.set_value(row,
|
||||
index,
|
||||
new_status[torrent_id][
|
||||
self.columns[column].status_field[
|
||||
column_index.index(index)]])
|
||||
except:
|
||||
pass
|
||||
row = self.liststore.iter_next(row)
|
||||
self.status_signal_received = True
|
||||
|
||||
|
|
Loading…
Reference in New Issue