From 36a78d8f219ff257549e4ac90a40bb9ef9c201cd Mon Sep 17 00:00:00 2001 From: bendikro Date: Thu, 11 Oct 2012 15:45:04 +0200 Subject: [PATCH] Optimized torrentview.cell_data_trackericon cell_data_trackericon would load the tracker icon with gtk.gdk.pixbuf_new_from_file_at_size each time it's requested. These regular requests acumulating to thousands calls to pixbuf_new_from_file_at_size with a big torrent list. Now, read the tracker icon from disk once, and cache it. --- deluge/ui/gtkui/torrentview.py | 21 +++++++++++++-------- deluge/ui/tracker_icons.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/deluge/ui/gtkui/torrentview.py b/deluge/ui/gtkui/torrentview.py index 366dce88f..8ccb22170 100644 --- a/deluge/ui/gtkui/torrentview.py +++ b/deluge/ui/gtkui/torrentview.py @@ -116,24 +116,29 @@ def cell_data_statusicon(column, cell, model, row, data): def cell_data_trackericon(column, cell, model, row, data): def on_get_icon(icon): - def create_blank_icon(): + def create_blank_pixbuf(): i = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 16, 16) i.fill(0x00000000) return i if icon: - try: - icon = gtk.gdk.pixbuf_new_from_file_at_size(icon.get_filename(), 16, 16) - except Exception, e: - icon = create_blank_icon() + pixbuf = icon.get_cached_icon() + if not pixbuf: + try: + pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(icon.get_filename(), 16, 16) + except gobject.GError, e: + # Failed to load the pixbuf (Bad image file), so set a blank pixbuf + pixbuf = create_blank_pixbuf() + finally: + icon.set_cached_icon(pixbuf) else: - icon = create_blank_icon() + pixbuf = create_blank_pixbuf() #Supress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed with warnings.catch_warnings(): warnings.simplefilter("ignore") - if cell.get_property("pixbuf") != icon: - cell.set_property("pixbuf", icon) + if cell.get_property("pixbuf") != pixbuf: + cell.set_property("pixbuf", pixbuf) host = model[row][data] if host: diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py index 6b4bb5eb4..c31732031 100644 --- a/deluge/ui/tracker_icons.py +++ b/deluge/ui/tracker_icons.py @@ -74,6 +74,7 @@ class TrackerIcon(object): self.filename = os.path.abspath(filename) self.mimetype = extension_to_mimetype(self.filename.rpartition('.')[2]) self.data = None + self.icon_cache = None def __eq__(self, other): """ @@ -122,6 +123,20 @@ class TrackerIcon(object): """ return self.filename if full else os.path.basename(self.filename) + def set_cached_icon(self, data): + """ + Set the cached icon data. + + """ + self.icon_cache = data + + def get_cached_icon(self): + """ + Returns the cached icon data. + + """ + return self.icon_cache + class TrackerIcons(Component): """ A TrackerIcon factory class