Skip to content

Flat buffers #416

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

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5d2f0d8
unit tests from network.rs moved to tests/unit/filters/network.rs
boocmp Jan 8, 2025
2892b25
unit tests from cosmetic.rs moved to tests/unit/filters/cosmetic.rs
boocmp Jan 8, 2025
ae41283
Removed unused files from url_parser.
boocmp Jan 8, 2025
b61adfe
unit tests from resource_storage.rs moved to tests/unit/filters/resou…
boocmp Jan 8, 2025
0d10ae9
unit tests from resource_assembler.rs moved to tests/unit/filters/res…
boocmp Jan 8, 2025
168ad77
unit tests from blocker.rs moved to tests/unit/blocker.rs
boocmp Jan 8, 2025
49e74da
unit tests from regex_manager.rs moved to tests/unit/regex_manager.rs
boocmp Jan 8, 2025
f586683
unit tests from content_blocking.rs moved to tests/unit/content_block…
boocmp Jan 8, 2025
e236f2e
unit tests from cosmetic_filter_cache.rs moved to tests/unit/cosmetic…
boocmp Jan 8, 2025
cbfd5d5
unit tests from engine.rs moved to tests/unit/engine.rs
boocmp Jan 8, 2025
0972777
unit tests from lists.rs moved to tests/unit/lists.rs
boocmp Jan 8, 2025
34ff68a
unit tests from request.rs moved to tests/unit/request.rs
boocmp Jan 8, 2025
140fba3
unit tests from optimizer.rs moved to tests/unit/optimizer.rs
boocmp Jan 8, 2025
9558e26
unit tests from utils.rs moved to tests/unit/utils.rs
boocmp Jan 8, 2025
a6fd9a5
Rust formatter.
boocmp Jan 21, 2025
39ffc44
AbstractNetworkFilter moved to abstract_network.rs.
boocmp Jan 8, 2025
175698e
The regex stuff moved from network.rs to regex_manager.rs
boocmp Jan 9, 2025
f837228
Key type of compiled regexes map changed to u64.
boocmp Jan 9, 2025
1a41937
Filter matching functions have beed moved to network_matchers.rs. Rem…
boocmp Jan 9, 2025
a9f3a71
Unit tests compilation and Rust-fmt. Matchers tests moved from networ…
boocmp Jan 9, 2025
1c979ce
Added flatbuffer network filters implementation.
boocmp Jan 9, 2025
5c976b4
Added url_lower_cased member in request to prevent the memory allocat…
boocmp Jan 9, 2025
7403547
Removed "object-pooling". request_tokens is a part of Request now.
boocmp Jan 12, 2025
f42ffed
Added checkable_tokens_iter
boocmp Jan 14, 2025
3e323b9
Tests compilation.
boocmp Jan 21, 2025
a03a531
NetworkFilterList moved to network_filter_list.rs
boocmp Jan 21, 2025
a5c4c1c
Added flatbuffer structure for network filters.
boocmp Jan 15, 2025
94a3382
Added NetworkFilterMaskHelper trait to provide bool getters for diffe…
boocmp Jan 16, 2025
7a6476c
Fixed fb_network.rs to match flat schema.
boocmp Jan 21, 2025
669ef56
Added NetworkFilterListTrait to provide flat impl later.
boocmp Jan 21, 2025
df8828b
Added list type generic parameter to the Blocker.
boocmp Jan 21, 2025
f98c9e1
Simplified NetworkFilterList impl.
boocmp Jan 21, 2025
f239499
Added FlatNetworkFilterList. Added FlatNetworkFilter. Implemented mat…
boocmp Jan 21, 2025
b66d65f
Temporary using NetworkFilterList for filters optimization.
boocmp Jan 21, 2025
fff0b48
Fixed filter's unique key for regex manager.
boocmp Jan 23, 2025
81a9343
Optimization for FlatNetworkFilterList.
boocmp Jan 23, 2025
92fcc49
Added check for flatbuffers feature.
boocmp Jan 24, 2025
bc8d2e3
Wrong conflicts due the rebase. Fixed.
boocmp Jan 29, 2025
880d83c
Rust fmt after rebase.
boocmp Jan 29, 2025
d2f45e5
Enable flatbuffers feature in perf CI
atuchin-m Jan 29, 2025
09b7b88
Added Serialize trait for engine.
boocmp Jan 29, 2025
7b5cfcb
Tests fixing and disabling.
boocmp Jan 30, 2025
a9521b8
Added build & test steps in GHWF.
boocmp Jan 30, 2025
a3159df
Disabled serialization tests for flatbuffers feature.
boocmp Jan 30, 2025
a2a8f5a
Changed 'Run Brave-specific tests' CI step.
boocmp Jan 30, 2025
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ cssparser = { version = "0.28", optional = true }
selectors = { version = "0.23", optional = true }
serde_json = "1.0"
thiserror = "1.0"
flatbuffers = "24.12.23"

[dev-dependencies]
criterion = "0.5"
Expand All @@ -55,6 +56,9 @@ sha2 = "0.9"
[lib]
bench = false

[profile.bench]
debug = true

[[bench]]
name = "bench_regex"
harness = false
Expand Down Expand Up @@ -98,3 +102,4 @@ css-validation = ["cssparser", "selectors"]
content-blocking = []
embedded-domain-resolver = ["addr"] # Requires setting an external domain resolver if disabled.
resource-assembler = []
flatbuffers = []
16 changes: 2 additions & 14 deletions benches/bench_cosmetic_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ fn by_classes_ids(c: &mut Criterion) {
let (_, cosmetic_filters) = parse_filters(&rules, false, FilterFormat::Standard);
let cfcache = CosmeticFilterCache::from_rules(cosmetic_filters);
let exceptions = Default::default();
b.iter(|| {
cfcache.hidden_class_id_selectors(
&["ad"],
&["ad"],
&exceptions,
)
})
b.iter(|| cfcache.hidden_class_id_selectors(&["ad"], &["ad"], &exceptions))
});
group.bench_function("many lists", move |b| {
let rules = rules_from_lists(&[
Expand All @@ -75,13 +69,7 @@ fn by_classes_ids(c: &mut Criterion) {
let (_, cosmetic_filters) = parse_filters(&rules, false, FilterFormat::Standard);
let cfcache = CosmeticFilterCache::from_rules(cosmetic_filters);
let exceptions = Default::default();
b.iter(|| {
cfcache.hidden_class_id_selectors(
&["ad"],
&["ad"],
&exceptions,
)
})
b.iter(|| cfcache.hidden_class_id_selectors(&["ad"], &["ad"], &exceptions))
});
group.bench_function("many matching classes and ids", move |b| {
let rules = rules_from_lists(&[
Expand Down
26 changes: 11 additions & 15 deletions benches/bench_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use criterion::*;

use serde::{Deserialize, Serialize};

use adblock::Engine;
use adblock::blocker::{Blocker, BlockerOptions};
use adblock::request::Request;
use adblock::resources::ResourceStorage;
use adblock::url_parser::parse_url;
use adblock::Engine;

#[path = "../tests/test_utils.rs"]
mod test_utils;
Expand Down Expand Up @@ -36,7 +36,7 @@ fn load_requests() -> Vec<TestRequest> {
reqs
}

fn get_blocker(rules: impl IntoIterator<Item=impl AsRef<str>>) -> Blocker {
fn get_blocker(rules: impl IntoIterator<Item = impl AsRef<str>>) -> Blocker {
let (network_filters, _) = adblock::lists::parse_filters(rules, false, Default::default());

let blocker_options = BlockerOptions {
Expand All @@ -61,7 +61,11 @@ fn bench_rule_matching(engine: &Engine, requests: &Vec<TestRequest>) -> (u32, u3
(matches, passes)
}

fn bench_matching_only(blocker: &Blocker, resources: &ResourceStorage, requests: &Vec<Request>) -> (u32, u32) {
fn bench_matching_only(
blocker: &Blocker,
resources: &ResourceStorage,
requests: &Vec<Request>,
) -> (u32, u32) {
let mut matches = 0;
let mut passes = 0;
requests.iter().for_each(|parsed| {
Expand Down Expand Up @@ -141,9 +145,7 @@ fn rule_match(c: &mut Criterion) {
fn rule_match_parsed_el(c: &mut Criterion) {
let mut group = c.benchmark_group("rule-match-parsed");

let rules = rules_from_lists(&[
"data/easylist.to/easylist/easylist.txt",
]);
let rules = rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]);
let requests = load_requests();
let requests_parsed: Vec<_> = requests
.into_iter()
Expand Down Expand Up @@ -221,9 +223,7 @@ fn serialization(c: &mut Criterion) {
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
});
group.bench_function("el", move |b| {
let full_rules = rules_from_lists(&[
"data/easylist.to/easylist/easylist.txt",
]);
let full_rules = rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]);

let engine = Engine::from_rules(full_rules, Default::default());
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
Expand Down Expand Up @@ -258,9 +258,7 @@ fn deserialization(c: &mut Criterion) {
})
});
group.bench_function("el", move |b| {
let full_rules = rules_from_lists(&[
"data/easylist.to/easylist/easylist.txt",
]);
let full_rules = rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]);

let engine = Engine::from_rules(full_rules, Default::default());
let serialized = engine.serialize_raw().unwrap();
Expand Down Expand Up @@ -294,9 +292,7 @@ fn rule_match_browserlike_comparable(c: &mut Criterion) {
group.throughput(Throughput::Elements(requests_len));
group.sample_size(20);

fn requests_parsed(
requests: &[TestRequest],
) -> Vec<(String, String, String, String, bool)> {
fn requests_parsed(requests: &[TestRequest]) -> Vec<(String, String, String, String, bool)> {
requests
.iter()
.map(|r| {
Expand Down
30 changes: 14 additions & 16 deletions benches/bench_redirect_performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use criterion::*;
use tokio::runtime::Runtime;

use adblock::blocker::{Blocker, BlockerOptions};
use adblock::filters::network::NetworkFilterMaskHelper;
use adblock::filters::network::{NetworkFilter, NetworkFilterMask};
use adblock::request::Request;
use adblock::resources::ResourceStorage;
Expand Down Expand Up @@ -66,9 +67,7 @@ fn get_redirect_rules() -> Vec<NetworkFilter> {
.into_iter()
.filter(NetworkFilter::is_redirect)
.filter(NetworkFilter::also_block_redirect)
.filter(|rule| {
rule.modifier_option.as_ref().unwrap() != "none"
})
.filter(|rule| rule.modifier_option.as_ref().unwrap() != "none")
.enumerate()
.map(|(index, mut rule)| {
rule.mask.insert(NetworkFilterMask::IS_LEFT_ANCHOR);
Expand Down Expand Up @@ -102,8 +101,8 @@ fn build_resources_for_filters(#[allow(unused)] filters: &[NetworkFilter]) -> Re

#[cfg(feature = "resource-assembler")]
{
use std::path::Path;
use adblock::resources::resource_assembler::assemble_web_accessible_resources;
use std::path::Path;

let mut resource_data = assemble_web_accessible_resources(
Path::new("data/test/fake-uBO-files/web_accessible_resources"),
Expand All @@ -116,16 +115,14 @@ fn build_resources_for_filters(#[allow(unused)] filters: &[NetworkFilter]) -> Re
)),
);

resource_data
.into_iter()
.for_each(|resource| {
let _res = resources.add_resource(resource);
});
resource_data.into_iter().for_each(|resource| {
let _res = resources.add_resource(resource);
});
}

#[cfg(not(feature = "resource-assembler"))]
{
use adblock::resources::{Resource, ResourceType, MimeType};
use adblock::resources::{MimeType, Resource, ResourceType};

filters
.iter()
Expand Down Expand Up @@ -213,19 +210,20 @@ pub fn build_custom_requests(rules: Vec<NetworkFilter>) -> Vec<Request> {

let source_url = format!("https://{}", source_hostname);

Request::new(
&url,
&source_url,
raw_type,
).unwrap()
Request::new(&url, &source_url, raw_type).unwrap()
})
.collect::<Vec<_>>()
}

fn bench_fn(blocker: &Blocker, resources: &ResourceStorage, requests: &[Request]) {
requests.iter().for_each(|request| {
let block_result = blocker.check(&request, &resources);
assert!(block_result.redirect.is_some(), "{:?}, {:?}", request, block_result);
assert!(
block_result.redirect.is_some(),
"{:?}, {:?}",
request,
block_result
);
});
}

Expand Down
15 changes: 7 additions & 8 deletions benches/bench_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ use adblock::blocker::{Blocker, BlockerOptions};
mod test_utils;
use test_utils::rules_from_lists;

static DEFAULT_LISTS: Lazy<Vec<String>> = Lazy::new(|| {
rules_from_lists(&[
"data/easylist.to/easylist/easylist.txt",
]).collect()
});
static DEFAULT_LISTS: Lazy<Vec<String>> =
Lazy::new(|| rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]).collect());

fn bench_string_hashing(filters: &Vec<String>) -> adblock::utils::Hash {
let mut dummy: adblock::utils::Hash = 0;
Expand Down Expand Up @@ -81,7 +78,7 @@ fn list_parse(c: &mut Criterion) {
group.finish();
}

fn get_blocker(rules: impl IntoIterator<Item=impl AsRef<str>>) -> Blocker {
fn get_blocker(rules: impl IntoIterator<Item = impl AsRef<str>>) -> Blocker {
let (network_filters, _) = adblock::lists::parse_filters(rules, false, Default::default());

let blocker_options = BlockerOptions {
Expand All @@ -100,10 +97,12 @@ fn blocker_new(c: &mut Criterion) {
let easylist_rules: Vec<_> = rules_from_lists(&[
"data/easylist.to/easylist/easylist.txt",
"data/easylist.to/easylist/easyprivacy.txt",
]).collect();
])
.collect();
let brave_list_rules: Vec<_> = rules_from_lists(&[
"data/brave/brave-main-list.txt",
]).collect();
])
.collect();

group.bench_function("el+ep", move |b| b.iter(|| get_blocker(&easylist_rules)));
group.bench_function("brave-list", move |b| b.iter(|| get_blocker(&brave_list_rules)));
Expand Down
5 changes: 1 addition & 4 deletions examples/deserialization.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use adblock::{
Engine,
request::Request,
};
use adblock::{request::Request, Engine};

use serde::Deserialize;

Expand Down
5 changes: 3 additions & 2 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use adblock::{
Engine,
lists::{FilterSet, ParseOptions},
request::Request,
Engine,
};

fn main() {
Expand All @@ -22,7 +22,8 @@ fn main() {
"http://example.com/-advertisement-icon.",
"http://example.com/helloworld",
"image",
).unwrap();
)
.unwrap();
let blocker_result = engine.check_network_request(&request);

println!("Blocker result: {:?}", blocker_result);
Expand Down
15 changes: 5 additions & 10 deletions examples/generate-dat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use adblock::{
Engine,
request::Request,
};
use adblock::{request::Request, Engine};

use std::fs::File;
use std::io::prelude::*;
Expand All @@ -20,12 +17,10 @@ fn main() {
let request = Request::new(
"https://platform.twitter.com/widgets.js",
"https://fmarier.github.io/brave-testing/social-widgets.html",
"script"
).unwrap();
assert!(engine
.check_network_request(&request)
.exception
.is_some());
"script",
)
.unwrap();
assert!(engine.check_network_request(&request).exception.is_some());
let serialized = engine.serialize_raw().expect("Could not serialize!");

// Write to file
Expand Down
8 changes: 3 additions & 5 deletions examples/use-dat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use adblock::{
Engine,
request::Request,
};
use adblock::{request::Request, Engine};

use std::fs::File;
use std::io::prelude::*;
Expand All @@ -23,7 +20,8 @@ fn main() {
"https://platform.twitter.com/widgets.js",
"https://fmarier.github.io/brave-testing/social-widgets.html",
"script",
).unwrap();
)
.unwrap();
let checked = engine.check_network_request(&request);
assert!(checked.filter.is_some());
assert!(checked.exception.is_some());
Expand Down
Loading
Loading