From 8a48e5d2a5357d84e31f8d14b748dd7b94fe44ff Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Mon, 8 Oct 2018 23:28:36 -0700 Subject: [PATCH 1/2] Add benchmark for config cache --- tests/unit/config/test_configcache.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unit/config/test_configcache.py b/tests/unit/config/test_configcache.py index 91e2f22fa..7c0f6012f 100644 --- a/tests/unit/config/test_configcache.py +++ b/tests/unit/config/test_configcache.py @@ -50,3 +50,17 @@ def test_configcache_get_after_set(config_stub): assert not config.cache['auto_save.session'] config_stub.val.auto_save.session = True assert config.cache['auto_save.session'] + + +def test_configcache_naive_benchmark(config_stub, benchmark): + def _run_bench(): + for _i in range(10000): + # pylint: disable=pointless-statement + config.cache['tabs.padding'] + config.cache['tabs.indicator.width'] + config.cache['tabs.indicator.padding'] + config.cache['tabs.min_width'] + config.cache['tabs.max_width'] + config.cache['tabs.pinned.shrink'] + # pylint: enable=pointless-statement + benchmark(_run_bench) From 8bb2db5e56fb1553287bb1eb07f49f37438c8562 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Mon, 8 Oct 2018 23:31:07 -0700 Subject: [PATCH 2/2] Restructure cache to optimize critical path --- qutebrowser/config/configcache.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qutebrowser/config/configcache.py b/qutebrowser/config/configcache.py index a421ba85c..15f343478 100644 --- a/qutebrowser/config/configcache.py +++ b/qutebrowser/config/configcache.py @@ -46,7 +46,9 @@ class ConfigCache: self._cache[attr] = config.instance.get(attr) def __getitem__(self, attr: str) -> typing.Any: - if attr not in self._cache: + try: + return self._cache[attr] + except KeyError: assert not config.instance.get_opt(attr).supports_pattern self._cache[attr] = config.instance.get(attr) - return self._cache[attr] + return self._cache[attr]