From cc061d0bbca9371e214a77f10e307f12418faba5 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Sat, 2 Apr 2022 01:38:36 +0700 Subject: [PATCH 1/3] Optimize raw_line field --- src/data_format/legacy.rs | 6 +++--- src/filters/cosmetic.rs | 4 ++-- src/filters/network.rs | 13 ++++++++----- src/optimizer.rs | 8 ++++---- tests/hashing_test.rs | 2 +- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/data_format/legacy.rs b/src/data_format/legacy.rs index 91e069b6..2e92bc36 100644 --- a/src/data_format/legacy.rs +++ b/src/data_format/legacy.rs @@ -37,7 +37,7 @@ struct NetworkFilterLegacySerializeFmt<'a> { csp: &'a Option, bug: &'a Option, tag: &'a Option, - raw_line: &'a Option, + raw_line: Option, id: &'a crate::utils::Hash, _fuzzy_signature: Option>, opt_domains_union: &'a Option, @@ -59,7 +59,7 @@ impl<'a, T> From<&'a T> for NetworkFilterLegacySerializeFmt<'a> where T: std::bo csp: &v.csp, bug: &v.bug, tag: &v.tag, - raw_line: &v.raw_line, + raw_line: if let Some(raw) = &v.raw_line { Some(*raw.clone()) } else { None }, id: &v.id, _fuzzy_signature: None, opt_domains_union: &v.opt_domains_union, @@ -191,7 +191,7 @@ impl From for NetworkFilter { csp: v.csp, bug: v.bug, tag: v.tag, - raw_line: v.raw_line, + raw_line: if let Some(raw) = v.raw_line { Some(Box::new(raw)) } else { None }, id: v.id, opt_domains_union: v.opt_domains_union, opt_not_domains_union: v.opt_not_domains_union, diff --git a/src/filters/cosmetic.rs b/src/filters/cosmetic.rs index 01ccd4ea..79e286f5 100644 --- a/src/filters/cosmetic.rs +++ b/src/filters/cosmetic.rs @@ -48,7 +48,7 @@ pub struct CosmeticFilter { pub mask: CosmeticFilterMask, pub not_entities: Option>, pub not_hostnames: Option>, - pub raw_line: Option, + pub raw_line: Option>, pub selector: String, pub key: Option, pub style: Option, @@ -303,7 +303,7 @@ impl CosmeticFilter { not_entities, not_hostnames, raw_line: if debug { - Some(String::from(line)) + Some(Box::new(String::from(line))) } else { None }, diff --git a/src/filters/network.rs b/src/filters/network.rs index f206950d..06083ff1 100644 --- a/src/filters/network.rs +++ b/src/filters/network.rs @@ -482,7 +482,7 @@ pub struct NetworkFilter { pub bug: Option, pub tag: Option, - pub raw_line: Option, + pub raw_line: Option>, pub id: Hash, @@ -862,7 +862,7 @@ impl NetworkFilter { opt_not_domains, tag, raw_line: if debug { - Some(String::from(line)) + Some(Box::new(String::from(line))) } else { None }, @@ -2571,7 +2571,8 @@ mod parse_tests { fn parses_hosts_style() { { let filter = NetworkFilter::parse_hosts_style("example.com", true).unwrap(); - assert_eq!(filter.raw_line, Some("||example.com^".to_string())); + assert!(filter.raw_line.is_some()); + assert_eq!(*filter.raw_line.clone().unwrap(), "||example.com^".to_string()); let mut defaults = default_network_filter_breakdown(); defaults.hostname = Some("example.com".to_string()); defaults.is_plain = true; @@ -2582,7 +2583,8 @@ mod parse_tests { } { let filter = NetworkFilter::parse_hosts_style("www.example.com", true).unwrap(); - assert_eq!(filter.raw_line, Some("||example.com^".to_string())); + assert!(filter.raw_line.is_some()); + assert_eq!(*filter.raw_line.clone().unwrap(), "||example.com^".to_string()); let mut defaults = default_network_filter_breakdown(); defaults.hostname = Some("example.com".to_string()); defaults.is_plain = true; @@ -2593,7 +2595,8 @@ mod parse_tests { } { let filter = NetworkFilter::parse_hosts_style("malware.example.com", true).unwrap(); - assert_eq!(filter.raw_line, Some("||malware.example.com^".to_string())); + assert!(filter.raw_line.is_some()); + assert_eq!(*filter.raw_line.clone().unwrap(), "||malware.example.com^".to_string()); let mut defaults = default_network_filter_breakdown(); defaults.hostname = Some("malware.example.com".to_string()); defaults.is_plain = true; diff --git a/src/optimizer.rs b/src/optimizer.rs index 3b10c72f..7fdb3283 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -106,11 +106,11 @@ impl Optimization for SimplePatternGroup { filter.mask.set(NetworkFilterMask::IS_COMPLETE_REGEX, is_complete_regex); if base_filter.raw_line.is_some() { - filter.raw_line = Some( + filter.raw_line = Some(Box::new( filters .iter() .flat_map(|f| f.raw_line.clone()) - .join(" <+> "), + .join(" <+> ")), ) } @@ -170,11 +170,11 @@ impl Optimization for UnionDomainGroup { if base_filter.raw_line.is_some() { - filter.raw_line = Some( + filter.raw_line = Some(Box::new( filters .iter() .flat_map(|f| f.raw_line.clone()) - .join(" <+> "), + .join(" <+> ")), ) } diff --git a/tests/hashing_test.rs b/tests/hashing_test.rs index 90f3e1ad..0038eb7c 100644 --- a/tests/hashing_test.rs +++ b/tests/hashing_test.rs @@ -32,7 +32,7 @@ fn check_rule_ids_no_collisions() { for filter in network_filters { let id = filter.get_id(); - let rule = filter.raw_line.unwrap_or_default(); + let rule = *filter.raw_line.unwrap_or_default(); let existing_rule = filter_ids.get(&id); assert!(existing_rule.is_none() || existing_rule.unwrap() == &rule, "ID {} for {} already present from {}", id, rule, existing_rule.unwrap()); filter_ids.insert(id, rule); From 5b3dce222630127429111a9781994a1def36e777 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Mon, 4 Apr 2022 11:28:52 +0700 Subject: [PATCH 2/3] Fix raw_line in tests --- src/lists.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 706fabb4..09a81d18 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -164,7 +164,7 @@ impl FilterSet { let mut filters_used = vec![]; self.network_filters.into_iter().for_each(|filter| { - let original_rule = filter.raw_line.clone().expect("All rules should be in debug mode"); + let original_rule = *filter.raw_line.clone().expect("All rules should be in debug mode"); if let Ok(equivalent) = TryInto::::try_into(filter) { filters_used.push(original_rule); equivalent.into_iter().for_each(|cb_rule| { @@ -179,7 +179,7 @@ impl FilterSet { let add_fp_document_exception = !filters_used.is_empty(); self.cosmetic_filters.into_iter().for_each(|filter| { - let original_rule = filter.raw_line.clone().expect("All rules should be in debug mode"); + let original_rule = *filter.raw_line.clone().expect("All rules should be in debug mode"); if let Ok(cb_rule) = TryInto::::try_into(filter) { filters_used.push(original_rule); match &cb_rule.action.typ { From fd9d533fda1c962df4506cc2d7c0f45706951ba7 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Mon, 4 Apr 2022 09:45:10 -0700 Subject: [PATCH 3/3] if let -> map --- src/data_format/legacy.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data_format/legacy.rs b/src/data_format/legacy.rs index 2e92bc36..fa0136cc 100644 --- a/src/data_format/legacy.rs +++ b/src/data_format/legacy.rs @@ -59,7 +59,7 @@ impl<'a, T> From<&'a T> for NetworkFilterLegacySerializeFmt<'a> where T: std::bo csp: &v.csp, bug: &v.bug, tag: &v.tag, - raw_line: if let Some(raw) = &v.raw_line { Some(*raw.clone()) } else { None }, + raw_line: v.raw_line.as_ref().map(|raw| *raw.clone()), id: &v.id, _fuzzy_signature: None, opt_domains_union: &v.opt_domains_union, @@ -191,7 +191,7 @@ impl From for NetworkFilter { csp: v.csp, bug: v.bug, tag: v.tag, - raw_line: if let Some(raw) = v.raw_line { Some(Box::new(raw)) } else { None }, + raw_line: v.raw_line.map(|raw| Box::new(raw)), id: v.id, opt_domains_union: v.opt_domains_union, opt_not_domains_union: v.opt_not_domains_union,