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 5 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 test --benches && cd ..
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- cd plain_hasher/ && cargo test --no-default-features && cargo test --benches && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cargo check --benches && cd ..

Copy link
Contributor Author

@koushiro koushiro Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just my personal convention. cargo test --benches is building all benchmarkable targets and running them as tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo test --benches runs tests as well. And it seems it actually runs the benches. So that's not what we want here right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it runs the benchmarks like that (include running unit-tests):

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

...

Testing write_plain_hasher
Success

Testing write_default_hasher
Success

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and we do not wish to run the benchmarks on CI.

(Not that it's a bad thing per se to run the benches, but it's not worth anything if we don't collect the output performance data and store it somewhere, and we're not so for the time being running the benches just slows CI down for no benefit.)

- 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