[Python-checkins] cpython (merge 3.2 -> 3.3): Issue #16564: Fixed a performance regression relative to Python 3.1 in the

serhiy.storchaka python-checkins at python.org
Sat Mar 16 21:56:39 CET 2013


http://hg.python.org/cpython/rev/4b4dddd670d0
changeset:   82704:4b4dddd670d0
branch:      3.3
parent:      82701:858fa8b5d5d4
parent:      82703:14fe7a98b89c
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Mar 16 22:53:48 2013 +0200
summary:
  Issue #16564: Fixed a performance regression relative to Python 3.1 in the
caching of compiled regular expressions.

files:
  Lib/re.py |  30 ++++++++++++++++++++++++------
  Misc/NEWS |   3 +++
  2 files changed, 27 insertions(+), 6 deletions(-)


diff --git a/Lib/re.py b/Lib/re.py
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -215,8 +215,8 @@
 
 def purge():
     "Clear the regular expression caches"
-    _compile.cache_clear()
-    _compile_repl.cache_clear()
+    _cache.clear()
+    _cache_repl.clear()
 
 def template(pattern, flags=0):
     "Compile a template pattern, returning a pattern object"
@@ -259,11 +259,18 @@
 # --------------------------------------------------------------------
 # internals
 
+_cache = {}
+_cache_repl = {}
+
 _pattern_type = type(sre_compile.compile("", 0))
 
- at functools.lru_cache(maxsize=512, typed=True)
+_MAXCACHE = 512
 def _compile(pattern, flags):
     # internal: compile pattern
+    try:
+        return _cache[type(pattern), pattern, flags]
+    except KeyError:
+        pass
     if isinstance(pattern, _pattern_type):
         if flags:
             raise ValueError(
@@ -271,12 +278,23 @@
         return pattern
     if not sre_compile.isstring(pattern):
         raise TypeError("first argument must be string or compiled pattern")
-    return sre_compile.compile(pattern, flags)
+    p = sre_compile.compile(pattern, flags)
+    if len(_cache) >= _MAXCACHE:
+        _cache.clear()
+    _cache[type(pattern), pattern, flags] = p
+    return p
 
- at functools.lru_cache(maxsize=512)
 def _compile_repl(repl, pattern):
     # internal: compile replacement pattern
-    return sre_parse.parse_template(repl, 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
 
 def _expand(pattern, match, template):
     # internal: match.expand implementation hook
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -193,6 +193,9 @@
 Library
 -------
 
+- Issue #16564: Fixed a performance regression relative to Python 3.1 in the
+  caching of compiled regular expressions.
+
 - Issue #17431: Fix missing import of BytesFeedParser in email.parser.
 
 - Issue #1285086: Get rid of the refcounting hack and speed up

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


More information about the Python-checkins mailing list