Add an lru cache for WebHistoryInterface.historyContains

When loading heise.de, for some crazy reason QtWebKit calls historyContains
about 16'000 times.

With this cache (which we simply clear when *any* page has been loaded, as then
the links which have been visited can change), that's down to 250 or so...
This commit is contained in:
Florian Bruhin 2017-06-08 13:03:44 +02:00 committed by Ryan Roden-Corrent
parent 6fc61d12fc
commit 9b0395db08
2 changed files with 10 additions and 0 deletions

View File

@ -19,6 +19,7 @@
"""QtWebKit specific part of history."""
import functools
from PyQt5.QtWebKit import QWebHistoryInterface
@ -36,11 +37,13 @@ class WebHistoryInterface(QWebHistoryInterface):
def __init__(self, webhistory, parent=None):
super().__init__(parent)
self._history = webhistory
self._history.changed.connect(self.historyContains.cache_clear)
def addHistoryEntry(self, url_string):
"""Required for a QWebHistoryInterface impl, obsoleted by add_url."""
pass
@functools.lru_cache(maxsize=32768)
def historyContains(self, url_string):
"""Called by WebKit to determine if a URL is contained in the history.

View File

@ -170,8 +170,15 @@ def debug_cache_stats():
"""Print LRU cache stats."""
config_info = objreg.get('config').get.cache_info()
style_info = style.get_stylesheet.cache_info()
try:
from PyQt5.QtWebKit import QWebHistoryInterface
interface = QWebHistoryInterface.defaultInterface()
history_info = interface.historyContains.cache_info()
except ImportError:
history_info = None
log.misc.debug('config: {}'.format(config_info))
log.misc.debug('style: {}'.format(style_info))
log.misc.debug('history: {}'.format(history_info))
@cmdutils.register(debug=True)