From d898ac2a430cc30e1f654a0ae98b64139fd86bd7 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Fri, 24 Feb 2023 22:49:33 -0800 Subject: [PATCH] completion: micro-optimizations to improve startup time Memoize icon creation to avoid hitting the filesystem repeatedly for each loaded icon. This speeds git-dag when the repository contains a very large amount of files. Signed-off-by: David Aguilar --- cola/icons.py | 4 ++++ cola/widgets/completion.py | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cola/icons.py b/cola/icons.py index 2ecaa8bd..5f453b5c 100644 --- a/cola/icons.py +++ b/cola/icons.py @@ -6,6 +6,7 @@ from qtpy import QtGui from qtpy import QtWidgets from . import core +from . import decorators from . import qtcompat from . import resources from .compat import ustr @@ -65,6 +66,7 @@ def name_from_basename(basename): return 'icons:' + basename +@decorators.memoize def from_name(name): """Return a QIcon from an absolute filename or "icons:basename.svg" name""" return QtGui.QIcon(name) @@ -92,6 +94,7 @@ def from_theme(name, fallback=None): return icon(fallback or name) +@decorators.memoize def basename_from_filename(filename): """Returns an icon name based on the filename""" mimetype = core.guess_mimetype(filename) @@ -104,6 +107,7 @@ def basename_from_filename(filename): return KNOWN_FILE_EXTENSIONS.get(extension.lower(), 'file-text.svg') +@decorators.memoize def from_filename(filename): """Return a QIcon from a filename""" basename = basename_from_filename(filename) diff --git a/cola/widgets/completion.py b/cola/widgets/completion.py index 50233ef0..e33a8c05 100644 --- a/cola/widgets/completion.py +++ b/cola/widgets/completion.py @@ -430,6 +430,11 @@ class CompletionModel(QtGui.QStandardItemModel): return ((), (), set()) def apply_matches(self, match_tuple): + """Build widgets for all of the matching items""" + if not match_tuple: + # Results from background tasks can arrive after the widget has been destroyed. + utils.catch_runtime_error(self.set_items, []) + return matched_refs, matched_paths, dirs = match_tuple QStandardItem = QtGui.QStandardItem @@ -474,7 +479,7 @@ def _lower(x): return x.lower() -def filter_matches(match_text, candidates, case_sensitive, sort_key=lambda x: x): +def filter_matches(match_text, candidates, case_sensitive, sort_key=None): """Filter candidates and return the matches""" if case_sensitive: case_transform = _identity @@ -487,7 +492,16 @@ def filter_matches(match_text, candidates, case_sensitive, sort_key=lambda x: x) else: matches = list(candidates) - matches.sort(key=lambda x: sort_key(case_transform(x))) + if case_sensitive: + if sort_key is None: + matches.sort() + else: + matches.sort(key=sort_key) + else: + if sort_key is None: + matches.sort(key=_lower) + else: + matches.sort(key=lambda x: sort_key(_lower(x))) return matches -- 2.11.4.GIT