Skip to content
Snippets Groups Projects
Commit 9be9b007 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

benchmark the recoding process (!44)

this MR
- adds `criterion` as a dependency
- creates a new `benches/recoding.rs` benchmark file
- makes the following `pub`lic
  - `fec::combine`
  - `field` and `field::split_data_into_field_elements`

## example results
| bytes   | shards | k  | mean (us) |
| ------- | ------ | -- | --------- |
| 1       | 2      | 2  | 0.127     |
| 1       | 2      | 4  | 0.179     |
| 1       | 2      | 8  | 0.283     |
| 1       | 2      | 16 | 0.504     |
| 1       | 4      | 2  | 0.346     |
| 1       | 4      | 4  | 0.506     |
| 1       | 4      | 8  | 0.823     |
| 1       | 4      | 16 | 1.451     |
| 1       | 8      | 2  | 0.789     |
| 1       | 8      | 4  | 1.155     |
| 1       | 8      | 8  | 1.89      |
| 1       | 8      | 16 | 3.383     |
| 1       | 16     | 2  | 1.669     |
| 1       | 16     | 4  | 2.478     |
| 1       | 16     | 8  | 4.023     |
| 1       | 16     | 16 | 7.147     |
| 1024    | 2      | 2  | 1.02      |
| 1024    | 2      | 4  | 1.076     |
| 1024    | 2      | 8  | 1.172     |
| 1024    | 2      | 16 | 1.395     |
| 1024    | 4      | 2  | 2.981     |
| 1024    | 4      | 4  | 3.15      |
| 1024    | 4      | 8  | 3.453     |
| 1024    | 4      | 16 | 4.089     |
| 1024    | 8      | 2  | 6.907     |
| 1024    | 8      | 4  | 7.244     |
| 1024    | 8      | 8  | 7.969     |
| 1024    | 8      | 16 | 9.452     |
| 1024    | 16     | 2  | 15.169    |
| 1024    | 16     | 4  | 16.14     |
| 1024    | 16     | 8  | 17.086    |
| 1024    | 16     | 16 | 20.266    |
| 1048576 | 2      | 2  | 1470.966  |
| 1048576 | 2      | 4  | 1097.899  |
| 1048576 | 2      | 8  | 1091.298  |
| 1048576 | 2      | 16 | 1091.544  |
| 1048576 | 4      | 2  | 3274.852  |
| 1048576 | 4      | 4  | 3272.68   |
| 1048576 | 4      | 8  | 3251.877  |
| 1048576 | 4      | 16 | 3272.872  |
| 1048576 | 8      | 2  | 7582.074  |
| 1048576 | 8      | 4  | 7599.012  |
| 1048576 | 8      | 8  | 7584.59   |
| 1048576 | 8      | 16 | 7569.575  |
| 1048576 | 16     | 2  | 16274.986 |
| 1048576 | 16     | 4  | 16303.905 |
| 1048576 | 16     | 8  | 16313.429 |
| 1048576 | 16     | 16 | 16310.305 |
parent 425430d7
No related branches found
No related tags found
No related merge requests found
...@@ -21,3 +21,8 @@ tracing-subscriber = "0.3.17" ...@@ -21,3 +21,8 @@ tracing-subscriber = "0.3.17"
[dev-dependencies] [dev-dependencies]
rand = "0.8.5" rand = "0.8.5"
criterion = "0.3"
[[bench]]
name = "recoding"
harness = false
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use rand::Rng;
use komodo::{
fec::{combine, Shard},
field,
};
use criterion::{criterion_group, criterion_main, Criterion};
fn to_curve<E: Pairing>(n: u128) -> E::ScalarField {
E::ScalarField::from_le_bytes_mod_order(&n.to_le_bytes())
}
fn create_fake_shard<E: Pairing>(nb_bytes: usize, k: usize) -> Shard<E> {
let mut rng = rand::thread_rng();
let bytes: Vec<u8> = (0..nb_bytes).map(|_| rng.gen::<u8>()).collect();
let linear_combination: Vec<E::ScalarField> =
(0..k).map(|_| to_curve::<E>(rng.gen::<u128>())).collect();
Shard {
k: k as u32,
linear_combination,
hash: vec![],
bytes: field::split_data_into_field_elements::<E>(&bytes, 1),
size: 0,
}
}
fn bench_template<E: Pairing>(c: &mut Criterion, nb_bytes: usize, k: usize, nb_shards: usize) {
let shards: Vec<Shard<E>> = (0..nb_shards)
.map(|_| create_fake_shard(nb_bytes, k))
.collect();
let mut rng = rand::thread_rng();
let coeffs: Vec<E::ScalarField> = (0..nb_shards)
.map(|_| to_curve::<E>(rng.gen::<u128>()))
.collect();
c.bench_function(
&format!(
"recoding {} bytes and {} shards with k = {}",
nb_bytes, nb_shards, k
),
|b| b.iter(|| combine(&shards, &coeffs)),
);
}
fn criterion_benchmark(c: &mut Criterion) {
for nb_bytes in [1, 1_024, 1_024 * 1_024] {
for nb_shards in [2, 4, 8, 16] {
for k in [2, 4, 8, 16] {
bench_template::<Bls12_381>(c, nb_bytes, k, nb_shards);
}
}
}
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
...@@ -47,10 +47,7 @@ impl<E: Pairing> Shard<E> { ...@@ -47,10 +47,7 @@ impl<E: Pairing> Shard<E> {
} }
} }
pub(super) fn combine<E: Pairing>( pub fn combine<E: Pairing>(shards: &[Shard<E>], coeffs: &[E::ScalarField]) -> Option<Shard<E>> {
shards: &[Shard<E>],
coeffs: &[E::ScalarField],
) -> Option<Shard<E>> {
if shards.len() != coeffs.len() { if shards.len() != coeffs.len() {
return None; return None;
} }
......
...@@ -6,7 +6,7 @@ use ark_std::One; ...@@ -6,7 +6,7 @@ use ark_std::One;
/// ///
/// [`split_data_into_field_elements`] supports padding the output vector of /// [`split_data_into_field_elements`] supports padding the output vector of
/// elements by giving a number that needs to divide the length of the vector. /// elements by giving a number that needs to divide the length of the vector.
pub(crate) fn split_data_into_field_elements<E: Pairing>( pub fn split_data_into_field_elements<E: Pairing>(
bytes: &[u8], bytes: &[u8],
modulus: usize, modulus: usize,
) -> Vec<E::ScalarField> { ) -> Vec<E::ScalarField> {
......
...@@ -11,7 +11,7 @@ use tracing::{debug, info}; ...@@ -11,7 +11,7 @@ use tracing::{debug, info};
mod error; mod error;
pub mod fec; pub mod fec;
mod field; pub mod field;
pub mod linalg; pub mod linalg;
pub mod setup; pub mod setup;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment