generated from NethermindEth/rust-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Benchmarks for operations on *_monty_field types and improved division
#11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![allow(non_local_definitions)] | ||
| #![allow(clippy::eq_op)] | ||
|
|
||
| mod field_ops_bench_common; | ||
|
|
||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
| use crypto_bigint::{Odd, modular::BoxedMontyParams}; | ||
| use crypto_primitives::crypto_bigint_boxed_monty::BoxedMontyField; | ||
|
|
||
| use crate::field_ops_bench_common::field_benchmarks; | ||
|
|
||
| fn bench_config() -> BoxedMontyParams { | ||
| let modulus = crypto_bigint::BoxedUint::from_be_hex( | ||
| "0000000000000000000000000000000000860995AE68FC80E1B1BD1E39D54B33", | ||
| 256, | ||
| ) | ||
| .unwrap(); | ||
| let modulus = Odd::new(modulus).expect("modulus should be odd"); | ||
| BoxedMontyParams::new(modulus) | ||
| } | ||
|
|
||
| pub fn boxed_monty_benches(c: &mut Criterion) { | ||
| field_benchmarks::<BoxedMontyField>(c, "Boxed Monty Field Arithmetic", &bench_config()); | ||
| } | ||
|
|
||
| criterion_group!(benches, boxed_monty_benches); | ||
| criterion_main!(benches); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #![allow(non_local_definitions)] | ||
| #![allow(clippy::eq_op)] | ||
|
|
||
| mod field_ops_bench_common; | ||
|
|
||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
| use crypto_bigint::{U256, const_monty_params}; | ||
| use crypto_primitives::crypto_bigint_const_monty::ConstMontyField; | ||
|
|
||
| use crate::field_ops_bench_common::field_benchmarks; | ||
|
|
||
| const_monty_params!( | ||
| Params, | ||
| U256, | ||
| "0000000000000000000000000000000000860995AE68FC80E1B1BD1E39D54B33" | ||
| ); | ||
|
|
||
| pub fn const_monty_benches(c: &mut Criterion) { | ||
| field_benchmarks::<ConstMontyField<Params, _>>(c, "Const Monty Field Arithmetic", &()); | ||
| } | ||
|
|
||
| criterion_group!(benches, const_monty_benches); | ||
ElijahVlasov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| criterion_main!(benches); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| #![allow(non_local_definitions)] | ||
| #![allow(clippy::eq_op)] | ||
|
|
||
| use std::{ | ||
| hint::black_box, | ||
| ops::{Add, Div, Mul}, | ||
| }; | ||
|
|
||
| use criterion::{AxisScale, BatchSize, BenchmarkId, Criterion, PlotConfiguration}; | ||
| use crypto_primitives::FromPrimitiveWithConfig; | ||
|
|
||
| fn bench_random_field<F: FromPrimitiveWithConfig>( | ||
| group: &mut criterion::BenchmarkGroup<criterion::measurement::WallTime>, | ||
| num: u64, | ||
| config: &F::Config, | ||
| ) where | ||
| for<'a> &'a F: Add<&'a F>, | ||
| for<'a> &'a F: Mul<&'a F>, | ||
| for<'a> &'a F: Div<&'a F>, | ||
| { | ||
| let field_elem = F::from_with_cfg(num, config); | ||
| let param = format!("Param = {}", num); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Mul owned by owned", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs * rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Mul owned by ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs * &rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Mul ref by ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter(|| { | ||
| for _ in 0..10000 { | ||
| let _ = black_box(unop_elem * unop_elem); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Add owned to owned", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs + rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Add owned to ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs + &rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Add ref to ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter(|| { | ||
| for _ in 0..10000 { | ||
| let _ = black_box(unop_elem + unop_elem); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Div owned by owned", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs / rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Div owned by ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || { | ||
| ( | ||
| vec![unop_elem.clone(); 10000], | ||
| vec![unop_elem.clone(); 10000], | ||
| ) | ||
| }, | ||
| |(lhs, rhs)| { | ||
| for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()) { | ||
| let _ = black_box(lhs / &rhs); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Div ref by ref", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter(|| { | ||
| for _ in 0..10000 { | ||
| let _ = black_box(unop_elem / unop_elem); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| group.bench_with_input( | ||
| BenchmarkId::new("Negation", ¶m), | ||
| &field_elem, | ||
| |b, unop_elem| { | ||
| b.iter_batched( | ||
| || vec![unop_elem.clone(); 10000], | ||
| |unop_elem| { | ||
| for x in unop_elem.into_iter() { | ||
| let _ = black_box(-x); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| let v = vec![field_elem; 10]; | ||
|
|
||
| group.bench_with_input(BenchmarkId::new("Sum", ¶m), &v, |b, v| { | ||
| b.iter(|| { | ||
| for _ in 0..10000 { | ||
| let _ = black_box(F::sum(v.iter())); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| group.bench_with_input(BenchmarkId::new("Product", ¶m), &v, |b, v| { | ||
| b.iter(|| { | ||
| for _ in 0..10000 { | ||
| let _ = black_box(F::product(v.iter())); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| pub fn field_benchmarks<F: FromPrimitiveWithConfig>( | ||
| c: &mut Criterion, | ||
| name: &str, | ||
| config: &F::Config, | ||
| ) where | ||
| for<'a> &'a F: Add<&'a F>, | ||
| for<'a> &'a F: Mul<&'a F>, | ||
| for<'a> &'a F: Div<&'a F>, | ||
| { | ||
| let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); | ||
|
|
||
| let mut group = c.benchmark_group(name); | ||
| group.plot_config(plot_config); | ||
|
|
||
| bench_random_field::<F>(&mut group, 695962179703_u64, config); | ||
| bench_random_field::<F>(&mut group, 2345695962179703_u64, config); | ||
| bench_random_field::<F>(&mut group, 111111111111111111_u64, config); | ||
| bench_random_field::<F>(&mut group, 12345678124578658568_u64, config); | ||
| group.finish(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![allow(non_local_definitions)] | ||
| #![allow(clippy::eq_op)] | ||
|
|
||
| mod field_ops_bench_common; | ||
|
|
||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
| use crypto_bigint::{Odd, modular::MontyParams}; | ||
| use crypto_primitives::crypto_bigint_monty::MontyField; | ||
|
|
||
| use crate::field_ops_bench_common::field_benchmarks; | ||
|
|
||
| const LIMBS: usize = 4; | ||
|
|
||
| fn bench_config() -> MontyParams<LIMBS> { | ||
| let modulus = crypto_bigint::Uint::<LIMBS>::from_be_hex( | ||
| "0000000000000000000000000000000000860995AE68FC80E1B1BD1E39D54B33", | ||
| ); | ||
| let modulus = Odd::new(modulus).expect("modulus should be odd"); | ||
| MontyParams::new(modulus) | ||
| } | ||
|
|
||
| fn monty_field_benches(c: &mut Criterion) { | ||
| field_benchmarks::<MontyField<_>>(c, "Monty Field Arithmetic", &bench_config()); | ||
| } | ||
|
|
||
| criterion_group!(benches, monty_field_benches); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.