Skip to content

Commit

Permalink
Try to add LRU
Browse files Browse the repository at this point in the history
  • Loading branch information
atuchin-m committed Jan 10, 2025
1 parent 5b56285 commit 402a804
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
35 changes: 34 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ selectors = { version = "0.23", optional = true }
serde_json = "1.0"
thiserror = "1.0"
flatbuffers = "24.12.23"
lru = "0.12.5"

[dev-dependencies]
criterion = "0.5"
Expand Down
52 changes: 42 additions & 10 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,46 @@ impl NetworkFilterList {
false
}


fn load_fb_filter(&self, index: usize) -> FlatNetworkFilterView<'_> {
let storage =
unsafe { fb::root_as_network_filter_list_unchecked(&self.flat_filters_buffer) };
let filters = storage.global_list();
let flat_filter = filters.get(index);
let mut filter = FlatNetworkFilterView::from(flat_filter);
filter.key = index as u64;
filter
}

// fn load_fb_filter(&self, index: usize) -> &FlatNetworkFilterView<'static> {
// // use std::num::NonZeroUsize;
// unsafe {
// static mut CACHE: Lazy<LruCache<usize, FlatNetworkFilterView<'static>>> =
// // Lazy::new(|| LruCache::new(NonZeroUsize::new(5000).unwrap()));
// Lazy::new(|| LruCache::unbounded());


// // Check if the filter is already in the cache
// if let Some(cached_filter) = CACHE.get(&index) {
// return cached_filter;
// }


// // If not in cache, load the filter
// let storage: fb::NetworkFilterList<'static> =
// fb::root_as_network_filter_list_unchecked(&self.flat_filters_buffer);
// let filters = storage.global_list();
// let flat_filter = filters.get(index);
// let mut filter: FlatNetworkFilterView<'static> = FlatNetworkFilterView::from(flat_filter);
// filter.key = index as u64;

// // Insert the loaded filter into the cache
// CACHE.put(index, filter);

// CACHE.get(&index).unwrap()
// }
// }

/// Returns the first found filter, if any, that matches the given request. The backing storage
/// has a non-deterministic order, so this should be used for any category of filters where a
/// match from each would be functionally equivalent. For example, if two different exception
Expand All @@ -912,17 +952,11 @@ impl NetworkFilterList {
return None;
}

let storage =
unsafe { fb::root_as_network_filter_list_unchecked(&self.flat_filters_buffer) };
let filters = storage.global_list();

if let Some(source_hostname_hashes) = request.source_hostname_hashes.as_ref() {
for token in source_hostname_hashes {
if let Some(filter_bucket) = self.flat_filter_map.get(token) {
for filter_index in filter_bucket {
let flat_filter = filters.get(*filter_index as usize);
let mut filter = FlatNetworkFilterView::from(&flat_filter);
filter.key = *filter_index as u64;
let filter = self.load_fb_filter(*filter_index as usize);

if filter.matches(request, regex_manager)
&& filter.tag.map_or(true, |t| active_tags.contains(t))
Expand All @@ -937,9 +971,7 @@ impl NetworkFilterList {
for token in request_tokens {
if let Some(filter_bucket) = self.flat_filter_map.get(token) {
for filter_index in filter_bucket {
let flat_filter = filters.get(*filter_index as usize);
let mut filter = FlatNetworkFilterView::from(&flat_filter);
filter.key = *filter_index as u64;
let filter = self.load_fb_filter(*filter_index as usize);

if filter.matches(request, regex_manager)
&& filter.tag.map_or(true, |t| active_tags.contains(t))
Expand Down
4 changes: 2 additions & 2 deletions src/filters/fb_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ pub struct FlatNetworkFilterView<'a> {
pub tag: Option<&'a str>,
}

impl<'a> From<&'a fb::NetworkFilter<'a>> for FlatNetworkFilterView<'a> {
impl<'a> From<fb::NetworkFilter<'a>> for FlatNetworkFilterView<'a> {
#[inline(always)]
fn from(filter: &'a fb::NetworkFilter<'a>) -> Self {
fn from(filter: fb::NetworkFilter<'a>) -> Self {
let opt_domains = filter.opt_domains().map(|domains| unsafe {
let bytes = domains.bytes();
std::slice::from_raw_parts(
Expand Down

0 comments on commit 402a804

Please sign in to comment.