From 9307cf86fa6b55b646050c4a2951836b710c030c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 11 Jul 2017 21:06:53 +0200 Subject: [PATCH] Add a cache for rendered stylesheets Otherwise, when showing hints a few times, we spend around 8-10s just in jinja generating stylesheets. --- qutebrowser/config/config.py | 15 +++++++++++---- qutebrowser/misc/utilcmds.py | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 6f5d42dfa..97c536dc3 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -551,6 +551,15 @@ def set_register_stylesheet(obj, *, stylesheet=None, update=True): observer.register(update=update) +@functools.lru_cache() +def _render_stylesheet(stylesheet): + """Render the given stylesheet jinja template.""" + # Imported here to avoid a Python 3.4 circular import + from qutebrowser.utils import jinja + template = jinja.environment.from_string(stylesheet) + return template.render(conf=val) + + class StyleSheetObserver(QObject): """Set the stylesheet on the given object and update it on changes. @@ -574,10 +583,7 @@ class StyleSheetObserver(QObject): Return: The formatted template as string. """ - # Imported here to avoid a Python 3.4 circular import - from qutebrowser.utils import jinja - template = jinja.environment.from_string(self._stylesheet) - return template.render(conf=val) + return _render_stylesheet(self._stylesheet) @pyqtSlot() def _update_stylesheet(self): @@ -613,6 +619,7 @@ def init(parent=None): val = ConfigContainer(instance) key_instance = KeyConfig(instance) + instance.changed.connect(_render_stylesheet.cache_clear) configtypes.Font.monospace_fonts = val.fonts.monospace config_commands = ConfigCommands(instance, key_instance) diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index d5e55c063..10caa4cfb 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -37,7 +37,7 @@ from PyQt5.QtWidgets import QApplication # pylint: disable=unused-import from qutebrowser.browser import qutescheme from qutebrowser.utils import log, objreg, usertypes, message, debug, utils from qutebrowser.commands import cmdutils, runners, cmdexc -from qutebrowser.config import configdata +from qutebrowser.config import config, configdata from qutebrowser.misc import consolewidget @@ -169,7 +169,9 @@ def debug_all_objects(): def debug_cache_stats(): """Print LRU cache stats.""" prefix_info = configdata.is_valid_prefix.cache_info() + render_stylesheet_info = config._render_stylesheet.cache_info() log.misc.debug('is_valid_prefix: {}'.format(prefix_info)) + log.misc.debug('_render_stylesheet: {}'.format(render_stylesheet_info)) @cmdutils.register(debug=True)