Cache the completion delegate stylesheet

We removed various caches in b5eac744b5 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.
This commit is contained in:
Florian Bruhin 2017-09-27 23:16:40 +02:00
parent fac322058e
commit e1f3829383
2 changed files with 30 additions and 8 deletions

View File

@ -41,6 +41,7 @@ except ImportError:
import qutebrowser import qutebrowser
import qutebrowser.resources import qutebrowser.resources
from qutebrowser.completion import completiondelegate
from qutebrowser.completion.models import miscmodels from qutebrowser.completion.models import miscmodels
from qutebrowser.commands import cmdutils, runners, cmdexc from qutebrowser.commands import cmdutils, runners, cmdexc
from qutebrowser.config import (config, websettings, configexc, configfiles, from qutebrowser.config import (config, websettings, configexc, configfiles,
@ -414,6 +415,9 @@ def _init_modules(args, crash_handler):
pre_text='Error initializing SQL') pre_text='Error initializing SQL')
sys.exit(usertypes.Exit.err_init) sys.exit(usertypes.Exit.err_init)
log.init.debug("Initializing completion...")
completiondelegate.init()
log.init.debug("Initializing command history...") log.init.debug("Initializing command history...")
cmdhistory.init() cmdhistory.init()

View File

@ -34,6 +34,9 @@ from qutebrowser.config import config
from qutebrowser.utils import qtutils, jinja from qutebrowser.utils import qtutils, jinja
_cached_stylesheet = None
class CompletionItemDelegate(QStyledItemDelegate): class CompletionItemDelegate(QStyledItemDelegate):
"""Delegate used by CompletionView to draw individual items. """Delegate used by CompletionView to draw individual items.
@ -189,14 +192,8 @@ class CompletionItemDelegate(QStyledItemDelegate):
self._doc.setDefaultTextOption(text_option) self._doc.setDefaultTextOption(text_option)
self._doc.setDocumentMargin(2) self._doc.setDocumentMargin(2)
stylesheet = """ assert _cached_stylesheet is not None
.highlight { self._doc.setDefaultStyleSheet(_cached_stylesheet)
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))
if index.parent().isValid(): if index.parent().isValid():
view = self.parent() view = self.parent()
@ -283,3 +280,24 @@ class CompletionItemDelegate(QStyledItemDelegate):
self._draw_focus_rect() self._draw_focus_rect()
self._painter.restore() 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)