Improve settings processing time

This brings the load time of /api/settings on my crappy ARM board from
1s to 0.4s

This is done by

- optimizizing the hot loop inside deep_dict, which provides most the
gain (to 0.46s)
- using pickle for deep-copying, which is fast when doing lots of
pointer chasing, but slow when doing long copies (to 0.37s)
This commit is contained in:
Flaviu Tamas authored and Gina Häußge committed 2021-09-15 18:17:42 +02:00
1 parent d3bd4a895d
commit 4c854b435c
2 files changed
+30 -24

No files matched your search

+11 -20
View File
@@ -22,7 +22,6 @@ __author__ = "Gina Häußge <osd@foosel.net>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
import copy
import fnmatch
import io
import logging
@@ -54,6 +53,7 @@ from octoprint.util import (
CaseInsensitiveSet,
atomic_write,
dict_merge,
fast_deepcopy,
generate_api_key,
is_hidden_path,
yaml,
@@ -542,17 +542,14 @@ class DuplicateFolderPaths(InvalidSettings):
class HierarchicalChainMap(ChainMap):
def deep_dict(self, root=None):
if root is None:
root = self
def deep_dict(self):
def deep_dict_inner(root):
return {
key: deep_dict_inner(root[key]) if isinstance(value, dict) else value
for key, value in root.items()
}
result = {}
for key, value in root.items():
if isinstance(value, dict):
result[key] = self.deep_dict(root=self.__class__._get_next(key, root))
else:
result[key] = value
return result
return deep_dict_inner(self)
def has_path(self, path, only_local=False, only_defaults=False):
if only_defaults:
@@ -1778,19 +1775,13 @@ class Settings(object):
except KeyError:
raise NoSuchSettingsPath()
if preprocessors is not None:
try:
preprocessor = self._get_by_path(path, preprocessors)
except Exception:
pass
if callable(preprocessor):
value = preprocessor(value)
if callable(preprocessor):
value = preprocessor(value)
if do_copy:
if isinstance(value, KeysView):
value = list(value)
value = copy.deepcopy(value)
value = fast_deepcopy(value)
if asdict:
results[key] = value
+19 -4
View File
@@ -46,6 +46,11 @@ try:
except ImportError:
import Queue as queue
try:
import cPickle as pickle
except ImportError:
import pickle
# noinspection PyCompatibility
from past.builtins import basestring, unicode
@@ -539,6 +544,18 @@ def is_running_from_source():
)
def fast_deepcopy(obj):
# the best way to implement this would be as a C module, that way we'd be able to use
# the fast path every time.
try:
# implemented in C and much faster than deepcopy:
# https://stackoverflow.com/a/29385667
return pickle.loads(pickle.dumps(obj, -1))
except AttributeError:
# fall back when something unpickable is found
return copy.deepcopy(obj)
def dict_merge(a, b, leaf_merger=None, in_place=False):
"""
Recursively deep-merges two dictionaries.
@@ -587,8 +604,6 @@ def dict_merge(a, b, leaf_merger=None, in_place=False):
dict: ``b`` deep-merged into ``a``
"""
from copy import deepcopy
if a is None:
a = {}
if b is None:
@@ -600,7 +615,7 @@ def dict_merge(a, b, leaf_merger=None, in_place=False):
if in_place:
result = a
else:
result = deepcopy(a)
result = fast_deepcopy(a)
for k, v in b.items():
if k in result and isinstance(result[k], dict):
@@ -617,7 +632,7 @@ def dict_merge(a, b, leaf_merger=None, in_place=False):
pass
if merged is None:
merged = deepcopy(v)
merged = fast_deepcopy(v)
result[k] = merged
return result