diff options
author | Michael Buesch <m@bues.ch> | 2019-06-06 19:30:51 +0200 |
---|---|---|
committer | Michael Buesch <m@bues.ch> | 2019-06-08 16:12:16 +0200 |
commit | 795e0003f277a76778a005f0bad6dc1484ae01f0 (patch) | |
tree | 381099c2bbdab568cb6e4d635e5c2c46d2e3b5ec /icebox | |
parent | 5f49bea71ce6b5362d0d9e99dc5c4910b6943a82 (diff) | |
download | icestorm-795e0003f277a76778a005f0bad6dc1484ae01f0.tar.gz icestorm-795e0003f277a76778a005f0bad6dc1484ae01f0.tar.bz2 icestorm-795e0003f277a76778a005f0bad6dc1484ae01f0.zip |
icebox: Add helper functions to LRU cache regular expression results
Diffstat (limited to 'icebox')
-rw-r--r-- | icebox/icebox.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/icebox/icebox.py b/icebox/icebox.py index 6445699..810a4f6 100644 --- a/icebox/icebox.py +++ b/icebox/icebox.py @@ -18,6 +18,32 @@ import iceboxdb import re, sys, functools + +if True: + # icebox uses lots of regular expressions. + # Supply cached versions of common re functions + # to avoid re-calculating regular expression results + # over and over again. + re_cache_sizes = 2**14 + + @functools.lru_cache(maxsize=re_cache_sizes) + def re_match_cached(*args): + return re.match(*args) + + @functools.lru_cache(maxsize=re_cache_sizes) + def re_sub_cached(*args): + return re.sub(*args) + + @functools.lru_cache(maxsize=re_cache_sizes) + def re_search_cached(*args): + return re.search(*args) +else: + # Disable regular expression caching. + re_match_cached = re.match + re_sub_cached = re.sub + re_search_cached = re.search + + class iceconfig: def __init__(self): self.clear() |