-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
online-phase: algebra: scalar: Optimize (de)serialization
This has large effects throughout any stack built on top of the `Scalar` type, so we use the `ark_serialize` `CanonicalSerialize` and `CanonicalDeserialize` traits.
- Loading branch information
Showing
3 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,61 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use ark_mpc::{algebra::Scalar, test_helpers::TestCurve}; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}; | ||
use rand::thread_rng; | ||
|
||
/// Benchmark the serialization of scalars | ||
fn bench_scalar_serialization(c: &mut Criterion) { | ||
let mut rng = thread_rng(); | ||
let mut group = c.benchmark_group("scalar_serialization"); | ||
group.throughput(Throughput::Elements(1)); | ||
|
||
group.bench_function("scalar_serialization", |b| { | ||
b.iter_custom(|n_iters| { | ||
let mut total_time = Duration::from_secs(0); | ||
for _ in 0..n_iters { | ||
let scalar = Scalar::<TestCurve>::random(&mut rng); | ||
|
||
let start = Instant::now(); | ||
let bytes = serde_json::to_value(scalar).unwrap(); | ||
total_time += start.elapsed(); | ||
|
||
black_box(bytes); | ||
} | ||
total_time | ||
}) | ||
}); | ||
} | ||
|
||
/// Benchmark the deserialization of scalars | ||
fn bench_scalar_deserialization(c: &mut Criterion) { | ||
let mut rng = thread_rng(); | ||
let mut group = c.benchmark_group("scalar_deserialization"); | ||
group.throughput(Throughput::Elements(1)); | ||
|
||
group.bench_function("scalar_deserialization", |b| { | ||
b.iter_custom(|n_iters| { | ||
let mut total_time = Duration::from_secs(0); | ||
for _ in 0..n_iters { | ||
let scalar = Scalar::<TestCurve>::random(&mut rng); | ||
let serialized = serde_json::to_value(scalar).unwrap(); | ||
|
||
// Time deserialization only | ||
let start = Instant::now(); | ||
let deserialized: Scalar<TestCurve> = serde_json::from_value(serialized).unwrap(); | ||
total_time += start.elapsed(); | ||
|
||
black_box(deserialized); | ||
} | ||
|
||
total_time | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group! { | ||
name = scalar_ops; | ||
config = Criterion::default(); | ||
targets = bench_scalar_serialization, bench_scalar_deserialization | ||
} | ||
criterion_main!(scalar_ops); |
This file contains 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