Skip to content

Commit

Permalink
Highly optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu committed Sep 20, 2018
1 parent 14ed943 commit b24cfa8
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 118 deletions.
70 changes: 70 additions & 0 deletions comparison/benchmark_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

running 60 tests
test bench32::xxhash32_0_byte ... bench: 7 ns/iter (+/- 1)
test bench32::xxhash32_1024_byte ... bench: 2,147 ns/iter (+/- 370) = 7154 MB/s
test bench32::xxhash32_128_byte ... bench: 289 ns/iter (+/- 52) = 6643 MB/s
test bench32::xxhash32_16_byte ... bench: 37 ns/iter (+/- 5) = 6486 MB/s
test bench32::xxhash32_1_byte ... bench: 23 ns/iter (+/- 2) = 652 MB/s
test bench32::xxhash32_256_byte ... bench: 521 ns/iter (+/- 52) = 7370 MB/s
test bench32::xxhash32_32_byte ... bench: 70 ns/iter (+/- 10) = 6857 MB/s
test bench32::xxhash32_4_byte ... bench: 22 ns/iter (+/- 1) = 2727 MB/s
test bench32::xxhash32_512_byte ... bench: 1,028 ns/iter (+/- 296) = 7470 MB/s
test bench32::xxhash32_megabyte ... bench: 2,168,024 ns/iter (+/- 531,375) = 7254 MB/s
test bench32::zerohash32_0_byte ... bench: 2 ns/iter (+/- 0)
test bench32::zerohash32_1024_byte ... bench: 2,039 ns/iter (+/- 194) = 7533 MB/s
test bench32::zerohash32_128_byte ... bench: 261 ns/iter (+/- 8) = 7356 MB/s
test bench32::zerohash32_16_byte ... bench: 37 ns/iter (+/- 0) = 6486 MB/s
test bench32::zerohash32_1_byte ... bench: 7 ns/iter (+/- 1) = 2142 MB/s
test bench32::zerohash32_256_byte ... bench: 521 ns/iter (+/- 62) = 7370 MB/s
test bench32::zerohash32_32_byte ... bench: 71 ns/iter (+/- 9) = 6760 MB/s
test bench32::zerohash32_4_byte ... bench: 11 ns/iter (+/- 0) = 5454 MB/s
test bench32::zerohash32_512_byte ... bench: 1,030 ns/iter (+/- 59) = 7456 MB/s
test bench32::zerohash32_megabyte ... bench: 2,145,970 ns/iter (+/- 164,592) = 7329 MB/s
test bench64::fnvhash_0_byte ... bench: 0 ns/iter (+/- 0)
test bench64::fnvhash_1024_byte ... bench: 33,502 ns/iter (+/- 2,556) = 947 MB/s
test bench64::fnvhash_128_byte ... bench: 4,187 ns/iter (+/- 242) = 947 MB/s
test bench64::fnvhash_16_byte ... bench: 523 ns/iter (+/- 43) = 948 MB/s
test bench64::fnvhash_1_byte ... bench: 32 ns/iter (+/- 0) = 968 MB/s
test bench64::fnvhash_256_byte ... bench: 8,375 ns/iter (+/- 383) = 947 MB/s
test bench64::fnvhash_4_byte ... bench: 130 ns/iter (+/- 6) = 953 MB/s
test bench64::fnvhash_512_byte ... bench: 16,750 ns/iter (+/- 910) = 947 MB/s
test bench64::fnvhash_64_byte ... bench: 2,093 ns/iter (+/- 101) = 947 MB/s
test bench64::fnvhash_megabyte ... bench: 35,113,176 ns/iter (+/- 1,149,847) = 925 MB/s
test bench64::siphash_0_byte ... bench: 9 ns/iter (+/- 0)
test bench64::siphash_1024_byte ... bench: 13,503 ns/iter (+/- 748) = 2350 MB/s
test bench64::siphash_128_byte ... bench: 1,698 ns/iter (+/- 143) = 2336 MB/s
test bench64::siphash_16_byte ... bench: 224 ns/iter (+/- 19) = 2214 MB/s
test bench64::siphash_1_byte ... bench: 25 ns/iter (+/- 7) = 1240 MB/s
test bench64::siphash_256_byte ... bench: 3,423 ns/iter (+/- 179) = 2318 MB/s
test bench64::siphash_4_byte ... bench: 63 ns/iter (+/- 12) = 1968 MB/s
test bench64::siphash_512_byte ... bench: 6,811 ns/iter (+/- 366) = 2330 MB/s
test bench64::siphash_64_byte ... bench: 856 ns/iter (+/- 49) = 2317 MB/s
test bench64::siphash_megabyte ... bench: 14,323,033 ns/iter (+/- 1,883,569) = 2269 MB/s
test bench64::xxhash64_0_byte ... bench: 8 ns/iter (+/- 1)
test bench64::xxhash64_1024_byte ... bench: 2,110 ns/iter (+/- 149) = 15044 MB/s
test bench64::xxhash64_128_byte ... bench: 278 ns/iter (+/- 18) = 14273 MB/s
test bench64::xxhash64_16_byte ... bench: 49 ns/iter (+/- 7) = 10122 MB/s
test bench64::xxhash64_1_byte ... bench: 28 ns/iter (+/- 1) = 1107 MB/s
test bench64::xxhash64_256_byte ... bench: 540 ns/iter (+/- 142) = 14696 MB/s
test bench64::xxhash64_4_byte ... bench: 31 ns/iter (+/- 4) = 4000 MB/s
test bench64::xxhash64_512_byte ... bench: 1,067 ns/iter (+/- 102) = 14875 MB/s
test bench64::xxhash64_64_byte ... bench: 142 ns/iter (+/- 13) = 13971 MB/s
test bench64::xxhash64_megabyte ... bench: 2,533,382 ns/iter (+/- 172,558) = 12831 MB/s
test bench64::zerohash64_0_byte ... bench: 4 ns/iter (+/- 0)
test bench64::zerohash64_1024_byte ... bench: 2,112 ns/iter (+/- 184) = 15030 MB/s
test bench64::zerohash64_128_byte ... bench: 274 ns/iter (+/- 42) = 14481 MB/s
test bench64::zerohash64_16_byte ... bench: 42 ns/iter (+/- 4) = 11809 MB/s
test bench64::zerohash64_1_byte ... bench: 8 ns/iter (+/- 0) = 3875 MB/s
test bench64::zerohash64_256_byte ... bench: 540 ns/iter (+/- 54) = 14696 MB/s
test bench64::zerohash64_4_byte ... bench: 20 ns/iter (+/- 5) = 6200 MB/s
test bench64::zerohash64_512_byte ... bench: 1,058 ns/iter (+/- 38) = 15001 MB/s
test bench64::zerohash64_64_byte ... bench: 144 ns/iter (+/- 6) = 13777 MB/s
test bench64::zerohash64_megabyte ... bench: 2,610,637 ns/iter (+/- 99,186) = 12451 MB/s

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


running 0 tests

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

70 changes: 0 additions & 70 deletions comparison/result.txt

This file was deleted.

21 changes: 11 additions & 10 deletions comparison/src/bench32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use test;
use twox_hash::XxHash32;
use zero_xxhash::hash32::xxhash32;

const size_0: usize = 0;
const size_1: usize = 1;
const size_4: usize = 4;
const size_16: usize = 16;
const size_32: usize = 32;
const size_128: usize = 128;
const size_256: usize = 256;
const size_512: usize = 512;
const size_1024: usize = 1024;
const size_mega: usize = 1024 * 1024 * 8; // 8MB
const degree: usize = 15;
const size_0: usize = 0 * degree;
const size_1: usize = 1 * degree;
const size_4: usize = 4 * degree;
const size_16: usize = 16 * degree;
const size_32: usize = 32 * degree;
const size_128: usize = 128 * degree;
const size_256: usize = 256 * degree;
const size_512: usize = 512 * degree;
const size_1024: usize = 1024 * degree;
const size_mega: usize = 1024 * 1024 * degree;

fn hasher_bench<H>(b: &mut test::Bencher, mut hasher: H, len: usize)
where
Expand Down
21 changes: 11 additions & 10 deletions comparison/src/bench64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ use test;
use twox_hash::XxHash;
use zero_xxhash::hash64::xxhash64;

const size_0: usize = 0;
const size_1: usize = 1;
const size_4: usize = 4;
const size_16: usize = 16;
const size_64: usize = 64;
const size_128: usize = 128;
const size_256: usize = 256;
const size_512: usize = 512;
const size_1024: usize = 1024;
const size_mega: usize = 1024 * 1024 * 16; // 16MB
const degree: usize = 31;
const size_0: usize = 0 * degree;
const size_1: usize = 1 * degree;
const size_4: usize = 4 * degree;
const size_16: usize = 16 * degree;
const size_64: usize = 64 * degree;
const size_128: usize = 128 * degree;
const size_256: usize = 256 * degree;
const size_512: usize = 512 * degree;
const size_1024: usize = 1024 * degree;
const size_mega: usize = 1024 * 1024 * degree;

fn hasher_bench<H>(b: &mut test::Bencher, mut hasher: H, len: usize)
where
Expand Down
67 changes: 57 additions & 10 deletions src/hash32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,73 @@ fn xxh32_round(acc: u32, input: u32) -> u32 {
pub fn xxhash32(data: &[u8], seed: u32) -> u32 {
let mut h32: u32;
let n = data.len();
let limit = n - (n & 15);
let mut limit = n - (n & 15);

let mut ptr = data;
let mut acc1;
let mut acc2;
let mut acc3;
let mut acc4;

let mut t = 0;

if n >= 16 {
acc1 = seed.wrapping_add(PRIME32_1).wrapping_add(PRIME32_2);
acc2 = seed.wrapping_add(PRIME32_2);
acc3 = seed;
acc4 = seed.wrapping_sub(PRIME32_1);

while t < limit {
acc1 = xxh32_round(acc1, read_u32(&data[t..]));
acc2 = xxh32_round(acc2, read_u32(&data[t + 4..]));
acc3 = xxh32_round(acc3, read_u32(&data[t + 8..]));
acc4 = xxh32_round(acc4, read_u32(&data[t + 12..]));
t += 16;
if limit >> 6 > 0 {
for _i in 0..limit >> 6 {
acc1 = xxh32_round(acc1, read_u32(&ptr));
acc2 = xxh32_round(acc2, read_u32(&ptr[4..8]));
acc3 = xxh32_round(acc3, read_u32(&ptr[8..12]));
acc4 = xxh32_round(acc4, read_u32(&ptr[12..16]));

acc1 = xxh32_round(acc1, read_u32(&ptr[16..20]));
acc2 = xxh32_round(acc2, read_u32(&ptr[20..24]));
acc3 = xxh32_round(acc3, read_u32(&ptr[24..28]));
acc4 = xxh32_round(acc4, read_u32(&ptr[28..32]));

acc1 = xxh32_round(acc1, read_u32(&ptr[32..36]));
acc2 = xxh32_round(acc2, read_u32(&ptr[36..40]));
acc3 = xxh32_round(acc3, read_u32(&ptr[40..44]));
acc4 = xxh32_round(acc4, read_u32(&ptr[44..48]));

acc1 = xxh32_round(acc1, read_u32(&ptr[48..52]));
acc2 = xxh32_round(acc2, read_u32(&ptr[52..56]));
acc3 = xxh32_round(acc3, read_u32(&ptr[56..60]));
acc4 = xxh32_round(acc4, read_u32(&ptr[60..64]));

ptr = &ptr[64..];
}
limit = limit & 63;
}

if limit >> 5 > 0 {
for _i in 0..limit >> 5 {
acc1 = xxh32_round(acc1, read_u32(&ptr));
acc2 = xxh32_round(acc2, read_u32(&ptr[4..8]));
acc3 = xxh32_round(acc3, read_u32(&ptr[8..12]));
acc4 = xxh32_round(acc4, read_u32(&ptr[12..16]));

acc1 = xxh32_round(acc1, read_u32(&ptr[16..20]));
acc2 = xxh32_round(acc2, read_u32(&ptr[20..24]));
acc3 = xxh32_round(acc3, read_u32(&ptr[24..28]));
acc4 = xxh32_round(acc4, read_u32(&ptr[28..32]));

ptr = &ptr[32..];
}
limit = limit & 31;
}

if limit >> 4 > 0 {
for _i in 0..limit >> 4 {
acc1 = xxh32_round(acc1, read_u32(&ptr));
acc2 = xxh32_round(acc2, read_u32(&ptr[4..8]));
acc3 = xxh32_round(acc3, read_u32(&ptr[8..12]));
acc4 = xxh32_round(acc4, read_u32(&ptr[12..16]));

ptr = &ptr[16..];
}
}

h32 = acc1
Expand All @@ -63,10 +109,11 @@ pub fn xxhash32(data: &[u8], seed: u32) -> u32 {
h32 = seed.wrapping_add(PRIME32_5);
}

// add len to hash
h32 = h32.wrapping_add(n as u32);

// finalize
xxh32_finalize(&data[limit..], h32)
xxh32_finalize(&ptr, h32)
}

fn xxh32_finalize(data: &[u8], mut h32: u32) -> u32 {
Expand Down
Loading

0 comments on commit b24cfa8

Please sign in to comment.