comparison configmix/config.py @ 499:f46932e8a84c

Configuration._lookupvar() now uses an internal cache (positive and negative). This give a nice performance boost.
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 18 Dec 2021 00:43:36 +0100
parents 8e516f17cf95
children 34cd4f111134
comparison
equal deleted inserted replaced
498:e7c82706b67a 499:f46932e8a84c
31 from .compat import u, uchr, n, str_and_u, PY2 31 from .compat import u, uchr, n, str_and_u, PY2
32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER 32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER
33 33
34 34
35 _MARKER = object() 35 _MARKER = object()
36 _MISSING = object()
36 37
37 38
38 class _AttributeDict(ConfigurationBase): 39 class _AttributeDict(ConfigurationBase):
39 40
40 def __getattr__(self, name): 41 def __getattr__(self, name):
261 """ 262 """
262 263
263 is_jail = False 264 is_jail = False
264 """Flag to show that this is not a jail for another configuration""" 265 """Flag to show that this is not a jail for another configuration"""
265 266
267 def __init__(self, *args, **kwds):
268 #
269 # PY2.7 compat: must be set before calling the superclass' __init__
270 #
271 self.__cache = {}
272 super(Configuration, self).__init__(*args, **kwds)
273
266 def __getitem__(self, key): 274 def __getitem__(self, key):
267 """Mapping and list interface that forwards to :meth:`~.getvarl_s` 275 """Mapping and list interface that forwards to :meth:`~.getvarl_s`
268 276
269 """ 277 """
270 if isinstance(key, (tuple, list)): 278 if isinstance(key, (tuple, list)):
578 :raise KeyError: An unexisting `path` raises a `KeyError` 586 :raise KeyError: An unexisting `path` raises a `KeyError`
579 587
580 """ 588 """
581 if not path: 589 if not path:
582 return self 590 return self
591 v = self.__cache.get(path, _MARKER)
592 if v is not _MARKER:
593 if v is _MISSING:
594 raise KeyError(
595 "Configuration variable %r not found"
596 " (negative internal cache value)" % (path,))
597 else:
598 return v
583 eiref = self.expand_if_reference 599 eiref = self.expand_if_reference
584 try: 600 try:
585 v = eiref(super(Configuration, self).__getitem__(path[0])) 601 v = eiref(super(Configuration, self).__getitem__(path[0]))
586 for p in path[1:]: 602 for p in path[1:]:
587 v = eiref(v[p]) 603 v = eiref(v[p])
588 except TypeError: 604 except TypeError:
605 self.__cache[path] = _MISSING
589 raise KeyError( 606 raise KeyError(
590 "Configuration variable %r not found" 607 "Configuration variable %r not found"
591 "(missing intermediate keys?)" % (path,)) 608 "(missing intermediate keys?)" % (path,))
609 except KeyError:
610 self.__cache[path] = _MISSING
611 raise
612 self.__cache[path] = v
592 return v 613 return v
593 614
594 def _lookupref(self, key, default=_MARKER): 615 def _lookupref(self, key, default=_MARKER):
595 """ 616 """
596 617