Skip to content

Commit

Permalink
feat: impl hex decode
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Dec 26, 2018
1 parent a41d322 commit abb37fa
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 48 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "faster-hex"
version = "0.1.1"
version = "0.3.0"
authors = ["zhangsoledad <787953403@qq.com>"]
edition = "2018"
keywords = ["simd", "hex"]
Expand All @@ -18,5 +18,10 @@ hex = "0.3.2"
proptest = "0.8"

[[bench]]
name = "hex_encode"
name = "hex"
harness = false


[[bench]]
name = "check"
harness = false
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ destination using various different instruction sets.
## License

This project is licensed under the [MIT license](LICENSE).

### Third party software

This product includes copies and modifications of software developed by third parties:

* [src/encode.rs](src/encode.rs) is based on
[stdsimd](https://github.com/rust-lang-nursery/stdsimd), licensed
under the MIT license or the Apache License (Version 2.0).

See the source code files for more details.

Copies of third party licenses can be found in [LICENSE-THIRD-PARTY](LICENSE-THIRD-PARTY).
38 changes: 38 additions & 0 deletions benches/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use faster_hex::{hex_check_fallback, hex_check_sse};

fn bench(c: &mut Criterion) {
let s1 = "Bf9E2d38aceDeeCbbAfccc4B4B7AE";
let s2 = "ed136fFDdCcC1DbaFE8CB6Df1AdDBAea44aCcC17b0DbC2741F9CeEeaFbE7A51D";
let s3 = " \u{0} 𐀀G\u{0}𐀀 GG\u{0}𐀀G\u{0}Gࠀ\u{0} 𐀀 \u{0}:\u{0}\u{0}gࠀG G::GG::g𐀀G𐀀\u{0}\u{0}¡𐀀ࠀ\u{0}:GGG Gg𐀀 :\u{0}:gG ¡";
let s4 = "ed136fFDdCcC1DbaFE8CB6Df1AdDBAea44aCcC17b0DbC2741F9CeEeaFbE7A51D\u{0} 𐀀G\u{0}𐀀 GG\u{0}𐀀G\u{0}Gࠀ\u{0} 𐀀 \u{0}:\u{0}\u{0}gࠀG G::GG::g𐀀G𐀀\u{0}\u{0}¡𐀀ࠀ\u{0}:GGG Gg𐀀 :\u{0}:gG ¡";

c.bench_function("bench_check_fallback", move |b| {
b.iter(|| {
let ret1 = hex_check_fallback(s1.as_bytes());
black_box(ret1);
let ret2 = hex_check_fallback(s2.as_bytes());
black_box(ret2);
let ret3 = hex_check_fallback(s3.as_bytes());
black_box(ret3);
let ret4 = hex_check_fallback(s4.as_bytes());
black_box(ret4);
})
});

c.bench_function("bench_check_sse", move |b| {
b.iter(|| {
let ret1 = unsafe { hex_check_sse(s1.as_bytes()) };
black_box(ret1);
let ret2 = unsafe { hex_check_sse(s2.as_bytes()) };
black_box(ret2);
let ret3 = unsafe { hex_check_sse(s3.as_bytes()) };
black_box(ret3);
let ret4 = unsafe { hex_check_sse(s4.as_bytes()) };
black_box(ret4);
})
});
}

criterion_group!(benches, bench);
criterion_main!(benches);
81 changes: 81 additions & 0 deletions benches/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use faster_hex::{hex_decode, hex_decode_fallback, hex_decode_unchecked, hex_string, hex_encode_fallback};
use rustc_hex::{FromHex, ToHex};

fn bench(c: &mut Criterion) {
let s = "Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.";

c.bench_function("bench_rustc_hex", move |b| {
b.iter(|| {
let ret = s.as_bytes().to_hex();
black_box(ret);
})
});

c.bench_function("bench_hex_hex", move |b| {
b.iter(|| {
let ret = hex::encode(s);
black_box(ret);
})
});

c.bench_function("bench_faster_hex", move |b| {
b.iter(|| {
let ret = hex_string(s.as_bytes()).unwrap();
black_box(ret);
})
});

c.bench_function("bench_faster_hex_fallback", move |b| {
b.iter(|| {
let bytes = s.as_bytes();
let mut buffer = vec![0; bytes.len() * 2];
let ret = hex_encode_fallback(bytes, &mut buffer);
black_box(ret);
})
});

c.bench_function("bench_rustc_hex_decode", move |b| {
let hex = s.as_bytes().to_hex();
b.iter(|| {
let ret: Vec<u8> = hex.from_hex().unwrap();
black_box(ret);
})
});

c.bench_function("bench_faster_hex_decode", move |b| {
let hex = hex_string(s.as_bytes()).unwrap();
let len = s.as_bytes().len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
let ret = hex_decode(hex.as_bytes(), &mut dst);
black_box(ret);
})
});

c.bench_function("bench_faster_hex_decode_unchecked", move |b| {
let hex = hex_string(s.as_bytes()).unwrap();
let len = s.as_bytes().len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
let ret = hex_decode_unchecked(hex.as_bytes(), &mut dst);
black_box(ret);
})
});

c.bench_function("bench_faster_hex_decode_fallback", move |b| {
let hex = hex_string(s.as_bytes()).unwrap();
let len = s.as_bytes().len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
let ret = hex_decode_fallback(hex.as_bytes(), &mut dst);
black_box(ret);
})
});
}

criterion_group!(benches, bench);
criterion_main!(benches);
31 changes: 0 additions & 31 deletions benches/hex_encode.rs

This file was deleted.

Empty file added proptest-regressions/.gitkeep
Empty file.
Loading

0 comments on commit abb37fa

Please sign in to comment.