From 03dc78f5c4cb6c5179ea0f20ff608f7db2036976 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 26 Sep 2017 17:51:55 +0300 Subject: [PATCH 1/2] bpo-28293: The regex cache no longer completely dump when full. --- Lib/re.py | 14 ++++++++++++-- .../2017-09-26-17-51-17.bpo-28293.UC5pm4.rst | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst diff --git a/Lib/re.py b/Lib/re.py index d0ee5db175b5fa..657a4f6721ed59 100644 --- a/Lib/re.py +++ b/Lib/re.py @@ -128,6 +128,13 @@ 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", @@ -260,7 +267,7 @@ def escape(pattern): # -------------------------------------------------------------------- # internals -_cache = {} +_cache = OrderedDict() _pattern_type = type(sre_compile.compile("", 0)) @@ -281,7 +288,10 @@ def _compile(pattern, flags): p = sre_compile.compile(pattern, flags) if not (flags & DEBUG): if len(_cache) >= _MAXCACHE: - _cache.clear() + try: + _cache.popitem(False) + except KeyError: + pass _cache[type(pattern), pattern, flags] = p return p diff --git a/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst b/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst new file mode 100644 index 00000000000000..4c70e2424df65f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst @@ -0,0 +1 @@ +The regex cache no longer completely dump when full. From 7aaa5b7d547c94e59a7e51abc7f530280e817e0d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 26 Sep 2017 19:27:13 +0300 Subject: [PATCH 2/2] Improve wording. --- .../next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst b/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst index 4c70e2424df65f..16b92b05dbf630 100644 --- a/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst +++ b/Misc/NEWS.d/next/Library/2017-09-26-17-51-17.bpo-28293.UC5pm4.rst @@ -1 +1 @@ -The regex cache no longer completely dump when full. +The regular expression cache is no longer completely dumped when it is full.