From fb19d62e4208a85e91a10bccb17d2ac13b082dae Mon Sep 17 00:00:00 2001 From: wunder957 Date: Mon, 28 Aug 2023 18:32:07 +0800 Subject: [PATCH] Add cache on regex --- duetector/filters/pattern.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/duetector/filters/pattern.py b/duetector/filters/pattern.py index a03bb60..3f04943 100644 --- a/duetector/filters/pattern.py +++ b/duetector/filters/pattern.py @@ -28,6 +28,7 @@ class PatternFilter(Filter): 0, ], } + _re_cache = {} @property def enable_customize_exclude(self) -> bool: @@ -50,7 +51,13 @@ def re_exclude(self, field: Optional[str], re_list: Union[str, List[str]]) -> bo return False if isinstance(re_list, str): re_list = [re_list] - return any(re.search(pattern, field) for pattern in re_list) + + def _cached_search(pattern, field): + if pattern not in self._re_cache: + self._re_cache[pattern] = re.compile(pattern) + return self._re_cache[pattern].search(field) + + return any(_cached_search(pattern, field) for pattern in re_list) def filter(self, data: NamedTuple) -> Optional[NamedTuple]: if getattr(data, "pid", None) == os.getpid():