Skip to content

Commit ef99615

Browse files
Make clippy happy
1 parent 6399fec commit ef99615

File tree

7 files changed

+422
-464
lines changed

7 files changed

+422
-464
lines changed

Diff for: circuits/src/benches/benches.rs

-42
This file was deleted.

Diff for: circuits/src/benches/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1-
pub mod benches;
21
pub mod nop;
32
pub mod omni;
43
pub mod poseidon2;
54
pub mod sort;
65
pub mod xor;
6+
7+
use std::time::Duration;
8+
9+
use anyhow::Result;
10+
pub use mozak_cli_args::bench_args::{BenchArgs, BenchFunction};
11+
use nop::NopBench;
12+
use omni::OmniBench;
13+
use poseidon2::Poseidon2Bench;
14+
use sort::{SortBench, SortBenchRecursive};
15+
use xor::XorBench;
16+
17+
pub(crate) trait Bench {
18+
type Args;
19+
type Prepared;
20+
21+
/// method to be executed to prepare the benchmark
22+
fn prepare(&self, args: &Self::Args) -> Self::Prepared;
23+
24+
/// actual benchmark function, whose execution time is
25+
/// to be measured
26+
fn execute(&self, prepared: Self::Prepared) -> Result<()>;
27+
28+
/// benchmark the `execute` function implemented through the
29+
/// trait `Bench`
30+
fn bench(&self, args: &Self::Args) -> Result<Duration> {
31+
let prepared = self.prepare(args);
32+
let start = std::time::Instant::now();
33+
self.execute(prepared)?;
34+
Ok(start.elapsed())
35+
}
36+
}
37+
38+
pub fn bench(args: &BenchArgs) -> Result<Duration> {
39+
match &args.function {
40+
BenchFunction::XorBench { iterations } => XorBench.bench(iterations),
41+
BenchFunction::NopBench { iterations } => NopBench.bench(iterations),
42+
BenchFunction::OmniBench { iterations } => OmniBench.bench(iterations),
43+
BenchFunction::Poseidon2Bench { input_len } => Poseidon2Bench.bench(input_len),
44+
BenchFunction::SortBench { n } => SortBench.bench(n),
45+
BenchFunction::SortBenchRecursive { n } => SortBenchRecursive.bench(n),
46+
}
47+
}

Diff for: circuits/src/benches/nop.rs

+31-40
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,49 @@ use mozak_runner::instruction::{Args, Instruction, Op, NOP};
44
use mozak_runner::vm::ExecutionRecord;
55
use starky::config::StarkConfig;
66

7-
use super::benches::Bench;
7+
use super::Bench;
88
use crate::test_utils::{prove_and_verify_mozak_stark, F};
99

10-
#[allow(clippy::module_name_repetitions)]
11-
pub fn nop_execute((program, record): (Program, ExecutionRecord<F>)) -> Result<(), anyhow::Error> {
12-
prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config())
13-
}
14-
15-
#[allow(clippy::module_name_repetitions)]
16-
#[must_use]
17-
pub fn nop_prepare(iterations: u32) -> (Program, ExecutionRecord<F>) {
18-
let instructions = [
19-
Instruction {
20-
op: Op::ADD,
21-
args: Args {
22-
rd: 1,
23-
rs1: 1,
24-
imm: 1_u32.wrapping_neg(),
25-
..Args::default()
26-
},
27-
},
28-
NOP,
29-
Instruction {
30-
op: Op::BLT,
31-
args: Args {
32-
rs1: 0,
33-
rs2: 1,
34-
imm: 0,
35-
..Args::default()
36-
},
37-
},
38-
];
39-
code::execute(instructions, &[], &[(1, iterations)])
40-
}
41-
4210
pub(crate) struct NopBench;
4311

4412
impl Bench for NopBench {
4513
type Args = u32;
4614
type Prepared = (Program, ExecutionRecord<F>);
4715

48-
fn prepare(&self, args: &Self::Args) -> Self::Prepared { nop_prepare(*args) }
16+
fn prepare(&self, &iterations: &u32) -> Self::Prepared {
17+
let instructions = [
18+
Instruction {
19+
op: Op::ADD,
20+
args: Args {
21+
rd: 1,
22+
rs1: 1,
23+
imm: 1_u32.wrapping_neg(),
24+
..Args::default()
25+
},
26+
},
27+
NOP,
28+
Instruction {
29+
op: Op::BLT,
30+
args: Args {
31+
rs1: 0,
32+
rs2: 1,
33+
imm: 0,
34+
..Args::default()
35+
},
36+
},
37+
];
38+
code::execute(instructions, &[], &[(1, iterations)])
39+
}
4940

50-
fn execute(&self, prepared: Self::Prepared) -> anyhow::Result<()> { nop_execute(prepared) }
41+
fn execute(&self, (program, record): (Program, ExecutionRecord<F>)) -> anyhow::Result<()> {
42+
prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config())
43+
}
5144
}
5245
#[cfg(test)]
5346
mod tests {
54-
use super::{nop_execute, nop_prepare};
47+
use super::NopBench;
48+
use crate::benches::Bench;
5549

5650
#[test]
57-
fn test_nop_bench() -> anyhow::Result<()> {
58-
let iterations = 10;
59-
nop_execute(nop_prepare(iterations))
60-
}
51+
fn test_nop_bench() -> anyhow::Result<()> { NopBench {}.execute(NopBench {}.prepare(&10)) }
6152
}

0 commit comments

Comments
 (0)