Improve performance of column coloring:

1) cache the results of calling the formatter even if they are negative.
2) use compiled templates
This commit is contained in:
Charles Haley 2014-07-06 10:25:01 +02:00
parent 2dc956bbd7
commit 93e828fbbd

View File

@ -54,17 +54,22 @@ class ColumnColor(object): # {{{
self.mi = None self.mi = None
self.formatter = formatter self.formatter = formatter
def __call__(self, id_, key, fmt, db, color_cache): def __call__(self, id_, key, fmt, db, color_cache, template_cache):
if id_ in color_cache and key in color_cache[id_]: if id_ in color_cache and key in color_cache[id_]:
self.mi = None self.mi = None
return color_cache[id_][key] color = color_cache[id_][key]
if color.isValid():
return QVariant(color)
return None
try: try:
if self.mi is None: if self.mi is None:
self.mi = db.new_api.get_proxy_metadata(id_) self.mi = db.new_api.get_proxy_metadata(id_)
color = QColor(self.formatter.safe_format(fmt, self.mi, '', self.mi)) color = QColor(self.formatter.safe_format(fmt, self.mi, '', self.mi,
column_name=key,
template_cache=template_cache))
color_cache[id_][key] = color
if color.isValid(): if color.isValid():
color = QVariant(color) color = QVariant(color)
color_cache[id_][key] = color
self.mi = None self.mi = None
return color return color
except: except:
@ -78,7 +83,8 @@ class ColumnIcon(object): # {{{
self.formatter = formatter self.formatter = formatter
self.model = model self.model = model
def __call__(self, id_, key, fmts, cache_index, db, icon_cache, icon_bitmap_cache): def __call__(self, id_, fmts, cache_index, db, icon_cache, icon_bitmap_cache,
template_cache):
if id_ in icon_cache and cache_index in icon_cache[id_]: if id_ in icon_cache and cache_index in icon_cache[id_]:
self.mi = None self.mi = None
return icon_cache[id_][cache_index] return icon_cache[id_][cache_index]
@ -87,7 +93,8 @@ class ColumnIcon(object): # {{{
self.mi = db.new_api.get_proxy_metadata(id_) self.mi = db.new_api.get_proxy_metadata(id_)
icons = [] icons = []
for kind, fmt in fmts: for kind, fmt in fmts:
rule_icons = self.formatter.safe_format(fmt, self.mi, '', self.mi) rule_icons = self.formatter.safe_format(fmt, self.mi, '', self.mi,
column_name=cache_index, template_cache=template_cache)
if not rule_icons: if not rule_icons:
continue continue
icon_list = [ic.strip() for ic in rule_icons.split(':')] icon_list = [ic.strip() for ic in rule_icons.split(':')]
@ -204,6 +211,8 @@ class BooksModel(QAbstractTableModel): # {{{
self.icon_cache = defaultdict(dict) self.icon_cache = defaultdict(dict)
self.icon_bitmap_cache = {} self.icon_bitmap_cache = {}
self.color_row_fmt_cache = None self.color_row_fmt_cache = None
self.color_template_cache = {}
self.icon_template_cache = {}
def set_row_height(self, height): def set_row_height(self, height):
self.row_height = height self.row_height = height
@ -815,8 +824,9 @@ class BooksModel(QAbstractTableModel): # {{{
if fmts: if fmts:
cache_index = key + ':DisplayRole' cache_index = key + ':DisplayRole'
ccicon = self.column_icon(id_, key, fmts, cache_index, self.db, ccicon = self.column_icon(id_, fmts, cache_index, self.db,
self.icon_cache, self.icon_bitmap_cache) self.icon_cache, self.icon_bitmap_cache,
self.icon_template_cache)
if ccicon is not None: if ccicon is not None:
return NONE return NONE
self.icon_cache[id_][cache_index] = None self.icon_cache[id_][cache_index] = None
@ -838,7 +848,7 @@ class BooksModel(QAbstractTableModel): # {{{
for k, fmt in self.db.prefs['column_color_rules']: for k, fmt in self.db.prefs['column_color_rules']:
if k == key: if k == key:
ccol = self.column_color(id_, key, fmt, self.db, ccol = self.column_color(id_, key, fmt, self.db,
self.color_cache) self.color_cache, self.color_template_cache)
if ccol is not None: if ccol is not None:
return ccol return ccol
@ -859,7 +869,7 @@ class BooksModel(QAbstractTableModel): # {{{
for fmt in self.color_row_fmt_cache: for fmt in self.color_row_fmt_cache:
ccol = self.column_color(id_, color_row_key, fmt, self.db, ccol = self.column_color(id_, color_row_key, fmt, self.db,
self.color_cache) self.color_cache, self.color_template_cache)
if ccol is not None: if ccol is not None:
return ccol return ccol
@ -887,8 +897,9 @@ class BooksModel(QAbstractTableModel): # {{{
need_icon_with_text = True need_icon_with_text = True
if fmts: if fmts:
cache_index = key + ':DecorationRole' cache_index = key + ':DecorationRole'
ccicon = self.column_icon(id_, key, fmts, cache_index, self.db, ccicon = self.column_icon(id_, fmts, cache_index, self.db,
self.icon_cache, self.icon_bitmap_cache) self.icon_cache, self.icon_bitmap_cache,
self.icon_template_cache)
if ccicon is not None: if ccicon is not None:
return ccicon return ccicon
if need_icon_with_text: if need_icon_with_text: