From 795e0003f277a76778a005f0bad6dc1484ae01f0 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Thu, 6 Jun 2019 19:30:51 +0200 Subject: icebox: Add helper functions to LRU cache regular expression results --- icebox/icebox.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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() -- cgit v1.2.3