Skip to content

Commit

Permalink
Reapply "perf(interpreter): improve ATC interprater's cache efficiency (
Browse files Browse the repository at this point in the history
#246)"

This reverts commit 430eb9e.
  • Loading branch information
Oyami-Srk committed Dec 30, 2024
1 parent 4d29e10 commit 0722ea5
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 75 deletions.
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ crate-type = ["lib", "cdylib", "staticlib"]
default = ["ffi"]
ffi = ["dep:bitflags"]
serde = ["cidr/serde", "dep:serde", "dep:serde_regex"]

[[bench]]
name = "test"
harness = false

[[bench]]
name = "string"
harness = false

[[bench]]
name = "match_mix"
harness = false

[[bench]]
name = "not_match_mix"
harness = false

[[bench]]
name = "build"
harness = false
41 changes: 41 additions & 0 deletions benches/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use atc_router::ast::{Type, Value};
use atc_router::context::Context;
use atc_router::router::Router;
use atc_router::schema::Schema;
use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

// To run this benchmark, execute the following command:
// ```shell
// cargo bench --bench build
// ```

const N: usize = 1000;

fn make_uuid(a: usize) -> String {
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a)
}

fn criterion_benchmark(c: &mut Criterion) {
let mut schema = Schema::default();
schema.add_field("a", Type::Int);

let mut context = Context::new(&schema);
context.add_value("a", Value::Int(N as i64));

c.bench_function("Build Router", |b| {
b.iter_with_large_drop(|| {
let mut router = Router::new(&schema);
for i in 0..N {
let expr = format!("((a > 0 || a < {}) && a != 0) && a == 1", N + 1);
let variant = make_uuid(i);
let uuid = Uuid::try_from(variant.as_str()).unwrap();
router.add_matcher(N - i, uuid, &expr).unwrap();
}
router
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
57 changes: 57 additions & 0 deletions benches/match_mix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use atc_router::ast::Value;
use atc_router::context::Context;
use atc_router::router::Router;
use atc_router::schema::Schema;
use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

// To run this benchmark, execute the following command:
// ```shell
// cargo bench --bench match_mix
// ```

const N: usize = 100000;

fn make_uuid(a: usize) -> String {
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a)
}

fn criterion_benchmark(c: &mut Criterion) {
let mut schema = Schema::default();
schema.add_field("http.path", atc_router::ast::Type::String);
schema.add_field("http.version", atc_router::ast::Type::String);
schema.add_field("a", atc_router::ast::Type::Int);

let mut router = Router::new(&schema);

for i in 0..N {
let expr = format!(
r#"(http.path == "hello{}" && http.version == "1.1") || !(( a == 2) && ( a == 9 )) || !(a == 1) || ( a == 3 && a == 4) && !(a == 5)"#,
i
);
let variant = make_uuid(i);
let uuid = Uuid::try_from(variant.as_str()).unwrap();
router.add_matcher(N - i, uuid, &expr).unwrap();
}

let mut ctx_match = Context::new(&schema);
ctx_match.add_value(
"http.path",
atc_router::ast::Value::String("hello49999".to_string()),
);
ctx_match.add_value(
"http.version",
atc_router::ast::Value::String("1.1".to_string()),
);
ctx_match.add_value("a", Value::Int(3 as i64));

c.bench_function("Match", |b| {
b.iter(|| {
let is_match = router.execute(&mut ctx_match);
assert!(is_match);
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
57 changes: 57 additions & 0 deletions benches/not_match_mix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use atc_router::ast::{Type, Value};
use atc_router::context::Context;
use atc_router::router::Router;
use atc_router::schema::Schema;
use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

// To run this benchmark, execute the following command:
// ```shell
// cargo bench --bench not_match_mix
// ```

const N: usize = 100000;

fn make_uuid(a: usize) -> String {
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a)
}

fn criterion_benchmark(c: &mut Criterion) {
let mut schema = Schema::default();
schema.add_field("http.path", atc_router::ast::Type::String);
schema.add_field("http.version", atc_router::ast::Type::String);
schema.add_field("a", atc_router::ast::Type::Int);

let mut router = Router::new(&schema);

for i in 0..N {
let expr = format!(
r#"(http.path == "hello{}" && http.version == "1.1") || !(( a == 2) && ( a == 9 )) || !(a == 1) || ( a == 3 && a == 4) && !(a == 5)"#,
i
);
let variant = make_uuid(i);
let uuid = Uuid::try_from(variant.as_str()).unwrap();
router.add_matcher(N - i, uuid, &expr).unwrap();
}

let mut ctx_match = Context::new(&schema);
ctx_match.add_value(
"http.path",
atc_router::ast::Value::String("hello49999".to_string()),
);
ctx_match.add_value(
"http.version",
atc_router::ast::Value::String("1.1".to_string()),
);
ctx_match.add_value("a", Value::Int(5 as i64));

c.bench_function("Doesn't Match", |b| {
b.iter(|| {
let is_match = router.execute(&mut ctx_match);
assert!(!is_match);
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
53 changes: 53 additions & 0 deletions benches/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use atc_router::ast::{Type, Value};
use atc_router::context::Context;
use atc_router::router::Router;
use atc_router::schema::Schema;
use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

// To run this benchmark, execute the following command:
// ```shell
// cargo bench --bench string
// ```

const N_MATCHER: usize = 1;
const N_EXECUTE: usize = 100000;

fn make_uuid(a: usize) -> String {
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a)
}

fn criterion_benchmark(c: &mut Criterion) {
let mut schema = Schema::default();
schema.add_field("http.path.segments.*", Type::String);
schema.add_field("http.path.segments.len", Type::Int);

let mut router = Router::new(&schema);

for i in 0..N_MATCHER {
let expr = format!(
r#"http.path.segments.0_1 == "test/run" && http.path.segments.3 == "address{}" && http.path.segments.len == 3"#,
i
);
let variant = make_uuid(i);
let uuid = Uuid::try_from(variant.as_str()).unwrap();
router.add_matcher(N_MATCHER - i, uuid, &expr).unwrap();
}

let mut context = Context::new(&schema);
context.add_value("http.path.segments.0_1", "test/run".to_string().into());
context.add_value("http.path.segments.3", "bar".to_string().into());
context.add_value("http.path.segments.len", Value::Int(3 as i64));

c.bench_function("Doesn't Match", |b| {
b.iter(|| {
for _i in 0..N_EXECUTE {
let is_match = router.execute(&mut context);
assert!(!is_match);
}
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
46 changes: 46 additions & 0 deletions benches/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use atc_router::ast::{Type, Value};
use atc_router::context::Context;
use atc_router::router::Router;
use atc_router::schema::Schema;
use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

// To run this benchmark, execute the following command:
// ```shell
// cargo bench --bench test
// ```

const N: usize = 100000;

fn make_uuid(a: usize) -> String {
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a)
}

fn criterion_benchmark(c: &mut Criterion) {
let mut schema = Schema::default();
schema.add_field("a", Type::Int);

let mut router = Router::new(&schema);

for i in 0..N {
let expr = format!("((a > 0 || a < {}) && a != 0) && a == 1", N + 1);
let variant = make_uuid(i);
let uuid = Uuid::try_from(variant.as_str()).unwrap();
router.add_matcher(N - i, uuid, &expr).unwrap();
}

let mut context = Context::new(&schema);
context.add_value("a", Value::Int(N as i64));

c.bench_function("Doesn't Match", |b| {
b.iter(|| {
for _ in 0..10 {
let is_match = router.execute(&mut context);
assert!(!is_match);
}
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
8 changes: 4 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub enum LogicalExpression {
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LhsTransformations {
Lower,
Any,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOperator {
Equals, // ==
NotEquals, // !=
Expand Down Expand Up @@ -101,7 +101,7 @@ pub enum Type {
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Lhs {
pub var_name: String,
pub transformations: Vec<LhsTransformations>,
Expand All @@ -126,7 +126,7 @@ impl Lhs {
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Predicate {
pub lhs: Lhs,
pub rhs: Value,
Expand Down
Loading

0 comments on commit 0722ea5

Please sign in to comment.