Use an LRU cache for stylesheets

This commit is contained in:
Florian Bruhin 2014-08-28 09:51:54 +02:00
parent 5176dac391
commit 6d2acc244c
3 changed files with 12 additions and 30 deletions

View File

@ -394,7 +394,7 @@ class Application(QApplication):
status.prompt.prompter.ask_question, Qt.DirectConnection) status.prompt.prompter.ask_question, Qt.DirectConnection)
# config # config
self.config.style_changed.connect(style.invalidate_caches) self.config.style_changed.connect(style.get_stylesheet.cache_clear)
for obj in (tabs, completion, self.mainwindow, self.cmd_history, for obj in (tabs, completion, self.mainwindow, self.cmd_history,
websettings, kp[utypes.KeyMode.normal], self.modeman, websettings, kp[utypes.KeyMode.normal], self.modeman,
status, status.txt): status, status.txt):

View File

@ -17,12 +17,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Utilities related to the look&feel of qutebrowser. """Utilities related to the look&feel of qutebrowser."""
Module attributes:
_colordict: The global cached ColorDict.
_fontdict: The global cached FontDict.
"""
import functools import functools
@ -32,10 +27,7 @@ from qutebrowser.config import config
from qutebrowser.utils import log, utils from qutebrowser.utils import log, utils
_colordict = None @functools.lru_cache(maxsize=16)
_fontdict = None
def get_stylesheet(template): def get_stylesheet(template):
"""Format a stylesheet based on a template. """Format a stylesheet based on a template.
@ -45,12 +37,9 @@ def get_stylesheet(template):
Return: Return:
The formatted template as string. The formatted template as string.
""" """
global _colordict, _fontdict colordict = ColorDict(config.section('colors'))
if _colordict is None: fontdict = FontDict(config.section('fonts'))
_colordict = ColorDict(config.section('colors')) return template.strip().format(color=colordict, font=fontdict,
if _fontdict is None:
_fontdict = FontDict(config.section('fonts'))
return template.strip().format(color=_colordict, font=_fontdict,
config=config.instance()) config=config.instance())
@ -78,15 +67,6 @@ def _update_stylesheet(obj, _section, _option):
obj.setStyleSheet(get_stylesheet(obj.STYLESHEET)) obj.setStyleSheet(get_stylesheet(obj.STYLESHEET))
def invalidate_caches(section, _option):
"""Invalidate cached dicts."""
global _colordict, _fontdict
if section == 'colors':
_colordict = None
elif section == 'fonts':
_fontdict = None
class ColorDict(dict): class ColorDict(dict):
"""A dict aimed at Qt stylesheet colors.""" """A dict aimed at Qt stylesheet colors."""

View File

@ -29,7 +29,7 @@ from PyQt5.QtCore import pyqtRemoveInputHook, QEvent, QCoreApplication
from qutebrowser.utils import log, utils from qutebrowser.utils import log, utils
from qutebrowser.commands import cmdutils from qutebrowser.commands import cmdutils
from qutebrowser.config import config from qutebrowser.config import config, style
@cmdutils.register(debug=True, name='debug-set-trace') @cmdutils.register(debug=True, name='debug-set-trace')
@ -87,9 +87,11 @@ def debug_all_objects():
@cmdutils.register(debug=True) @cmdutils.register(debug=True)
def debug_cache_stats(): def debug_cache_stats():
"""Print config LRU cache stats.""" """Print LRU cache stats."""
info = config.instance().get.cache_info() config_info = config.instance().get.cache_info()
log.misc.debug(info) style_info = style.get_stylesheet.cache_info()
log.misc.debug('config: {}'.format(config_info))
log.misc.debug('style: {}'.format(style_info))
def log_events(klass): def log_events(klass):