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

perf: Reduced memory usage by removing unnecessary regex compilation #1047

Merged
merged 2 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGELOG-Japanese.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

**改善:**

- XXX
- 新たに変換されたルールを使用する際のメモリ使用量を半分に削減した。(#1047) (@fukusuket)

**バグ修正:**

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

**Enhancements:**

- XXX
- Reduced memory usage by half when using newly converted rules. (#1047) (@fukusuket)

**Bug Fixes:**

Expand Down
22 changes: 11 additions & 11 deletions src/detections/rule/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,6 @@ impl LeafMatcher for DefaultMatcher {
if n == 0 {
// パイプがないケース
self.fast_match = Self::convert_to_fast_match(&pattern, true);
if self.fast_match.is_some()
&& matches!(
&self.fast_match.as_ref().unwrap()[0],
FastMatch::Exact(_) | FastMatch::Contains(_)
)
&& !self.key_list.is_empty()
{
// FastMatch::Exact/Contains検索に置き換えられたときは正規表現は不要
return Result::Ok(());
}
} else if n == 1 {
// パイプがあるケース
self.fast_match = match &self.pipes[0] {
Expand Down Expand Up @@ -461,7 +451,16 @@ impl LeafMatcher for DefaultMatcher {
);
return Result::Err(vec![errmsg]);
}

if self.fast_match.is_some()
&& matches!(
&self.fast_match.as_ref().unwrap()[0],
FastMatch::Exact(_) | FastMatch::Contains(_)
)
&& !self.key_list.is_empty()
{
// FastMatch::Exact/Contains検索に置き換えられたときは正規表現は不要
return Result::Ok(());
}
let is_eqfield = self.pipes.iter().any(|pipe_element| {
matches!(
pipe_element,
Expand Down Expand Up @@ -2337,6 +2336,7 @@ mod tests {
assert!(!DefaultMatcher::ends_with_ignore_case("bc", "bcd").unwrap());
assert!(!DefaultMatcher::ends_with_ignore_case("bcd", "abc").unwrap());
}

#[test]
fn test_convert_to_fast_match() {
assert_eq!(DefaultMatcher::convert_to_fast_match("ab?", true), None);
Expand Down