Memoize settings hashes to speed up cache refresh

Memoize `settings.config_hash` and `settings.effective_hash`. The
hashes will only be calculated on the first call to the respective
functions. These memoized values are invalidated when the settings
object becomes dirty, or the settings are loaded from the config file.

Reason for change: During a refresh of the root route (/), the hash of
`settings.effective.yaml` is calculated 5 times. Each call lead to a
large configuration object to be serialized to a YAML string, which would
then be hashed. As it turns out, serializing to YAML is expensive.

Note: while I do not see `settings.config_hash` hit during a refresh,
but making a change to it was easy and appropriate. It's possible that
other areas of the application may see some benefit.

Performance (on my 2016 Macbook Pro:
- building of initial cache on startup:
  - before: ~3.4s
  - after:  ~2.9s
- loading `http://octoprint/?_refresh=1`
  - before: ~1100ms
  - after:  ~200ms
This commit is contained in:
Mike Ryan authored and Gina Häußge committed 2020-04-06 19:01:34 +02:00
1 parent 01574aadfe
commit 02ec6c5a47
2 files changed
+31 -17

No files matched your search

+2 -3
View File
@@ -1385,7 +1385,7 @@ class SettingsCheckUpdater(webassets.updater.BaseUpdater):
return False
cache_key = ('octo', 'settings')
current_hash = webassets.utils.hash_func(json.dumps(settings().effective_yaml))
current_hash = settings().effective_hash
cached_hash = ctx.cache.get(cache_key)
# This may seem counter-intuitive, but if no cache entry is found
# then we actually return "no update needed". This is because
@@ -1403,8 +1403,7 @@ class SettingsCheckUpdater(webassets.updater.BaseUpdater):
return
cache_key = ('octo', 'settings')
cache_value = webassets.utils.hash_func(json.dumps(settings().effective_yaml))
ctx.cache.set(cache_key, cache_value)
ctx.cache.set(cache_key, settings().effective_hash)
##~~ core assets collector
def collect_core_assets(enable_gcodeviewer=True, preferred_stylesheet="css"):
+29 -14
View File
@@ -646,6 +646,8 @@ class Settings(object):
self._config = None
self._dirty = False
self._dirty_time = 0
self._last_config_hash = None
self._last_effective_hash = None
self._mtime = None
self._get_preprocessors = dict(
@@ -843,6 +845,15 @@ class Settings(object):
return list(map(process_control, controls))
def _forget_hashes(self):
self._last_config_hash = None
self._last_effective_hash = None
def _mark_dirty(self):
self._dirty = True
self._dirty_time = time.time()
self._forget_hashes()
@property
def effective(self):
return self._map.deep_dict()
@@ -854,10 +865,14 @@ class Settings(object):
@property
def effective_hash(self):
if self._last_effective_hash is not None:
return self._last_effective_hash
import hashlib
hash = hashlib.md5()
hash.update(self.effective_yaml.encode('utf-8'))
return hash.hexdigest()
self._last_effective_hash = hash.hexdigest()
return self._last_effective_hash
@property
def config_yaml(self):
@@ -866,10 +881,14 @@ class Settings(object):
@property
def config_hash(self):
if self._last_config_hash:
return self._last_config_hash
import hashlib
hash = hashlib.md5()
hash.update(self.config_yaml.encode('utf-8'))
return hash.hexdigest()
self._last_config_hash = hash.hexdigest()
return self._last_config_hash
@property
def _config(self):
@@ -934,6 +953,8 @@ class Settings(object):
if migrate:
self._migrate_config()
self._forget_hashes()
def load_overlay(self, overlay, migrate=True):
config = None
@@ -1604,8 +1625,7 @@ class Settings(object):
del self._config["folder"][type]
if not len(self._config["folder"]):
del self._config["folder"]
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
self.save()
except KeyError:
pass
@@ -1653,8 +1673,7 @@ class Settings(object):
try:
chain.del_by_path(path)
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
except KeyError:
if error_on_path:
raise NoSuchSettingsPath()
@@ -1712,8 +1731,7 @@ class Settings(object):
if not force and in_defaults and in_local and default_value == value:
try:
chain.del_by_path(path)
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
except KeyError:
if error_on_path:
raise NoSuchSettingsPath()
@@ -1723,8 +1741,7 @@ class Settings(object):
chain.del_by_path(path)
else:
chain.set_by_path(path, value)
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
def setInt(self, path, value, **kwargs):
if value is None:
@@ -1786,8 +1803,7 @@ class Settings(object):
del self._config["folder"][type]
if not self._config["folder"]:
del self._config["folder"]
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
elif (path != currentPath and path != defaultPath) or force:
if validate:
_validate_folder(path, check_writable=True, deep_check_writable=True)
@@ -1795,8 +1811,7 @@ class Settings(object):
if "folder" not in self._config:
self._config["folder"] = {}
self._config["folder"][type] = path
self._dirty = True
self._dirty_time = time.time()
self._mark_dirty()
def saveScript(self, script_type, name, script):
script_folder = self.getBaseFolder("scripts")