Skip to content
Closed
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 14 additions & 4 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions src/regex_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegexDebugEntry>,
Expand All @@ -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.
///
Expand Down Expand Up @@ -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()
Expand All @@ -322,7 +322,7 @@ impl RegexManager {
});
}

#[cfg(feature = "regex-debug-info")]
#[cfg(feature = "debug-info")]
pub(crate) fn get_debug_regex_data(&self) -> Vec<RegexDebugEntry> {
use itertools::Itertools;
self.map
Expand All @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/filters/network_matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"#;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/regex_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(all(test, feature = "regex-debug-info"))]
#[cfg(all(test, feature = "debug-info"))]
mod tests {
use super::super::*;

Expand Down
Loading