[Python-checkins] cpython (3.6): Issue #28193: Use lru_cache in the re module.

raymond.hettinger python-checkins at python.org
Sun Sep 18 23:17:57 EDT 2016


https://hg.python.org/cpython/rev/88110cfbf4dc
changeset:   103944:88110cfbf4dc
branch:      3.6
parent:      103942:99c2861918a1
user:        Raymond Hettinger <python at rcn.com>
date:        Sun Sep 18 20:17:21 2016 -0700
summary:
  Issue #28193: Use lru_cache in the re module.

files:
  Lib/re.py |  15 ++++-----------
  1 files changed, 4 insertions(+), 11 deletions(-)


diff --git a/Lib/re.py b/Lib/re.py
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -122,6 +122,7 @@
 import enum
 import sre_compile
 import sre_parse
+import functools
 try:
     import _locale
 except ImportError:
@@ -234,7 +235,7 @@
 def purge():
     "Clear the regular expression caches"
     _cache.clear()
-    _cache_repl.clear()
+    _compile_repl.cache_clear()
 
 def template(pattern, flags=0):
     "Compile a template pattern, returning a pattern object"
@@ -278,7 +279,6 @@
 # internals
 
 _cache = {}
-_cache_repl = {}
 
 _pattern_type = type(sre_compile.compile("", 0))
 
@@ -311,17 +311,10 @@
         _cache[type(pattern), pattern, flags] = p, loc
     return p
 
+ at functools.lru_cache(_MAXCACHE)
 def _compile_repl(repl, pattern):
     # internal: compile replacement pattern
-    try:
-        return _cache_repl[repl, pattern]
-    except KeyError:
-        pass
-    p = sre_parse.parse_template(repl, pattern)
-    if len(_cache_repl) >= _MAXCACHE:
-        _cache_repl.clear()
-    _cache_repl[repl, pattern] = p
-    return p
+    return sre_parse.parse_template(repl, pattern)
 
 def _expand(pattern, match, template):
     # internal: match.expand implementation hook

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list