Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache on regex #26

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion duetector/filters/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PatternFilter(Filter):
0,
],
}
_re_cache = {}

@property
def enable_customize_exclude(self) -> bool:
Expand All @@ -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():
Expand Down