Skip to content

Commit 588ddea

Browse files
authored
refactor: improve benchmarking with edge cases and comparison groups (#175)
* refactor: improve benchmarking with edge cases and comparison groups - Added edge cases to benchmark inputs across modules (`sqrt_price_math`, `tick_math`, `swap_math`, `bit_math`) to enhance robustness. - Introduced SDK vs. reference comparison groups using `Criterion::benchmark_group` for better performance analysis. - Optimized benchmarks by leveraging `black_box` to prevent compiler optimizations and added `Throughput` for more informative results. - Reorganized and replaced individual benchmark functions with grouped comparisons for clarity and maintainability. * refactor: clean up benchmarks by optimizing imports and group definitions - Consolidated imports in `sqrt_price_math.rs` for better readability. - Removed redundant trailing commas in `criterion_group!` definitions in both `sqrt_price_math.rs` and `swap_math.rs`.
1 parent d5a4faf commit 588ddea

File tree

4 files changed

+347
-193
lines changed

4 files changed

+347
-193
lines changed

benches/bit_math.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,69 @@
11
use alloy_primitives::{uint, U256};
2-
use criterion::{criterion_group, criterion_main, Criterion};
2+
use core::hint::black_box;
3+
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
34
use uniswap_v3_math::bit_math;
45
use uniswap_v3_sdk::prelude::*;
56

67
const ONE: U256 = uint!(1_U256);
78

8-
fn most_significant_bit_benchmark(c: &mut Criterion) {
9-
c.bench_function("most_significant_bit", |b| {
9+
fn generate_test_values() -> Vec<U256> {
10+
let mut values = (0u8..=255).map(|i| ONE << i).collect::<Vec<_>>();
11+
// Add edge cases
12+
values.extend([ONE, U256::MAX]);
13+
values
14+
}
15+
16+
fn most_significant_bit_comparison(c: &mut Criterion) {
17+
let values = generate_test_values();
18+
let mut group = c.benchmark_group("most_significant_bit");
19+
group.throughput(Throughput::Elements(values.len() as u64));
20+
21+
group.bench_function("sdk", |b| {
1022
b.iter(|| {
11-
for i in 0u8..=255 {
12-
let _ = most_significant_bit(ONE << i);
23+
for value in &values {
24+
let _ = black_box(most_significant_bit(*value));
1325
}
1426
})
1527
});
16-
}
1728

18-
fn most_significant_bit_benchmark_ref(c: &mut Criterion) {
19-
c.bench_function("most_significant_bit_ref", |b| {
29+
group.bench_function("reference", |b| {
2030
b.iter(|| {
21-
for i in 0u8..=255 {
22-
let _ = bit_math::most_significant_bit(ONE << i);
31+
for value in &values {
32+
let _ = black_box(bit_math::most_significant_bit(*value));
2333
}
2434
})
2535
});
36+
37+
group.finish();
2638
}
2739

28-
fn least_significant_bit_benchmark(c: &mut Criterion) {
29-
c.bench_function("least_significant_bit", |b| {
40+
fn least_significant_bit_comparison(c: &mut Criterion) {
41+
let values = generate_test_values();
42+
let mut group = c.benchmark_group("least_significant_bit");
43+
group.throughput(Throughput::Elements(values.len() as u64));
44+
45+
group.bench_function("sdk", |b| {
3046
b.iter(|| {
31-
for i in 0u8..=255 {
32-
let _ = least_significant_bit(ONE << i);
47+
for value in &values {
48+
let _ = black_box(least_significant_bit(*value));
3349
}
34-
});
50+
})
3551
});
36-
}
3752

38-
fn least_significant_bit_benchmark_ref(c: &mut Criterion) {
39-
c.bench_function("least_significant_bit_ref", |b| {
53+
group.bench_function("reference", |b| {
4054
b.iter(|| {
41-
for i in 0u8..=255 {
42-
let _ = bit_math::least_significant_bit(ONE << i);
55+
for value in &values {
56+
let _ = black_box(bit_math::least_significant_bit(*value));
4357
}
44-
});
58+
})
4559
});
60+
61+
group.finish();
4662
}
4763

4864
criterion_group!(
4965
benches,
50-
most_significant_bit_benchmark,
51-
most_significant_bit_benchmark_ref,
52-
least_significant_bit_benchmark,
53-
least_significant_bit_benchmark_ref
66+
most_significant_bit_comparison,
67+
least_significant_bit_comparison
5468
);
5569
criterion_main!(benches);

0 commit comments

Comments
 (0)