diff --git a/Cargo.toml b/Cargo.toml index cc0023bd..16f60c38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,7 +94,7 @@ harness = false default = ["embedded-domain-resolver", "full-regex-handling", "single-thread"] full-regex-handling = [] single-thread = [] # disables `Send` and `Sync` on `Engine`. -regex-debug-info = [] +debug-info = [] css-validation = ["cssparser", "selectors"] content-blocking = [] embedded-domain-resolver = ["addr"] # Requires setting an external domain resolver if disabled. diff --git a/src/blocker.rs b/src/blocker.rs index d83861bf..dfb8d62d 100644 --- a/src/blocker.rs +++ b/src/blocker.rs @@ -487,13 +487,13 @@ impl Blocker { regex_manager.set_discard_policy(new_discard_policy); } - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub fn discard_regex(&self, regex_id: u64) { let mut regex_manager = self.borrow_regex_manager(); regex_manager.discard_regex(regex_id); } - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub fn get_regex_debug_info(&self) -> crate::regex_manager::RegexDebugInfo { let regex_manager = self.borrow_regex_manager(); regex_manager.get_debug_info() diff --git a/src/engine.rs b/src/engine.rs index bc99908b..df1d7ed1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -58,6 +58,13 @@ pub struct Engine { filter_data_context: FilterDataContextRef, } +#[cfg(feature = "debug-info")] +pub struct EngineDebugInfo { + pub regex_debug_info: crate::regex_manager::RegexDebugInfo, + + pub flatbuffer_size: usize, +} + impl Default for Engine { fn default() -> Self { Self::from_filter_set(FilterSet::new(false), false) @@ -241,14 +248,17 @@ impl Engine { self.blocker.set_regex_discard_policy(new_discard_policy); } - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub fn discard_regex(&mut self, regex_id: u64) { self.blocker.discard_regex(regex_id); } - #[cfg(feature = "regex-debug-info")] - pub fn get_regex_debug_info(&self) -> crate::regex_manager::RegexDebugInfo { - self.blocker.get_regex_debug_info() + #[cfg(feature = "debug-info")] + pub fn get_debug_info(&self) -> EngineDebugInfo { + EngineDebugInfo { + regex_debug_info: self.blocker.get_regex_debug_info(), + flatbuffer_size: self.filter_data_context.memory.data().len(), + } } /// Serializes the `Engine` into a binary format so that it can be quickly reloaded later. diff --git a/src/regex_manager.rs b/src/regex_manager.rs index e54da304..20ff910e 100644 --- a/src/regex_manager.rs +++ b/src/regex_manager.rs @@ -39,7 +39,7 @@ const DEFAULT_DISCARD_UNUSED_TIME: Duration = Duration::from_secs(180); /// Reports [`RegexManager`] metrics that may be useful for creating an optimized /// [`RegexManagerDiscardPolicy`]. -#[cfg(feature = "regex-debug-info")] +#[cfg(feature = "debug-info")] pub struct RegexDebugInfo { /// Information about each regex contained in the [`RegexManager`]. pub regex_data: Vec, @@ -48,7 +48,7 @@ pub struct RegexDebugInfo { } /// Describes metrics about a single regex from the [`RegexManager`]. -#[cfg(feature = "regex-debug-info")] +#[cfg(feature = "debug-info")] pub struct RegexDebugEntry { /// Id for this particular regex, which is constant and unique for its lifetime. /// @@ -312,7 +312,7 @@ impl RegexManager { } /// Discard one regex, identified by its id from a [`RegexDebugEntry`]. - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub fn discard_regex(&mut self, regex_id: u64) { self.map .iter_mut() @@ -322,7 +322,7 @@ impl RegexManager { }); } - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub(crate) fn get_debug_regex_data(&self) -> Vec { use itertools::Itertools; self.map @@ -336,13 +336,13 @@ impl RegexManager { .collect_vec() } - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub(crate) fn get_compiled_regex_count(&self) -> usize { self.compiled_regex_count } /// Collect metrics that may be useful for creating an optimized [`RegexManagerDiscardPolicy`]. - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] pub fn get_debug_info(&self) -> RegexDebugInfo { RegexDebugInfo { regex_data: self.get_debug_regex_data(), diff --git a/tests/unit/engine.rs b/tests/unit/engine.rs index 674384bc..fe204ba9 100644 --- a/tests/unit/engine.rs +++ b/tests/unit/engine.rs @@ -218,6 +218,14 @@ mod tests { let mut engine = Engine::from_rules_parametrised(rules, Default::default(), false, true); let data = engine.serialize().to_vec(); + #[cfg(feature = "debug-info")] + { + let debug_info = engine.get_debug_info(); + let expected_size = 8_527_344_f32; + assert!(debug_info.flatbuffer_size >= (expected_size * 0.99) as usize); + assert!(debug_info.flatbuffer_size <= (expected_size * 1.01) as usize); + } + let expected_hash: u64 = if cfg!(feature = "css-validation") { 2942520321544562177 } else { diff --git a/tests/unit/filters/network_matchers.rs b/tests/unit/filters/network_matchers.rs index 54392eab..96be7d58 100644 --- a/tests/unit/filters/network_matchers.rs +++ b/tests/unit/filters/network_matchers.rs @@ -678,7 +678,7 @@ mod match_tests { #[test] #[ignore] // Not going to handle lookaround regexes - #[cfg(feature = "regex-debug-info")] + #[cfg(feature = "debug-info")] fn check_lookaround_regex_handled() { { let filter = r#"/^https?:\/\/([0-9a-z\-]+\.)?(9anime|animeland|animenova|animeplus|animetoon|animewow|gamestorrent|goodanime|gogoanime|igg-games|kimcartoon|memecenter|readcomiconline|toonget|toonova|watchcartoononline)\.[a-z]{2,4}\/(?!([Ee]xternal|[Ii]mages|[Ss]cripts|[Uu]ploads|ac|ajax|assets|combined|content|cov|cover|(img\/bg)|(img\/icon)|inc|jwplayer|player|playlist-cat-rss|static|thumbs|wp-content|wp-includes)\/)(.*)/$image,other,script,~third-party,xmlhttprequest,domain=~animeland.hu"#; diff --git a/tests/unit/regex_manager.rs b/tests/unit/regex_manager.rs index 8ffe1ff4..fdd9d107 100644 --- a/tests/unit/regex_manager.rs +++ b/tests/unit/regex_manager.rs @@ -1,4 +1,4 @@ -#[cfg(all(test, feature = "regex-debug-info"))] +#[cfg(all(test, feature = "debug-info"))] mod tests { use super::super::*;