Skip to content
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

[plain_hasher] Migrate to 2018 edition #213

Merged
merged 6 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ script:
fi
- cd fixed-hash/ && cargo test --all-features && cd ..
- cd uint/ && cargo test --features=std,quickcheck --release && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cargo check --benches && cd ..
- cd parity-bytes/ && cargo test --no-default-features && cd ..
- cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd ..
- cd parity-util-mem/ && cargo test --features=jemalloc-global && cd ..
Expand Down
12 changes: 10 additions & 2 deletions plain_hasher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
keywords = ["hash", "hasher"]
homepage = "https://github.com/paritytech/parity-common"
categories = [ "no-std"]
categories = ["no-std"]
edition = "2018"

[dependencies]
crunchy = { version = "0.2.1", default-features = false }
crunchy = { version = "0.2", default-features = false }

[dev-dependencies]
criterion = "0.3"

[features]
default = ["std"]
std = ["crunchy/std"]

[[bench]]
name = "bench"
harness = false
42 changes: 17 additions & 25 deletions plain_hasher/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,28 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

#![feature(test)]

extern crate test;
extern crate plain_hasher;

use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;
use test::{Bencher, black_box};
use std::hash::Hasher;

use criterion::{criterion_group, criterion_main, Criterion};
use plain_hasher::PlainHasher;

#[bench]
fn write_plain_hasher(b: &mut Bencher) {
b.iter(|| {
let n: u8 = black_box(100);
(0..n).fold(PlainHasher::default(), |mut old, new| {
let bb = black_box([new; 32]);
old.write(&bb as &[u8]);
fn bench_write_hasher(c: &mut Criterion) {
c.bench_function("write_plain_hasher", |b| b.iter(|| {
(0..100u8).fold(PlainHasher::default(), |mut old, new| {
let bb = [new; 32];
old.write(&bb);
old
});
});
}

#[bench]
fn write_default_hasher(b: &mut Bencher) {
b.iter(|| {
let n: u8 = black_box(100);
(0..n).fold(DefaultHasher::default(), |mut old, new| {
let bb = black_box([new; 32]);
old.write(&bb as &[u8]);
}));
c.bench_function("write_default_hasher", |b| b.iter(|| {
(0..100u8).fold(DefaultHasher::default(), |mut old, new| {
let bb = [new; 32];
old.write(&bb);
old
});
});
}));
}

criterion_group!(benches, bench_write_hasher);
criterion_main!(benches);
12 changes: 4 additions & 8 deletions plain_hasher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate crunchy;
use core::hash::Hasher;

#[cfg(feature = "std")]
extern crate core;
use crunchy::unroll;

use core::hash;
/// Hasher that just takes 8 bytes of the provided value.
/// May only be used for keys which are 32 bytes.
#[derive(Default)]
pub struct PlainHasher {
prefix: u64,
}

impl hash::Hasher for PlainHasher {
impl Hasher for PlainHasher {
#[inline]
fn finish(&self) -> u64 {
self.prefix
Expand All @@ -57,8 +54,7 @@ impl hash::Hasher for PlainHasher {

#[cfg(test)]
mod tests {
use core::hash::Hasher;
use super::PlainHasher;
use super::*;

#[test]
fn it_works() {
Expand Down