|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 4 | +use ergo_chain_types::ec_point::{exponentiate, generator}; |
| 5 | +use ergotree_interpreter::eval::test_util::eval_out_wo_ctx; |
| 6 | +use ergotree_ir::{ |
| 7 | + bigint256::BigInt256, |
| 8 | + ergo_tree::{ErgoTree, ErgoTreeHeader}, |
| 9 | + mir::{ |
| 10 | + bin_op::{BinOp, BinOpKind, RelationOp}, |
| 11 | + calc_blake2b256::CalcBlake2b256, |
| 12 | + calc_sha256::CalcSha256, |
| 13 | + coll_append::Append, |
| 14 | + coll_filter::Filter, |
| 15 | + constant::Constant, |
| 16 | + exponentiate::Exponentiate, |
| 17 | + expr::Expr, |
| 18 | + func_value::{FuncArg, FuncValue}, |
| 19 | + method_call::MethodCall, |
| 20 | + sigma_and::SigmaAnd, |
| 21 | + subst_const::SubstConstants, |
| 22 | + val_use::ValUse, |
| 23 | + value::Value, |
| 24 | + }, |
| 25 | + serialization::SigmaSerializable, |
| 26 | + sigma_protocol::sigma_boolean::{SigmaBoolean, SigmaProp}, |
| 27 | + types::{scoll::FLATMAP_METHOD, stype::SType, stype_param::STypeVar}, |
| 28 | +}; |
| 29 | + |
| 30 | +fn bench_blake2b256(c: &mut Criterion) { |
| 31 | + let mut group = c.benchmark_group("blake2b256"); |
| 32 | + for size in (1..1024).step_by(200) { |
| 33 | + let arr = vec![0u8; size]; |
| 34 | + let expr: Expr = CalcBlake2b256 { |
| 35 | + input: Box::new(Constant::from(arr).into()), |
| 36 | + } |
| 37 | + .into(); |
| 38 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:4}")), |b| { |
| 39 | + b.iter(|| eval_out_wo_ctx::<Vec<i8>>(&expr)) |
| 40 | + }); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn bench_sha256(c: &mut Criterion) { |
| 45 | + let mut group = c.benchmark_group("sha256"); |
| 46 | + for size in (1..1024).step_by(200) { |
| 47 | + let arr = vec![0u8; size]; |
| 48 | + let expr: Expr = CalcSha256 { |
| 49 | + input: Box::new(Constant::from(arr).into()), |
| 50 | + } |
| 51 | + .into(); |
| 52 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:4}")), |b| { |
| 53 | + b.iter(|| eval_out_wo_ctx::<Vec<i8>>(&expr)) |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn bench_append(c: &mut Criterion) { |
| 59 | + let mut group = c.benchmark_group("Coll.append"); |
| 60 | + for size in (1..1024).step_by(200) { |
| 61 | + let arr = vec![0u8; size]; |
| 62 | + let expr: Expr = Append::new( |
| 63 | + Constant::from(arr.clone()).into(), |
| 64 | + Constant::from(arr).into(), |
| 65 | + ) |
| 66 | + .unwrap() |
| 67 | + .into(); |
| 68 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:4}")), |b| { |
| 69 | + b.iter(|| eval_out_wo_ctx::<Value<'_>>(&expr)); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn bench_substitute_constants(c: &mut Criterion) { |
| 75 | + let mut group = c.benchmark_group("substituteConstants"); |
| 76 | + for size in (1..=101).step_by(10) { |
| 77 | + let tree_expr: Expr = |
| 78 | + SigmaAnd::new(vec![Constant::from(SigmaBoolean::from(true)).into(); size]) |
| 79 | + .unwrap() |
| 80 | + .into(); |
| 81 | + let tree_bytes = ErgoTree::new(ErgoTreeHeader::v1(true), &tree_expr) |
| 82 | + .unwrap() |
| 83 | + .sigma_serialize_bytes() |
| 84 | + .unwrap(); |
| 85 | + let subst_expr = SubstConstants::new( |
| 86 | + tree_bytes.into(), |
| 87 | + Constant::from((0..size as i32).collect::<Vec<_>>()).into(), |
| 88 | + Constant::from(vec![SigmaProp::new(false.into()); size]).into(), |
| 89 | + ) |
| 90 | + .unwrap() |
| 91 | + .into(); |
| 92 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:3}")), |b| { |
| 93 | + b.iter(|| eval_out_wo_ctx::<Value<'_>>(&subst_expr)) |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn bench_exponentiate(c: &mut Criterion) { |
| 99 | + c.bench_function("exponentiate(generator, a)", |b| { |
| 100 | + let a = BigInt256::from_str("1000").unwrap(); |
| 101 | + let expr: Expr = Exponentiate::new(generator().into(), a.into()) |
| 102 | + .unwrap() |
| 103 | + .into(); |
| 104 | + b.iter(|| eval_out_wo_ctx::<Value<'_>>(&expr)) |
| 105 | + }); |
| 106 | + c.bench_function("exponentiate(point, a)", |b| { |
| 107 | + let a = BigInt256::from_str("1000").unwrap(); |
| 108 | + let expr: Expr = |
| 109 | + Exponentiate::new((exponentiate(&generator(), &2u32.into())).into(), a.into()) |
| 110 | + .unwrap() |
| 111 | + .into(); |
| 112 | + b.iter(|| eval_out_wo_ctx::<Value<'_>>(&expr)) |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +fn bench_filter(c: &mut Criterion) { |
| 117 | + let mut group = c.benchmark_group("filter"); |
| 118 | + // v % 2 == 0 |
| 119 | + let filter_lambda = BinOp { |
| 120 | + kind: BinOpKind::Relation(RelationOp::Eq), |
| 121 | + left: Box::new( |
| 122 | + BinOp { |
| 123 | + kind: BinOpKind::Arith(ergotree_ir::mir::bin_op::ArithOp::Modulo), |
| 124 | + left: Box::new( |
| 125 | + ValUse { |
| 126 | + val_id: 1.into(), |
| 127 | + tpe: SType::SInt, |
| 128 | + } |
| 129 | + .into(), |
| 130 | + ), |
| 131 | + right: Box::new(Constant::from(2i32).into()), |
| 132 | + } |
| 133 | + .into(), |
| 134 | + ), |
| 135 | + right: Box::new(Constant::from(0i32).into()), |
| 136 | + }; |
| 137 | + for size in (1..=1001i32).step_by(100) { |
| 138 | + let arr: Expr = Constant::from((0..size).collect::<Vec<_>>()).into(); |
| 139 | + let expr: Expr = Filter::new( |
| 140 | + arr, |
| 141 | + FuncValue::new( |
| 142 | + vec![FuncArg { |
| 143 | + idx: 1.into(), |
| 144 | + tpe: SType::SInt, |
| 145 | + }], |
| 146 | + filter_lambda.clone().into(), |
| 147 | + ) |
| 148 | + .into(), |
| 149 | + ) |
| 150 | + .unwrap() |
| 151 | + .into(); |
| 152 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:4}")), |b| { |
| 153 | + b.iter(|| eval_out_wo_ctx::<Value<'_>>(&expr)) |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +fn bench_flatmap(c: &mut Criterion) { |
| 159 | + let mut group = c.benchmark_group("flatMap"); |
| 160 | + // flatten Coll[Coll[Byte]] into Coll[Byte] |
| 161 | + let flatmap_lambda: Expr = ValUse { |
| 162 | + val_id: 1.into(), |
| 163 | + tpe: SType::SColl(SType::SByte.into()), |
| 164 | + } |
| 165 | + .into(); |
| 166 | + for size in (1..=1001).step_by(100) { |
| 167 | + let obj: Expr = Constant::from(vec![vec![0u8; 100]; size]).into(); |
| 168 | + let mc: Expr = MethodCall::new( |
| 169 | + obj, |
| 170 | + FLATMAP_METHOD.clone().with_concrete_types( |
| 171 | + &[ |
| 172 | + (STypeVar::iv(), SType::SColl(SType::SByte.into())), |
| 173 | + (STypeVar::ov(), SType::SByte), |
| 174 | + ] |
| 175 | + .into_iter() |
| 176 | + .collect(), |
| 177 | + ), |
| 178 | + vec![FuncValue::new( |
| 179 | + vec![FuncArg { |
| 180 | + idx: 1.into(), |
| 181 | + tpe: SType::SColl(SType::SByte.into()), |
| 182 | + }], |
| 183 | + flatmap_lambda.clone(), |
| 184 | + ) |
| 185 | + .into()], |
| 186 | + ) |
| 187 | + .unwrap() |
| 188 | + .into(); |
| 189 | + group.bench_function(BenchmarkId::from_parameter(format!("{size:4}")), |b| { |
| 190 | + b.iter(|| assert_eq!(eval_out_wo_ctx::<Vec<i8>>(&mc).len(), 100 * size)) |
| 191 | + }); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +criterion_group!( |
| 196 | + eval_benches, |
| 197 | + bench_blake2b256, |
| 198 | + bench_sha256, |
| 199 | + bench_append, |
| 200 | + bench_substitute_constants, |
| 201 | + bench_exponentiate, |
| 202 | + bench_filter, |
| 203 | + bench_flatmap |
| 204 | +); |
| 205 | + |
| 206 | +criterion_main!(eval_benches); |
0 commit comments