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

Optimize regexp memory usage #201

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 src/data_format/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl From<NetworkFilterLegacyDeserializeFmt> for NetworkFilter {
id: v.id,
opt_domains_union: v.opt_domains_union,
opt_not_domains_union: v.opt_not_domains_union,
regex: std::sync::Arc::new(std::sync::RwLock::new(None)),
regex: std::cell::RefCell::new(None),
}
}
}
Expand Down
28 changes: 11 additions & 17 deletions src/filters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use once_cell::sync::Lazy;
use crate::url_parser::parse_url;

use std::fmt;
use std::sync::{Arc, RwLock};
use std::cell::RefCell;
use std::sync::Arc;

use crate::request;
use crate::utils;
Expand Down Expand Up @@ -446,14 +447,13 @@ pub struct NetworkFilter {
pub opt_not_domains_union: Option<Hash>,

// Regex compild lazily, using "Interior Mutability"
// Arc (Atomic Reference Counter) allows for cloned NetworkFilters
// to point to the same RwLock and what is inside.
// RwLock allows for concurrent access when reading as well as writing
// from the inside.
// When the Regex hasn't been compiled, <None> is stored, afterwards Arc to Some<CompiledRegex>
// RefCell allows for cloned NetworkFilters to point to the same Option and what is inside.
// When the Regex hasn't been compiled, <None> is stored, afterwards Arc to CompiledRegex
// to avoid expensive cloning of the Regex itself.
// Non-thread safe, should be used from a single thread.
// Atomic Arc is used for the backward-compatibilty with the other code.
#[serde(skip_serializing, skip_deserializing)]
pub(crate) regex: Arc<RwLock<Option<Arc<CompiledRegex>>>>
pub(crate) regex: RefCell<Option<Arc<CompiledRegex>>>
}

// TODO - restrict the API so that this is always true - i.e. lazy-calculate IDs from actual data,
Expand Down Expand Up @@ -831,7 +831,7 @@ impl NetworkFilter {
id: utils::fast_hash(&line),
opt_domains_union,
opt_not_domains_union,
regex: Arc::new(RwLock::new(None))
regex: RefCell::new(None),
})
}

Expand Down Expand Up @@ -1057,23 +1057,17 @@ impl NetworkMatchable for NetworkFilter {
if !self.is_regex() && !self.is_complete_regex() {
return Arc::new(CompiledRegex::MatchAll);
}
// Create a new scope to contain the lifetime of the
// dynamic read borrow
{
let cache = self.regex.as_ref().read().unwrap();
if cache.is_some() {
return cache.as_ref().unwrap().clone();
}
if let Some(cache) = &*self.regex.borrow() {
return cache.clone();
}
let mut cache = self.regex.as_ref().write().unwrap();
let regex = compile_regex(
&self.filter,
self.is_right_anchor(),
self.is_left_anchor(),
self.is_complete_regex(),
);
let arc_regex = Arc::new(regex);
*cache = Some(arc_regex.clone());
*self.regex.borrow_mut() = Some(arc_regex.clone());
arc_regex
}
}
Expand Down