Add a cache for rendered stylesheets

Otherwise, when showing hints a few times, we spend around 8-10s just in jinja
generating stylesheets.
This commit is contained in:
Florian Bruhin 2017-07-11 21:06:53 +02:00
parent a91e6c3405
commit 9307cf86fa
2 changed files with 14 additions and 5 deletions

View File

@ -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)

View File

@ -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)