Parallelize verification
This commit is contained in:
parent
8ff8e90c5e
commit
31d79727b6
|
@ -0,0 +1,17 @@
|
||||||
|
language: rust
|
||||||
|
matrix:
|
||||||
|
allow_failures:
|
||||||
|
- rust: nightly
|
||||||
|
include:
|
||||||
|
- rust: beta
|
||||||
|
- rust: nightly
|
||||||
|
env:
|
||||||
|
- FEATURES='unstable'
|
||||||
|
cache: cargo
|
||||||
|
before_script:
|
||||||
|
- export PATH="$PATH:$HOME/.cargo/bin"
|
||||||
|
- rustup component add rustfmt-preview
|
||||||
|
script:
|
||||||
|
- cargo fmt -- --write-mode=diff
|
||||||
|
- cargo build --verbose --features "$FEATURES"
|
||||||
|
- cargo test --verbose --features "$FEATURES"
|
|
@ -3,4 +3,9 @@ name = "loomination"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Greg Fitzgerald <garious@gmail.com>"]
|
authors = ["Greg Fitzgerald <garious@gmail.com>"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
unstable = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rayon = "0.9"
|
||||||
|
itertools = "0.7.6"
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
|
#![cfg_attr(feature = "unstable", feature(test))]
|
||||||
pub mod tick;
|
pub mod tick;
|
||||||
|
extern crate itertools;
|
||||||
|
extern crate rayon;
|
||||||
|
|
37
src/tick.rs
37
src/tick.rs
|
@ -49,7 +49,42 @@ impl Tick {
|
||||||
/// assert!(!verify_slice(&vec![Tick::new(0, 0), Tick::new(1, 0)], 0)); // lazy inductive case, bad
|
/// assert!(!verify_slice(&vec![Tick::new(0, 0), Tick::new(1, 0)], 0)); // lazy inductive case, bad
|
||||||
/// ```
|
/// ```
|
||||||
pub fn verify_slice(ticks: &[Tick], seed: u64) -> bool {
|
pub fn verify_slice(ticks: &[Tick], seed: u64) -> bool {
|
||||||
|
use rayon::prelude::*;
|
||||||
let genesis = [Tick { hash: seed, n: 0 }];
|
let genesis = [Tick { hash: seed, n: 0 }];
|
||||||
let mut tick_pairs = genesis.iter().chain(ticks).zip(ticks);
|
let tick_pairs = genesis.par_iter().chain(ticks).zip(ticks);
|
||||||
tick_pairs.all(|(x, x1)| x1.verify(x.hash))
|
tick_pairs.all(|(x, x1)| x1.verify(x.hash))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Verifies the hashes and ticks serially. Exists only for reference.
|
||||||
|
pub fn verify_slice_seq(ticks: &[Tick], seed: u64) -> bool {
|
||||||
|
let genesis = [Tick { hash: seed, n: 0 }];
|
||||||
|
let tick_pairs = genesis.iter().chain(ticks).zip(ticks);
|
||||||
|
tick_pairs.into_iter().all(|(x, x1)| x1.verify(x.hash))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a vector of Ticks of length 'len' from 'seed' hash and 'hashes_per_tick'.
|
||||||
|
pub fn create_ticks(seed: u64, hashes_per_tick: u64, len: usize) -> Vec<Tick> {
|
||||||
|
use itertools::unfold;
|
||||||
|
let mut ticks_iter = unfold(seed, |state| {
|
||||||
|
let tick = Tick::new(*state, hashes_per_tick);
|
||||||
|
*state = tick.hash;
|
||||||
|
return Some(tick);
|
||||||
|
});
|
||||||
|
ticks_iter.by_ref().take(len).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "unstable", test))]
|
||||||
|
mod bench {
|
||||||
|
extern crate test;
|
||||||
|
use self::test::Bencher;
|
||||||
|
use tick;
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn tick_bench(bencher: &mut Bencher) {
|
||||||
|
let seed = 0;
|
||||||
|
let ticks = tick::create_ticks(seed, 100_000, 4);
|
||||||
|
bencher.iter(|| {
|
||||||
|
assert!(tick::verify_slice(&ticks, seed));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue