From e1f38293835a3f9e2874abeeb9e6c3ad741b7dfa Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 27 Sep 2017 23:16:40 +0200 Subject: [PATCH] Cache the completion delegate stylesheet We removed various caches in b5eac744b5a24263d1b595e52e48907687b2d268 but the completion delegate stylesheet gets rendered a lot, causing things to slow down. The rendering takes around 1ms, but it gets done ~10k times with a simple profiling run, so that adds up quickly. We don't use a functools.lru_cache here as the stylesheet template never changes. Thanks a lot to gilbertw1 for tracking this down! See #2812 - there's probably more possible, but this should fix the performance regression some people saw with the new config. --- qutebrowser/app.py | 4 +++ qutebrowser/completion/completiondelegate.py | 34 +++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 1f938c57e..85f0e9410 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -41,6 +41,7 @@ except ImportError: import qutebrowser import qutebrowser.resources +from qutebrowser.completion import completiondelegate from qutebrowser.completion.models import miscmodels from qutebrowser.commands import cmdutils, runners, cmdexc from qutebrowser.config import (config, websettings, configexc, configfiles, @@ -414,6 +415,9 @@ def _init_modules(args, crash_handler): pre_text='Error initializing SQL') sys.exit(usertypes.Exit.err_init) + log.init.debug("Initializing completion...") + completiondelegate.init() + log.init.debug("Initializing command history...") cmdhistory.init() diff --git a/qutebrowser/completion/completiondelegate.py b/qutebrowser/completion/completiondelegate.py index c59d81022..8248b3745 100644 --- a/qutebrowser/completion/completiondelegate.py +++ b/qutebrowser/completion/completiondelegate.py @@ -34,6 +34,9 @@ from qutebrowser.config import config from qutebrowser.utils import qtutils, jinja +_cached_stylesheet = None + + class CompletionItemDelegate(QStyledItemDelegate): """Delegate used by CompletionView to draw individual items. @@ -189,14 +192,8 @@ class CompletionItemDelegate(QStyledItemDelegate): self._doc.setDefaultTextOption(text_option) self._doc.setDocumentMargin(2) - stylesheet = """ - .highlight { - color: {{ conf.colors.completion.match.fg }}; - } - """ - with jinja.environment.no_autoescape(): - template = jinja.environment.from_string(stylesheet) - self._doc.setDefaultStyleSheet(template.render(conf=config.val)) + assert _cached_stylesheet is not None + self._doc.setDefaultStyleSheet(_cached_stylesheet) if index.parent().isValid(): view = self.parent() @@ -283,3 +280,24 @@ class CompletionItemDelegate(QStyledItemDelegate): self._draw_focus_rect() self._painter.restore() + + +@config.change_filter('colors.completion.match.fg', function=True) +def _update_stylesheet(): + """Update the cached stylesheet.""" + stylesheet = """ + .highlight { + color: {{ conf.colors.completion.match.fg }}; + } + """ + with jinja.environment.no_autoescape(): + template = jinja.environment.from_string(stylesheet) + + global _cached_stylesheet + _cached_stylesheet = template.render(conf=config.val) + + +def init(): + """Initialize the cached stylesheet.""" + _update_stylesheet() + config.instance.changed.connect(_update_stylesheet)