|
1 | 1 | use cairo_vm::{ |
| 2 | + felt::Felt252, |
2 | 3 | types::program::Program, |
3 | 4 | vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, |
4 | 5 | }; |
5 | | -use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; |
| 6 | +use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; |
| 7 | +use num_traits::Zero; |
6 | 8 |
|
7 | 9 | #[cfg(feature = "with_mimalloc")] |
8 | 10 | use mimalloc::MiMalloc; |
@@ -58,5 +60,39 @@ fn load_program_data(c: &mut Criterion) { |
58 | 60 | }); |
59 | 61 | } |
60 | 62 |
|
| 63 | +fn add_u64_with_felt252(c: &mut Criterion) { |
| 64 | + // There are 9 possible cases: |
| 65 | + // - The felt is `0` |
| 66 | + // - The felt is `-1` and the result in range |
| 67 | + // - The felt is `-1` but the result is out of range (the u64 is `0`) |
| 68 | + // - The felt is positive and the result in range |
| 69 | + // - The felt is positive and the result is out of range |
| 70 | + // - The felt is `> u64::MAX` which always causes to be out of range |
| 71 | + // - The felt is `<= -2` and the result is in range |
| 72 | + // - The felt is `<= -2` and the result is out of range |
| 73 | + // - The felt is `< -u64::MAX` which always causes to be out of range |
| 74 | + // I consider all of these cases because branching is the most likely |
| 75 | + // bottleneck here. |
| 76 | + let cases = [ |
| 77 | + (1u64, Felt252::zero()), |
| 78 | + (1u64, Felt252::from(-1i128)), |
| 79 | + (0u64, Felt252::from(-1i128)), |
| 80 | + (0u64, Felt252::from(1i128)), |
| 81 | + (1u64, Felt252::from(u64::MAX as i128)), |
| 82 | + (0u64, Felt252::from(u64::MAX as i128 + 1i128)), |
| 83 | + (2u64, Felt252::from(-2i128)), |
| 84 | + (1u64, Felt252::from(-2i128)), |
| 85 | + (0u64, Felt252::from(-(u64::MAX as i128) - 1i128)), |
| 86 | + ]; |
| 87 | + let mut group = c.benchmark_group("add_u64_with_felt"); |
| 88 | + for (i, case) in cases.iter().enumerate() { |
| 89 | + group.bench_with_input(BenchmarkId::from_parameter(i), case, |b, (lhs, rhs)| { |
| 90 | + b.iter(|| *lhs + rhs); |
| 91 | + }); |
| 92 | + } |
| 93 | + group.finish(); |
| 94 | +} |
| 95 | + |
| 96 | +criterion_group!(felt, add_u64_with_felt252); |
61 | 97 | criterion_group!(runner, build_many_runners, load_program_data, parse_program); |
62 | | -criterion_main!(runner); |
| 98 | +criterion_main!(felt, runner); |
0 commit comments