From 4c854b435c08b7071ce6986034ffe60bb9218371 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Sat, 11 Sep 2021 07:31:59 -0400 Subject: [PATCH] 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) --- src/octoprint/settings.py | 31 +++++++++++-------------------- src/octoprint/util/__init__.py | 23 +++++++++++++++++++---- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 0ea890e49..0fb13d796 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -22,7 +22,6 @@ __author__ = "Gina Häußge " __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 diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 213e9e4f9..589db7d39 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -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