diff options
author | Raymond Hettinger <python@rcn.com> | 2011-02-02 08:37:11 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-02-02 08:37:11 +0000 |
commit | d57b4d3b31f80e2e72eebab7aea37378c4729d19 (patch) | |
tree | 271e93e548e5fee7e5c5b87d2bbd9be5d6e9dc44 /Lib/ConfigParser.py | |
parent | 820021ebd8ff50b3fe70e1354b4d50c1e0f68611 (diff) | |
download | cpython-git-d57b4d3b31f80e2e72eebab7aea37378c4729d19.tar.gz |
Issue 11089: Fix performance bug in ConfigParser that impaired its
usability for large config files.
The ConfigParser.get() method should have been O(1) but had
O(n) dict copies and updates on every call. This was
exacerbated by using OrderedDicts which do not copy or
update nearly as fast as regular dicts which are coded in C.
Diffstat (limited to 'Lib/ConfigParser.py')
-rw-r--r-- | Lib/ConfigParser.py | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py index dc7398841b..6cf3c2ac5d 100644 --- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -545,6 +545,38 @@ class RawConfigParser: if isinstance(val, list): options[name] = '\n'.join(val) +import UserDict as _UserDict + +class _Chainmap(_UserDict.DictMixin): + """Combine multiple mappings for successive lookups. + + For example, to emulate Python's normal lookup sequence: + + import __builtin__ + pylookup = _Chainmap(locals(), globals(), vars(__builtin__)) + """ + + def __init__(self, *maps): + self._maps = maps + + def __getitem__(self, key): + for mapping in self._maps: + try: + return mapping[key] + except KeyError: + pass + raise KeyError(key) + + def keys(self): + result = [] + seen = set() + for mapping in self_maps: + for key in mapping: + if key not in seen: + result.append(key) + seen.add(key) + return result + class ConfigParser(RawConfigParser): def get(self, section, option, raw=False, vars=None): @@ -559,16 +591,18 @@ class ConfigParser(RawConfigParser): The section DEFAULT is special. """ - d = self._defaults.copy() + sectiondict = {} try: - d.update(self._sections[section]) + sectiondict = self._sections[section] except KeyError: if section != DEFAULTSECT: raise NoSectionError(section) # Update with the entry specific variables + vardict = {} if vars: for key, value in vars.items(): - d[self.optionxform(key)] = value + vardict[self.optionxform(key)] = value + d = _Chainmap(vardict, sectiondict, self._defaults) option = self.optionxform(option) try: value = d[option] |