diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2010-08-09 07:30:53 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2010-08-09 07:30:53 (GMT) |
commit | 9f347ea5457604694a8e1f94e18e9d00e85bded7 (patch) | |
tree | 775809513c7e344a5759501ddad5b15507f9b87b /Lib/re.py | |
parent | 5c87c1a5a843ab5d09edc279e506b6b8a1c3d3d2 (diff) | |
download | cpython-9f347ea5457604694a8e1f94e18e9d00e85bded7.zip cpython-9f347ea5457604694a8e1f94e18e9d00e85bded7.tar.gz cpython-9f347ea5457604694a8e1f94e18e9d00e85bded7.tar.bz2 |
reapply the revert made in r83875
Now the _collections is statically built, the build dependencies are in proper
order and build works fine.
Commit Log from r83874:
Issue 9396. Apply functools.lru_cache in the place of the
random flushing cache in the re module.
Diffstat (limited to 'Lib/re.py')
-rw-r--r-- | Lib/re.py | 32 |
1 files changed, 12 insertions, 20 deletions
@@ -118,6 +118,7 @@ This module also defines an exception 'error'. import sys import sre_compile import sre_parse +import functools # public symbols __all__ = [ "match", "search", "sub", "subn", "split", "findall", @@ -205,9 +206,9 @@ def compile(pattern, flags=0): return _compile(pattern, flags) def purge(): - "Clear the regular expression cache" - _cache.clear() - _cache_repl.clear() + "Clear the regular expression caches" + _compile_typed.clear() + _compile_repl.clear() def template(pattern, flags=0): "Compile a template pattern, returning a pattern object" @@ -289,12 +290,12 @@ def _shrink_cache(cache_dict, max_length, divisor=5): # Ignore problems if the cache changed from another thread. pass -def _compile(*key): +def _compile(*args): + return _compile_typed(type(args[0]), *args) + +@functools.lru_cache(maxsize=_MAXCACHE) +def _compile_typed(type, *key): # internal: compile pattern - cachekey = (type(key[0]),) + key - p = _cache.get(cachekey) - if p is not None: - return p pattern, flags = key if isinstance(pattern, _pattern_type): if flags: @@ -303,23 +304,14 @@ def _compile(*key): return pattern if not sre_compile.isstring(pattern): raise TypeError("first argument must be string or compiled pattern") - p = sre_compile.compile(pattern, flags) - if len(_cache) >= _MAXCACHE: - _shrink_cache(_cache, _MAXCACHE) - _cache[cachekey] = p + return sre_compile.compile(pattern, flags) return p +@functools.lru_cache(maxsize=_MAXCACHE) def _compile_repl(*key): # internal: compile replacement pattern - p = _cache_repl.get(key) - if p is not None: - return p repl, pattern = key - p = sre_parse.parse_template(repl, pattern) - if len(_cache_repl) >= _MAXCACHE: - _shrink_cache(_cache_repl, _MAXCACHE) - _cache_repl[key] = p - return p + return sre_parse.parse_template(repl, pattern) def _expand(pattern, match, template): # internal: match.expand implementation hook |