Micro optimization: Avoid using the DB API and thereby acquiring a lock just to read column coloring/icon preferences

This commit is contained in:
Kovid Goyal 2022-07-31 14:15:57 +05:30
parent 0bba6484d1
commit b17fd29136
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 4 deletions

View File

@ -240,6 +240,7 @@ class BooksModel(QAbstractTableModel): # {{{
self.highlight_only = False
self.row_height = 0
self.marked_text_icons = {}
self.db_prefs = {'column_color_rules': (), 'column_icon_rules': ()}
self.read_config()
def marked_text_icon_for(self, label):
@ -348,6 +349,7 @@ class BooksModel(QAbstractTableModel): # {{{
self.ids_to_highlight_set = set()
self.current_highlighted_idx = None
self.db = db
self.update_db_prefs_cache()
self.custom_columns = self.db.field_metadata.custom_field_metadata()
self.column_map = list(self.orig_headers.keys()) + \
list(self.custom_columns)
@ -372,6 +374,12 @@ class BooksModel(QAbstractTableModel): # {{{
self.stop_metadata_backup()
self.start_metadata_backup()
def update_db_prefs_cache(self):
self.db_prefs = {
'column_icon_rules': tuple(self.db.new_api.pref('column_icon_rules', ())),
'column_color_rules': tuple(self.db.new_api.pref('column_color_rules', ())),
}
def start_metadata_backup(self):
from calibre.db.backup import MetadataBackup
self.metadata_backup = MetadataBackup(self.db)
@ -975,7 +983,7 @@ class BooksModel(QAbstractTableModel): # {{{
if col >= len(self.column_to_dc_map) or col < 0:
return None
if role == Qt.ItemDataRole.DisplayRole:
rules = self.db.new_api.pref('column_icon_rules')
rules = self.db_prefs['column_icon_rules']
if rules:
key = self.column_map[col]
id_ = None
@ -1008,7 +1016,7 @@ class BooksModel(QAbstractTableModel): # {{{
id_ = self.id(index)
self.column_color.mi = None
for k, fmt in self.db.new_api.pref('column_color_rules', ()):
for k, fmt in self.db_prefs['column_color_rules']:
if k == key:
ccol = self.column_color(id_, key, fmt, self.db,
self.color_cache, self.color_template_cache)
@ -1032,7 +1040,7 @@ class BooksModel(QAbstractTableModel): # {{{
if self.color_row_fmt_cache is None:
self.color_row_fmt_cache = tuple(fmt for key, fmt in
self.db.new_api.pref('column_color_rules', ()) if key == color_row_key)
self.db_prefs['column_color_rules'] if key == color_row_key)
for fmt in self.color_row_fmt_cache:
ccol = self.column_color(id_, color_row_key, fmt, self.db,
self.color_cache, self.color_template_cache)
@ -1045,7 +1053,7 @@ class BooksModel(QAbstractTableModel): # {{{
default_icon = None
if self.column_to_dc_decorator_map[col] is not None:
default_icon = self.column_to_dc_decorator_map[index.column()](index.row())
rules = self.db.new_api.pref('column_icon_rules')
rules = self.db_prefs['column_icon_rules']
if rules:
key = self.column_map[col]
id_ = None

View File

@ -885,6 +885,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
def refresh_gui(self, gui):
gui.book_details.book_info.refresh_css()
m = gui.library_view.model()
m.update_db_prefs_cache()
m.beginResetModel(), m.endResetModel()
self.update_font_display()
gui.tags_view.set_look_and_feel()