diff -r 9552f8af321e Lib/re.py --- a/Lib/re.py Wed Sep 28 07:53:32 2016 +0300 +++ b/Lib/re.py Wed Sep 28 09:11:57 2016 +0300 @@ -128,6 +128,13 @@ try: except ImportError: _locale = None +# try _collections first to reduce startup cost +try: + from _collections import OrderedDict +except ImportError: + from collections import OrderedDict + + # public symbols __all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", @@ -278,7 +285,7 @@ def escape(pattern): # -------------------------------------------------------------------- # internals -_cache = {} +_cache = OrderedDict() _pattern_type = type(sre_compile.compile("", 0)) @@ -301,7 +308,9 @@ def _compile(pattern, flags): p = sre_compile.compile(pattern, flags) if not (flags & DEBUG): if len(_cache) >= _MAXCACHE: - _cache.clear() + for k in _cache: + del _cache[k] + break if p.flags & LOCALE: if not _locale: return p