From 9b93a812d39bad20d254c781cde2ab42c569800e Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sun, 16 Aug 2015 10:23:44 -0400 Subject: [PATCH] 8377: Slow scrolling in Gramps 4.X, on all platforms. --- gramps/gui/views/treemodels/basemodel.py | 67 ++++ .../gui/views/treemodels/citationbasemodel.py | 154 +++++--- gramps/gui/views/treemodels/eventmodel.py | 41 ++- gramps/gui/views/treemodels/familymodel.py | 128 ++++--- gramps/gui/views/treemodels/flatbasemodel.py | 21 +- gramps/gui/views/treemodels/mediamodel.py | 26 +- gramps/gui/views/treemodels/notemodel.py | 31 +- gramps/gui/views/treemodels/peoplemodel.py | 339 ++++++++++-------- gramps/gui/views/treemodels/placemodel.py | 42 ++- gramps/gui/views/treemodels/repomodel.py | 20 +- gramps/gui/views/treemodels/sourcemodel.py | 31 +- gramps/gui/views/treemodels/treebasemodel.py | 37 +- 12 files changed, 583 insertions(+), 354 deletions(-) create mode 100644 gramps/gui/views/treemodels/basemodel.py diff --git a/gramps/gui/views/treemodels/basemodel.py b/gramps/gui/views/treemodels/basemodel.py new file mode 100644 index 000000000..ea088bc56 --- /dev/null +++ b/gramps/gui/views/treemodels/basemodel.py @@ -0,0 +1,67 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2009 Benny Malengier +# Copyright (C) 2010 Nick Hall +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +#------------------------------------------------------------------------- +# +# GRAMPS modules +# +#------------------------------------------------------------------------- +from .lru import LRU + +class BaseModel(object): + + # LRU cache size + _CACHE_SIZE = 10000 # 250 + + def __init__(self): + self.lru_data = LRU(BaseModel._CACHE_SIZE) + + def destroy(self): + """ + """ + self.lru_data = None + + def clear_cache(self, handle=None): + """ + Clear the LRU cache. + """ + if handle: + if handle in self.lru_data: + del self.lru_data[handle] + else: + self.lru_data.clear() + + def get_cached_value(self, handle, col): + """ + """ + if handle in self.lru_data and col in self.lru_data[handle]: + return (True, self.lru_data[handle][col]) + return (False, None) + + def set_cached_value(self, handle, col, data): + """ + """ + if not self._in_build: + if handle not in self.lru_data: + self.lru_data[handle] = {} + self.lru_data[handle][col] = data + diff --git a/gramps/gui/views/treemodels/citationbasemodel.py b/gramps/gui/views/treemodels/citationbasemodel.py index 1c47100ed..372f7347b 100644 --- a/gramps/gui/views/treemodels/citationbasemodel.py +++ b/gramps/gui/views/treemodels/citationbasemodel.py @@ -135,15 +135,18 @@ class CitationBaseModel(object): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[COLUMN_TAGS]: - tag = self.db.get_tag_from_handle(handle) - if tag: + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[COLUMN_TAGS]: + tag = self.db.get_tag_from_handle(handle) this_priority = tag.get_priority() if tag_priority is None or this_priority < tag_priority: tag_color = tag.get_color() tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def citation_change(self, data): @@ -157,72 +160,104 @@ class CitationBaseModel(object): def citation_src_title(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return str(source.get_title()) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_TITLE") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = str(source.get_title()) + except: + value = '' + self.set_cached_value(source_handle, "SRC_TITLE", value) + return value def citation_src_id(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return str(source.gramps_id) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_ID") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = str(source.gramps_id) + except: + value = '' + self.set_cached_value(source_handle, "SRC_ID", value) + return value def citation_src_auth(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return str(source.get_author()) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_AUTH") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = str(source.get_author()) + except: + value = '' + self.set_cached_value(source_handle, "SRC_AUTH", value) + return value def citation_src_abbr(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return str(source.get_abbreviation()) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_ABBR") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = str(source.get_abbreviation()) + except: + value = '' + self.set_cached_value(source_handle, "SRC_ABBR", value) + return value def citation_src_pinfo(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return str(source.get_publication_info()) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_PINFO") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = str(source.get_publication_info()) + except: + value = '' + self.set_cached_value(source_handle, "SRC_PINFO", value) + return value def citation_src_private(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - if source.get_privacy(): - return 'gramps-lock' - else: - # There is a problem returning None here. - return '' - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_PRIVATE") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + if source.get_privacy(): + value = 'gramps-lock' + else: + # There is a problem returning None here. + value = '' + except: + value = '' + self.set_cached_value(source_handle, "SRC_PRIVATE", value) + return value def citation_src_tags(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - tag_list = list(map(self.get_tag_name, source.get_tag_list())) - return ', '.join(sorted(tag_list, key=glocale.sort_key)) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_TAGS") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + tag_list = list(map(self.get_tag_name, source.get_tag_list())) + value = ', '.join(sorted(tag_list, key=glocale.sort_key)) + except: + value = '' + self.set_cached_value(source_handle, "SRC_TAGS", value) + return value def citation_src_chan(self, data): source_handle = data[COLUMN_SOURCE] - try: - source = self.db.get_source_from_handle(source_handle) - return format_time(source.change) - except: - return '' + cached, value = self.get_cached_value(source_handle, "SRC_CHAN") + if not cached: + try: + source = self.db.get_source_from_handle(source_handle) + value = format_time(source.change) + except: + value = '' + self.set_cached_value(source_handle, "SRC_CHAN", value) + return value # Fields access when 'data' is a Source @@ -259,15 +294,18 @@ class CitationBaseModel(object): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[COLUMN2_TAGS]: - tag = self.db.get_tag_from_handle(handle) - if tag: + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[COLUMN2_TAGS]: + tag = self.db.get_tag_from_handle(handle) this_priority = tag.get_priority() if tag_priority is None or this_priority < tag_priority: tag_color = tag.get_color() tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def source_src_chan(self, data): @@ -284,4 +322,8 @@ class CitationBaseModel(object): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value diff --git a/gramps/gui/views/treemodels/eventmodel.py b/gramps/gui/views/treemodels/eventmodel.py index 6f57dfb66..b3597b274 100644 --- a/gramps/gui/views/treemodels/eventmodel.py +++ b/gramps/gui/views/treemodels/eventmodel.py @@ -49,7 +49,7 @@ from gramps.gen.const import GRAMPS_LOCALE as glocale #------------------------------------------------------------------------- # -# COLUMN constants +# Positions in raw data structure # #------------------------------------------------------------------------- COLUMN_HANDLE = 0 @@ -75,7 +75,7 @@ class EventModel(FlatBaseModel): skip=set(), sort_map=None): self.gen_cursor = db.get_event_cursor self.map = db.get_raw_event_data - + self.fmap = [ self.column_description, self.column_id, @@ -127,13 +127,22 @@ class EventModel(FlatBaseModel): return data[COLUMN_DESCRIPTION] def column_participant(self,data): - return get_participant_from_event(self.db, data[COLUMN_HANDLE]) + handle = data[0] + cached, value = self.get_cached_value(handle, "PARTICIPANT") + if not cached: + value = get_participant_from_event(self.db, data[COLUMN_HANDLE]) + self.set_cached_value(handle, "PARTICIPANT", value) + return value def column_place(self,data): if data[COLUMN_PLACE]: - event = Event() - event.unserialize(data) - return place_displayer.display_event(self.db, event) + cached, value = self.get_cached_value(data[0], "PLACE") + if not cached: + event = Event() + event.unserialize(data) + value = place_displayer.display_event(self.db, event) + self.set_cached_value(data[0], "PLACE", value) + return value else: return '' @@ -185,21 +194,29 @@ class EventModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + # TAG_NAME isn't a column, but we cache it + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[COLUMN_TAGS]: - tag = self.db.get_tag_from_handle(handle) - if tag: + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[COLUMN_TAGS]: + tag = self.db.get_tag_from_handle(handle) this_priority = tag.get_priority() if tag_priority is None or this_priority < tag_priority: tag_color = tag.get_color() tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def column_tags(self, data): diff --git a/gramps/gui/views/treemodels/familymodel.py b/gramps/gui/views/treemodels/familymodel.py index 89ff23451..6d8a24e06 100644 --- a/gramps/gui/views/treemodels/familymodel.py +++ b/gramps/gui/views/treemodels/familymodel.py @@ -106,56 +106,86 @@ class FamilyModel(FlatBaseModel): return len(self.fmap)+1 def column_father(self, data): - if data[2]: - person = self.db.get_person_from_handle(data[2]) - return name_displayer.display_name(person.primary_name) - else: - return "" + handle = data[0] + cached, value = self.get_cached_value(handle, "FATHER") + if not cached: + if data[2]: + person = self.db.get_person_from_handle(data[2]) + value = name_displayer.display_name(person.primary_name) + else: + value = "" + self.set_cached_value(handle, "FATHER", value) + return value def sort_father(self, data): - if data[2]: - person = self.db.get_person_from_handle(data[2]) - return name_displayer.sorted_name(person.primary_name) - else: - return "" + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_FATHER") + if not cached: + if data[2]: + person = self.db.get_person_from_handle(data[2]) + value = name_displayer.sorted_name(person.primary_name) + else: + value = "" + self.set_cached_value(handle, "SORT_FATHER", value) + return value def column_mother(self, data): - if data[3]: - person = self.db.get_person_from_handle(data[3]) - return name_displayer.display_name(person.primary_name) - else: - return "" + handle = data[0] + cached, value = self.get_cached_value(handle, "MOTHER") + if not cached: + if data[3]: + person = self.db.get_person_from_handle(data[3]) + value = name_displayer.display_name(person.primary_name) + else: + value = "" + self.set_cached_value(handle, "MOTHER", value) + return value def sort_mother(self, data): - if data[3]: - person = self.db.get_person_from_handle(data[3]) - return name_displayer.sorted_name(person.primary_name) - else: - return "" + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_MOTHER") + if not cached: + if data[3]: + person = self.db.get_person_from_handle(data[3]) + value = name_displayer.sorted_name(person.primary_name) + else: + value = "" + self.set_cached_value(handle, "SORT_MOTHER", value) + return value def column_type(self, data): return str(FamilyRelType(data[5])) def column_marriage(self, data): - family = self.db.get_family_from_handle(data[0]) - event = get_marriage_or_fallback(self.db, family, "%s") - if event: - if event.date.format: - return event.date.format % displayer.display(event.date) - elif not get_date_valid(event): - return invalid_date_format % displayer.display(event.date) + handle = data[0] + cached, value = self.get_cached_value(handle, "MARRIAGE") + if not cached: + family = self.db.get_family_from_handle(data[0]) + event = get_marriage_or_fallback(self.db, family, "%s") + if event: + if event.date.format: + value = event.date.format % displayer.display(event.date) + elif not get_date_valid(event): + value = invalid_date_format % displayer.display(event.date) + else: + value = "%s" % displayer.display(event.date) else: - return "%s" % displayer.display(event.date) - else: - return '' + value = '' + self.set_cached_value(handle, "MARRIAGE", value) + return value def sort_marriage(self, data): - family = self.db.get_family_from_handle(data[0]) - event = get_marriage_or_fallback(self.db, family) - if event: - return "%09d" % event.date.get_sort_value() - else: - return '' + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_MARRIAGE") + if not cached: + family = self.db.get_family_from_handle(data[0]) + event = get_marriage_or_fallback(self.db, family) + if event: + value = "%09d" % event.date.get_sort_value() + else: + value = '' + self.set_cached_value(handle, "SORT_MARRIAGE", value) + return value def column_id(self, data): return str(data[1]) @@ -177,20 +207,28 @@ class FamilyModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[13]: - tag = self.db.get_tag_from_handle(handle) - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[13]: + tag = self.db.get_tag_from_handle(handle) + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def column_tags(self, data): diff --git a/gramps/gui/views/treemodels/flatbasemodel.py b/gramps/gui/views/treemodels/flatbasemodel.py index 02cd5c5f7..7030309a4 100644 --- a/gramps/gui/views/treemodels/flatbasemodel.py +++ b/gramps/gui/views/treemodels/flatbasemodel.py @@ -73,6 +73,7 @@ from gi.repository import Gtk from gramps.gen.filters import SearchFilter, ExactSearchFilter from gramps.gen.constfunc import conv_to_unicode, handle2internal from gramps.gen.const import GRAMPS_LOCALE as glocale +from .basemodel import BaseModel #------------------------------------------------------------------------- # @@ -442,7 +443,7 @@ class FlatNodeMap(object): # FlatBaseModel # #------------------------------------------------------------------------- -class FlatBaseModel(GObject.GObject, Gtk.TreeModel): +class FlatBaseModel(GObject.GObject, Gtk.TreeModel, BaseModel): """ The base class for all flat treeview models. It keeps a FlatNodeMap, and obtains data from database as needed @@ -454,7 +455,8 @@ class FlatBaseModel(GObject.GObject, Gtk.TreeModel): search=None, skip=set(), sort_map=None): cput = time.clock() - super(FlatBaseModel, self).__init__() + GObject.GObject.__init__(self) + BaseModel.__init__(self) #inheriting classes must set self.map to obtain the data self.prev_handle = None self.prev_data = None @@ -491,6 +493,7 @@ class FlatBaseModel(GObject.GObject, Gtk.TreeModel): """ Unset all elements that prevent garbage collection """ + BaseModel.destroy(self) self.db = None self.sort_func = None if self.node_map: @@ -556,15 +559,6 @@ class FlatBaseModel(GObject.GObject, Gtk.TreeModel): """ return None - def clear_cache(self, handle=None): - """ - If you use a cache, overwrite here so it is cleared when this - method is called (on rebuild) - :param handle: if None, clear entire cache, otherwise clear the handle - entry if present - """ - pass - def sort_keys(self): """ Return the (sort_key, handle) list of all data that can maximally @@ -775,7 +769,10 @@ class FlatBaseModel(GObject.GObject, Gtk.TreeModel): We need this to search in the column in the GUI """ if handle != self.prev_handle: - data = self.map(handle) + cached, data = self.get_cached_value(handle, col) + if not cached: + data = self.map(handle) + self.set_cached_value(handle, col, data) if data is None: #object is no longer present return '' diff --git a/gramps/gui/views/treemodels/mediamodel.py b/gramps/gui/views/treemodels/mediamodel.py index d93a9cc67..56795e8a7 100644 --- a/gramps/gui/views/treemodels/mediamodel.py +++ b/gramps/gui/views/treemodels/mediamodel.py @@ -174,20 +174,28 @@ class MediaModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[11]: - tag = self.db.get_tag_from_handle(handle) - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[11]: + tag = self.db.get_tag_from_handle(handle) + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def column_tags(self, data): diff --git a/gramps/gui/views/treemodels/notemodel.py b/gramps/gui/views/treemodels/notemodel.py index 3b803157d..fe2784124 100644 --- a/gramps/gui/views/treemodels/notemodel.py +++ b/gramps/gui/views/treemodels/notemodel.py @@ -137,22 +137,31 @@ class NoteModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[Note.POS_TAGS]: - tag = self.db.get_tag_from_handle(handle) - if tag: - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority - return tag_color + tag_handle = data[0] + cached, value = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[Note.POS_TAGS]: + tag = self.db.get_tag_from_handle(handle) + if tag: + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + value = tag_color + self.set_cached_value(tag_handle, "TAG_COLOR", value) + return value def column_tags(self, data): """ diff --git a/gramps/gui/views/treemodels/peoplemodel.py b/gramps/gui/views/treemodels/peoplemodel.py index 44a906a3a..fb2168c6f 100644 --- a/gramps/gui/views/treemodels/peoplemodel.py +++ b/gramps/gui/views/treemodels/peoplemodel.py @@ -38,6 +38,7 @@ from html import escape # #------------------------------------------------------------------------- from gi.repository import Gtk +from gi.repository import GObject #------------------------------------------------------------------------- # @@ -59,15 +60,15 @@ from gramps.gen.lib import (Name, EventRef, EventType, EventRoleType, from gramps.gen.display.name import displayer as name_displayer from gramps.gen.display.place import displayer as place_displayer from gramps.gen.datehandler import format_time, get_date, get_date_valid -from .lru import LRU from .flatbasemodel import FlatBaseModel from .treebasemodel import TreeBaseModel +from .basemodel import BaseModel from gramps.gen.config import config from gramps.gen.const import GRAMPS_LOCALE as glocale #------------------------------------------------------------------------- # -# COLUMN constants +# COLUMN constants; positions in raw data structure # #------------------------------------------------------------------------- COLUMN_ID = 1 @@ -90,19 +91,17 @@ invalid_date_format = config.get('preferences.invalid-date-format') # PeopleBaseModel # #------------------------------------------------------------------------- -class PeopleBaseModel(object): +class PeopleBaseModel(BaseModel): """ Basic Model interface to handle the PersonViews """ _GENDER = [ _('female'), _('male'), _('unknown') ] - # LRU cache size - _CACHE_SIZE = 250 - def __init__(self, db): """ Initialize the model building the initial data """ + BaseModel.__init__(self) self.db = db self.gen_cursor = db.get_person_cursor self.map = db.get_raw_person_data @@ -144,24 +143,16 @@ class PeopleBaseModel(object): self.column_tag_color, ] - #columns are accessed on every mouse over, so it is worthwhile to - #cache columns visible in one screen to avoid expensive database - #lookup of derived values - self.lru_name = LRU(PeopleBaseModel._CACHE_SIZE) - self.lru_spouse = LRU(PeopleBaseModel._CACHE_SIZE) - self.lru_bdate = LRU(PeopleBaseModel._CACHE_SIZE) - self.lru_ddate = LRU(PeopleBaseModel._CACHE_SIZE) - def destroy(self): """ Unset all elements that can prevent garbage collection """ + BaseModel.destroy(self) self.db = None self.gen_cursor = None self.map = None self.fmap = None self.smap = None - self.clear_local_cache() def color_column(self): """ @@ -169,63 +160,38 @@ class PeopleBaseModel(object): """ return 15 - def clear_local_cache(self, handle=None): - """ Clear the LRU cache """ - if handle: - try: - del self.lru_name[handle] - except KeyError: - pass - try: - del self.lru_spouse[handle] - except KeyError: - pass - try: - del self.lru_bdate[handle] - except KeyError: - pass - try: - del self.lru_ddate[handle] - except KeyError: - pass - else: - self.lru_name.clear() - self.lru_spouse.clear() - self.lru_bdate.clear() - self.lru_ddate.clear() - def on_get_n_columns(self): """ Return the number of columns in the model """ return len(self.fmap)+1 def sort_name(self, data): - name = name_displayer.raw_sorted_name(data[COLUMN_NAME]) - # internally we work with utf-8 - if not isinstance(name, str): - name = name.decode('utf-8') + handle = data[0] + cached, name = self.get_cached_value(handle, "SORT_NAME") + if not cached: + name = name_displayer.raw_sorted_name(data[COLUMN_NAME]) + # internally we work with utf-8 + if not isinstance(name, str): + name = name.decode('utf-8') + self.set_cached_value(handle, "SORT_NAME", name) return name def column_name(self, data): handle = data[0] - if handle in self.lru_name: - name = self.lru_name[handle] - else: + cached, name = self.get_cached_value(handle, "NAME") + if not cached: name = name_displayer.raw_display_name(data[COLUMN_NAME]) # internally we work with utf-8 for python 2.7 if not isinstance(name, str): name = name.encode('utf-8') - if not self._in_build: - self.lru_name[handle] = name + self.set_cached_value(handle, "NAME", name) return name def column_spouse(self, data): handle = data[0] - if handle in self.lru_spouse: - value = self.lru_spouse[handle] - else: + cached, value = self.get_cached_value(handle, "SPOUSE") + if not cached: value = self._get_spouse_data(data) - if not self._in_build: - self.lru_spouse[handle] = value + self.set_cached_value(handle, "SPOUSE", value) return value def column_private(self, data): @@ -265,17 +231,19 @@ class PeopleBaseModel(object): def column_birth_day(self, data): handle = data[0] - if handle in self.lru_bdate: - value = self.lru_bdate[handle] - else: + cached, value = self.get_cached_value(handle, "BIRTH_DAY") + if not cached: value = self._get_birth_data(data, False) - if not self._in_build: - self.lru_bdate[handle] = value + self.set_cached_value(handle, "BIRTH_DAY", value) return value def sort_birth_day(self, data): handle = data[0] - return self._get_birth_data(data, True) + cached, value = self.get_cached_value(handle, "SORT_BIRTH_DAY") + if not cached: + value = self._get_birth_data(data, True) + self.set_cached_value(handle, "SORT_BIRTH_DAY", value) + return value def _get_birth_data(self, data, sort_mode): index = data[COLUMN_BIRTH] @@ -320,17 +288,19 @@ class PeopleBaseModel(object): def column_death_day(self, data): handle = data[0] - if handle in self.lru_ddate: - value = self.lru_ddate[handle] - else: + cached, value = self.get_cached_value(handle, "DEATH_DAY") + if not cached: value = self._get_death_data(data, False) - if not self._in_build: - self.lru_ddate[handle] = value + self.set_cached_value(handle, "DEATH_DAY", value) return value def sort_death_day(self, data): handle = data[0] - return self._get_death_data(data, True) + cached, value = self.get_cached_value(handle, "SORT_DEATH_DAY") + if not cached: + value = self._get_death_data(data, True) + self.set_cached_value(handle, "SORT_DEATH_DAY", value) + return value def _get_death_data(self, data, sort_mode): index = data[COLUMN_DEATH] @@ -375,61 +345,86 @@ class PeopleBaseModel(object): return "" def column_birth_place(self, data): - index = data[COLUMN_BIRTH] - if index != -1: - try: - local = data[COLUMN_EVENT][index] - br = EventRef() - br.unserialize(local) - event = self.db.get_event_from_handle(br.ref) - if event: - place_title = place_displayer.display_event(self.db, event) - if place_title: - return escape(place_title) - except: - return '' + handle = data[0] + cached, value = self.get_cached_value(handle, "BIRTH_PLACE") + if cached: + return value + else: + index = data[COLUMN_BIRTH] + if index != -1: + try: + local = data[COLUMN_EVENT][index] + br = EventRef() + br.unserialize(local) + event = self.db.get_event_from_handle(br.ref) + if event: + place_title = place_displayer.display_event(self.db, event) + if place_title: + value = escape(place_title) + self.set_cached_value(handle, "BIRTH_PLACE", value) + return value + except: + value = '' + self.set_cached_value(handle, "BIRTH_PLACE", value) + return value - for event_ref in data[COLUMN_EVENT]: - er = EventRef() - er.unserialize(event_ref) - event = self.db.get_event_from_handle(er.ref) - etype = event.get_type() - if (etype in [EventType.BAPTISM, EventType.CHRISTEN] and - er.get_role() == EventRoleType.PRIMARY): - - place_title = place_displayer.display_event(self.db, event) - if place_title: - return "%s" % escape(place_title) - return "" + for event_ref in data[COLUMN_EVENT]: + er = EventRef() + er.unserialize(event_ref) + event = self.db.get_event_from_handle(er.ref) + etype = event.get_type() + if (etype in [EventType.BAPTISM, EventType.CHRISTEN] and + er.get_role() == EventRoleType.PRIMARY): + place_title = place_displayer.display_event(self.db, event) + if place_title: + value = "%s" % escape(place_title) + self.set_cached_value(handle, "BIRTH_PLACE", value) + return value + value = "" + self.set_cached_value(handle, "BIRTH_PLACE", value) + return value def column_death_place(self, data): - index = data[COLUMN_DEATH] - if index != -1: - try: - local = data[COLUMN_EVENT][index] - dr = EventRef() - dr.unserialize(local) - event = self.db.get_event_from_handle(dr.ref) - if event: - place_title = place_displayer.display_event(self.db, event) - if place_title: - return escape(place_title) - except: - return '' - - for event_ref in data[COLUMN_EVENT]: - er = EventRef() - er.unserialize(event_ref) - event = self.db.get_event_from_handle(er.ref) - etype = event.get_type() - if (etype in [EventType.BURIAL, EventType.CREMATION, - EventType.CAUSE_DEATH] - and er.get_role() == EventRoleType.PRIMARY): + handle = data[0] + cached, value = self.get_cached_value(handle, "DEATH_PLACE") + if cached: + return value + else: + index = data[COLUMN_DEATH] + if index != -1: + try: + local = data[COLUMN_EVENT][index] + dr = EventRef() + dr.unserialize(local) + event = self.db.get_event_from_handle(dr.ref) + if event: + place_title = place_displayer.display_event(self.db, event) + if place_title: + value = escape(place_title) + self.set_cached_value(handle, "DEATH_PLACE", value) + return value + except: + value = '' + self.set_cached_value(handle, "DEATH_PLACE", value) + return value - place_title = place_displayer.display_event(self.db, event) - if place_title: - return "%s" % escape(place_title) - return "" + for event_ref in data[COLUMN_EVENT]: + er = EventRef() + er.unserialize(event_ref) + event = self.db.get_event_from_handle(er.ref) + etype = event.get_type() + if (etype in [EventType.BURIAL, EventType.CREMATION, + EventType.CAUSE_DEATH] + and er.get_role() == EventRoleType.PRIMARY): + + place_title = place_displayer.display_event(self.db, event) + if place_title: + value = "%s" % escape(place_title) + self.set_cached_value(handle, "DEATH_PLACE", value) + return value + value = "" + self.set_cached_value(handle, "DEATH_PLACE", value) + return value def _get_parents_data(self, data): parents = 0 @@ -468,56 +463,110 @@ class PeopleBaseModel(object): return todo def column_parents(self, data): - return str(self._get_parents_data(data)) + handle = data[0] + cached, value = self.get_cached_value(handle, "PARENTS") + if not cached: + value = self._get_parents_data(data) + self.set_cached_value(handle, "PARENTS", value) + return str(value) def sort_parents(self, data): - return '%06d' % self._get_parents_data(data) + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_PARENTS") + if not cached: + value = self._get_parents_data(data) + self.set_cached_value(handle, "SORT_PARENTS", value) + return '%06d' % value def column_marriages(self, data): - return str(self._get_marriages_data(data)) + handle = data[0] + cached, value = self.get_cached_value(handle, "MARRIAGES") + if not cached: + value = self._get_marriages_data(data) + self.set_cached_value(handle, "MARRIAGES", value) + return str(value) def sort_marriages(self, data): - return '%06d' % self._get_marriages_data(data) + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_MARRIAGES") + if not cached: + value = self._get_marriages_data(data) + self.set_cached_value(handle, "SORT_MARRIAGES", value) + return '%06d' % value def column_children(self, data): - return str(self._get_children_data(data)) + handle = data[0] + cached, value = self.get_cached_value(handle, "CHILDREN") + if not cached: + value = self._get_children_data(data) + self.set_cached_value(handle, "CHILDREN", value) + return str(value) def sort_children(self, data): - return '%06d' % self._get_children_data(data) + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_CHILDREN") + if not cached: + value = self._get_children_data(data) + self.set_cached_value(handle, "SORT_CHILDREN", value) + return '%06d' % value def column_todo(self, data): - return str(self._get_todo_data(data)) + handle = data[0] + cached, value = self.get_cached_value(handle, "TODO") + if not cached: + value = self._get_todo_data(data) + self.set_cached_value(handle, "TODO", value) + return str(value) def sort_todo(self, data): - return '%06d' % self._get_todo_data(data) + handle = data[0] + cached, value = self.get_cached_value(handle, "SORT_TODO") + if not cached: + value = self._get_todo_data(data) + self.set_cached_value(handle, "SORT_TODO", value) + return '%06d' % value def get_tag_name(self, tag_handle): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() - + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value + def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[COLUMN_TAGS]: - tag = self.db.get_tag_from_handle(handle) - if tag: - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority - return tag_color + tag_handle = data[0] + cached, value = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[COLUMN_TAGS]: + tag = self.db.get_tag_from_handle(handle) + if tag: + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + value = tag_color + self.set_cached_value(tag_handle, "TAG_COLOR", value) + return value def column_tags(self, data): """ Return the sorted list of tags. """ - tag_list = list(map(self.get_tag_name, data[COLUMN_TAGS])) - return ', '.join(sorted(tag_list, key=glocale.sort_key)) + handle = data[0] + cached, value = self.get_cached_value(handle, "TAGS") + if not cached: + tag_list = list(map(self.get_tag_name, data[COLUMN_TAGS])) + value = ', '.join(sorted(tag_list, key=glocale.sort_key)) + self.set_cached_value(handle, "TAGS", value) + return value class PersonListModel(PeopleBaseModel, FlatBaseModel): """ @@ -529,10 +578,6 @@ class PersonListModel(PeopleBaseModel, FlatBaseModel): FlatBaseModel.__init__(self, db, search=search, skip=skip, scol=scol, order=order, sort_map=sort_map) - def clear_cache(self, handle=None): - """ Clear the LRU cache """ - PeopleBaseModel.clear_local_cache(self, handle) - def destroy(self): """ Unset all elements that can prevent garbage collection @@ -546,7 +591,6 @@ class PersonTreeModel(PeopleBaseModel, TreeBaseModel): """ def __init__(self, db, scol=0, order=Gtk.SortType.ASCENDING, search=None, skip=set(), sort_map=None): - PeopleBaseModel.__init__(self, db) TreeBaseModel.__init__(self, db, search=search, skip=skip, scol=scol, order=order, sort_map=sort_map) @@ -564,13 +608,6 @@ class PersonTreeModel(PeopleBaseModel, TreeBaseModel): """ self.number_items = self.db.get_number_of_people - def clear_cache(self, handle=None): - """ Clear the LRU cache - overwrite of base methods - """ - TreeBaseModel.clear_cache(self, handle) - PeopleBaseModel.clear_local_cache(self, handle) - def get_tree_levels(self): """ Return the headings of the levels in the hierarchy. diff --git a/gramps/gui/views/treemodels/placemodel.py b/gramps/gui/views/treemodels/placemodel.py index f8c786c43..c7eedd251 100644 --- a/gramps/gui/views/treemodels/placemodel.py +++ b/gramps/gui/views/treemodels/placemodel.py @@ -116,9 +116,14 @@ class PlaceBaseModel(object): return len(self.fmap)+1 def column_title(self, data): - place = Place() - place.unserialize(data) - return place_displayer.display(self.db, place) + handle = data[0] + cached, value = self.get_cached_value(handle, "PLACE") + if not cached: + place = Place() + place.unserialize(data) + value = place_displayer.display(self.db, place) + self.set_cached_value(handle, "PLACE", value) + return value def column_name(self, data): return str(data[6][0]) @@ -181,22 +186,31 @@ class PlaceBaseModel(object): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[16]: - tag = self.db.get_tag_from_handle(handle) - if tag: - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority - return tag_color + tag_handle = data[0] + cached, value = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[16]: + tag = self.db.get_tag_from_handle(handle) + if tag: + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + value = tag_color + self.set_cached_value(tag_handle, "TAG_COLOR", value) + return value def column_tags(self, data): """ diff --git a/gramps/gui/views/treemodels/repomodel.py b/gramps/gui/views/treemodels/repomodel.py index bd9e4d5a2..4ebfc05a5 100644 --- a/gramps/gui/views/treemodels/repomodel.py +++ b/gramps/gui/views/treemodels/repomodel.py @@ -239,21 +239,29 @@ class RepositoryModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + # TAG_NAME isn't a column, but we cache it + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[8]: - tag = self.db.get_tag_from_handle(handle) - if tag: + tag_handle = data[0] + cached, tag_color = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[8]: + tag = self.db.get_tag_from_handle(handle) this_priority = tag.get_priority() if tag_priority is None or this_priority < tag_priority: tag_color = tag.get_color() tag_priority = this_priority + self.set_cached_value(tag_handle, "TAG_COLOR", tag_color) return tag_color def column_tags(self, data): diff --git a/gramps/gui/views/treemodels/sourcemodel.py b/gramps/gui/views/treemodels/sourcemodel.py index 07f4a82f6..f445e9e02 100644 --- a/gramps/gui/views/treemodels/sourcemodel.py +++ b/gramps/gui/views/treemodels/sourcemodel.py @@ -130,22 +130,31 @@ class SourceModel(FlatBaseModel): """ Return the tag name from the given tag handle. """ - return self.db.get_tag_from_handle(tag_handle).get_name() + cached, value = self.get_cached_value(tag_handle, "TAG_NAME") + if not cached: + value = self.db.get_tag_from_handle(tag_handle).get_name() + self.set_cached_value(tag_handle, "TAG_NAME", value) + return value def column_tag_color(self, data): """ Return the tag color. """ - tag_color = "#000000000000" - tag_priority = None - for handle in data[11]: - tag = self.db.get_tag_from_handle(handle) - if tag: - this_priority = tag.get_priority() - if tag_priority is None or this_priority < tag_priority: - tag_color = tag.get_color() - tag_priority = this_priority - return tag_color + tag_handle = data[0] + cached, value = self.get_cached_value(tag_handle, "TAG_COLOR") + if not cached: + tag_color = "#000000000000" + tag_priority = None + for handle in data[11]: + tag = self.db.get_tag_from_handle(handle) + if tag: + this_priority = tag.get_priority() + if tag_priority is None or this_priority < tag_priority: + tag_color = tag.get_color() + tag_priority = this_priority + value = tag_color + self.set_cached_value(tag_handle, "TAG_COLOR", value) + return value def column_tags(self, data): """ diff --git a/gramps/gui/views/treemodels/treebasemodel.py b/gramps/gui/views/treemodels/treebasemodel.py index da5e98617..ef960bc49 100644 --- a/gramps/gui/views/treemodels/treebasemodel.py +++ b/gramps/gui/views/treemodels/treebasemodel.py @@ -53,9 +53,9 @@ from gi.repository import Gtk from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext import gramps.gui.widgets.progressdialog as progressdlg -from .lru import LRU from bisect import bisect_right from gramps.gen.filters import SearchFilter, ExactSearchFilter +from .basemodel import BaseModel #------------------------------------------------------------------------- # @@ -231,7 +231,7 @@ class NodeMap(object): # TreeBaseModel # #------------------------------------------------------------------------- -class TreeBaseModel(GObject.GObject, Gtk.TreeModel): +class TreeBaseModel(GObject.GObject, Gtk.TreeModel, BaseModel): """ The base class for all hierarchical treeview models. The model defines the mapping between a unique node and a path. Paths are defined by a tuple. @@ -274,9 +274,6 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): secondary object type. """ - # LRU cache size - _CACHE_SIZE = 250 - def __init__(self, db, search=None, skip=set(), scol=0, order=Gtk.SortType.ASCENDING, sort_map=None, @@ -284,7 +281,8 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): group_can_have_handle = False, has_secondary=False): cput = time.clock() - super(TreeBaseModel, self).__init__() + GObject.GObject.__init__(self) + BaseModel.__init__(self) #We create a stamp to recognize invalid iterators. From the docs: #Set the stamp to be equal to your model's stamp, to mark the #iterator as valid. When your model's structure changes, you should @@ -332,8 +330,6 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): self._in_build = False - self.lru_data = LRU(TreeBaseModel._CACHE_SIZE) - self.__total = 0 self.__displayed = 0 @@ -350,6 +346,7 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): """ Unset all elements that prevent garbage collection """ + BaseModel.destroy(self) self.db = None self.sort_func = None if self.has_secondary: @@ -364,8 +361,6 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): self.search2 = None self.current_filter = None self.current_filter2 = None - self.clear_cache() - self.lru_data = None def _set_base_data(self): """ @@ -410,18 +405,6 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): """ return None - def clear_cache(self, handle=None): - """ - Clear the LRU cache. - """ - if handle: - try: - del self.lru_data[handle] - except KeyError: - pass - else: - self.lru_data.clear() - def clear(self): """ Clear the data map. @@ -958,16 +941,16 @@ class TreeBaseModel(GObject.GObject, Gtk.TreeModel): """ if secondary is None: raise NotImplementedError + + cached, data = self.get_cached_value(handle, col) - if handle in self.lru_data: - data = self.lru_data[handle] - else: + if not cached: if not secondary: data = self.map(handle) else: data = self.map2(handle) - if not self._in_build: - self.lru_data[handle] = data + if store_cache: + self.set_cached_value(handle, col, data) if not secondary: # None is used to indicate this column has no data