Skip to content

Commit

Permalink
Parallelize verification
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed Feb 14, 2018
1 parent 8ff8e90 commit 31d7972
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ name = "loomination"
version = "0.1.0"
authors = ["Greg Fitzgerald <[email protected]>"]

[features]
unstable = []

[dependencies]
rayon = "0.9"
itertools = "0.7.6"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#![cfg_attr(feature = "unstable", feature(test))]
pub mod tick;
extern crate itertools;
extern crate rayon;
37 changes: 36 additions & 1 deletion src/tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,42 @@ impl Tick {
/// 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 {
use rayon::prelude::*;
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))
}

/// 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));
});
}
}

0 comments on commit 31d7972

Please sign in to comment.