atc-router icon indicating copy to clipboard operation
atc-router copied to clipboard

perf(lib): add `#[inline]` for small functions

Open fjchen7 opened this issue 1 year ago • 1 comments

#[inline] on function can tell compiler to automatically expand the function at the places where it's used. The cost is that the the compiled binary file will become larger, and the compilation time might increase. But in general experience the trade-off are usually worth it for small function.

Below is the benchmark result of a hot spot function Predicate::Execute method, with efficiency improved by nearly 6.7%, in macOS on Apple M1 Pro (10-core, 32GB RAM).

time:   [6.4486 ns 6.4725 ns 6.5054 ns]
change: [-12.594% -6.7029% -2.1903%] (p = 0.01 < 0.05)
Performance has improved.

The benchmark source code

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

    let p = Predicate {
        lhs: ast::Lhs {
            var_name: "my_key".to_string(),
            transformations: vec![],
        },
        rhs: Value::String("foo".to_string()),
        op: BinaryOperator::Prefix,
    };

    c.bench_function("Predicate::execute", |b| {
        b.iter(|| {
            let mut ctx = Context::new(&schema);
            let mut mat = Match::new();
            p.execute(&mut ctx, &mut mat)
        })
    });
}

fjchen7 avatar Aug 04 '24 08:08 fjchen7

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 04 '24 08:08 CLAassistant