1
- # TwoX-Hash
1
+ A Rust implementation of the [ xxHash ] algorithm.
2
2
3
- A Rust implementation of the [ XXHash] algorithm.
3
+ [ ![ Crates.io] [ crates-badge ]] [ crates-url ]
4
+ [ ![ Documentation] [ docs-badge ]] [ docs-url ]
5
+ [ ![ Build Status] [ actions-badge ]] [ actions-url ]
4
6
5
- [ ![ Build Status ] ( https://travis-ci.org/shepmaster/twox-hash.svg )] ( https://travis-ci.org/shepmaster/twox-hash ) [ ![ Current Version ] ( https://img.shields.io/crates/v/twox-hash.svg )] ( https://crates.io/crates/twox-hash )
7
+ [ xxHash ] : https://github.com/Cyan4973/xxHash
6
8
7
- [ Documentation] ( https://docs.rs/twox-hash/ )
9
+ [ crates-badge ] : https://img.shields.io/crates/v/twox-hash.svg
10
+ [ crates-url ] : https://crates.io/crates/twox-hash
11
+ [ docs-badge ] : https://img.shields.io/docsrs/twox-hash
12
+ [ docs-url ] : https://docs.rs/twox-hash/
13
+ [ actions-badge ] : https://github.com/shepmaster/twox-hash/actions/workflows/ci.yml/badge.svg?branch=main
14
+ [ actions-url ] : https://github.com/shepmaster/twox-hash/actions/workflows/ci.yml?query=branch%3Amain
8
15
9
- [ XXHash ] : https://github.com/Cyan4973/xxHash
16
+ # Examples
10
17
11
- ## Examples
18
+ These examples use [ ` XxHash64 ` ] [ ] but the same ideas can be used for
19
+ [ ` XxHash32 ` ] [ ] or [ ` XxHash3_64 ` ] [ ] .
12
20
13
- ### With a fixed seed
21
+ ## Hashing arbitrary data
22
+
23
+ ### When all the data is available at once
14
24
15
25
``` rust
16
- use std :: hash :: BuildHasherDefault ;
17
- use std :: collections :: HashMap ;
18
26
use twox_hash :: XxHash64 ;
19
27
20
- let mut hash : HashMap <_ , _ , BuildHasherDefault <XxHash64 >> = Default :: default ();
28
+ let seed = 1234 ;
29
+ let hash = XxHash64 :: oneshot (seed , b " some bytes" );
30
+ assert_eq! (0xeab5_5659_a496_d78b , hash );
31
+ ```
32
+
33
+ ### When the data is streaming
34
+
35
+ ``` rust
36
+ use std :: hash :: Hasher as _;
37
+ use twox_hash :: XxHash64 ;
38
+
39
+ let seed = 1234 ;
40
+ let mut hasher = XxHash64 :: with_seed (seed );
41
+ hasher . write (b " some" );
42
+ hasher . write (b " " );
43
+ hasher . write (b " bytes" );
44
+ let hash = hasher . finish ();
45
+ assert_eq! (0xeab5_5659_a496_d78b , hash );
46
+ ```
47
+
48
+ ## In a [ ` HashMap ` ] ( std::collections::HashMap )
49
+
50
+ ### With a default seed
51
+
52
+ ``` rust
53
+ use std :: {collections :: HashMap , hash :: BuildHasherDefault };
54
+ use twox_hash :: XxHash64 ;
55
+
56
+ let mut hash = HashMap :: <_ , _ , BuildHasherDefault <XxHash64 >>:: default ();
21
57
hash . insert (42 , " the answer" );
22
58
assert_eq! (hash . get (& 42 ), Some (& " the answer" ));
23
59
```
@@ -26,73 +62,33 @@ assert_eq!(hash.get(&42), Some(&"the answer"));
26
62
27
63
``` rust
28
64
use std :: collections :: HashMap ;
29
- use twox_hash :: RandomXxHashBuilder64 ;
65
+ use twox_hash :: xxhash64;
66
+
67
+ let mut hash = HashMap :: <_ , _ , xxhash64 :: RandomState >:: default ();
68
+ hash . insert (42 , " the answer" );
69
+ assert_eq! (hash . get (& 42 ), Some (& " the answer" ));
70
+ ```
71
+
72
+ ### With a fixed seed
73
+
74
+ ``` rust
75
+ use std :: collections :: HashMap ;
76
+ use twox_hash :: xxhash64;
30
77
31
- let mut hash : HashMap < _ , _ , RandomXxHashBuilder64 > = Default :: default ( );
78
+ let mut hash = HashMap :: with_hasher ( xxhash64 :: State :: with_seed ( 0xdead_cafe ) );
32
79
hash . insert (42 , " the answer" );
33
80
assert_eq! (hash . get (& 42 ), Some (& " the answer" ));
34
81
```
35
82
36
- ## Benchmarks
37
-
38
- ### 64-bit
39
-
40
- | Bytes | SipHasher (MB/s) | XXHash (MB/s) | Ratio |
41
- | ---------| ------------------| ---------------| -------|
42
- | 1 | 52 | 38 | 73% |
43
- | 4 | 210 | 148 | 70% |
44
- | 16 | 615 | 615 | 100% |
45
- | 32 | 914 | 1391 | 152% |
46
- | 128 | 1347 | 3657 | 271% |
47
- | 256 | 1414 | 5019 | 355% |
48
- | 512 | 1546 | 6168 | 399% |
49
- | 1024 | 1565 | 6206 | 397% |
50
- | 1048576 | 1592 | 7564 | 475% |
51
-
52
- | Bytes | [ FnvHasher] [ fnv ] (MB/s) | XXHash (MB/s) | Ratio |
53
- | ---------| -------------------------| ---------------| -------|
54
- | 1 | 1000 | 38 | 4% |
55
- | 4 | 800 | 148 | 19% |
56
- | 16 | 761 | 615 | 81% |
57
- | 32 | 761 | 1391 | 183% |
58
- | 128 | 727 | 3657 | 503% |
59
- | 256 | 759 | 5019 | 661% |
60
- | 512 | 745 | 6168 | 828% |
61
- | 1024 | 741 | 6206 | 838% |
62
- | 1048576 | 745 | 7564 | 1015% |
63
-
64
- ### 32-bit
65
-
66
- | Bytes | SipHasher (MB/s) | XXHash32 (MB/s) | Ratio |
67
- | ---------| ------------------| -----------------| -------|
68
- | 1 | 52 | 55 | 106% |
69
- | 4 | 210 | 210 | 100% |
70
- | 16 | 615 | 1230 | 200% |
71
- | 32 | 914 | 1882 | 206% |
72
- | 128 | 1347 | 3282 | 244% |
73
- | 256 | 1414 | 3459 | 245% |
74
- | 512 | 1546 | 3792 | 245% |
75
- | 1024 | 1565 | 3938 | 252% |
76
- | 1048576 | 1592 | 4127 | 259% |
77
-
78
- | Bytes | [ FnvHasher] [ fnv ] (MB/s) | XXHash32 (MB/s) | Ratio |
79
- | ---------| -------------------------| -----------------| -------|
80
- | 1 | 1000 | 55 | 6% |
81
- | 4 | 800 | 210 | 26% |
82
- | 16 | 761 | 1230 | 162% |
83
- | 32 | 761 | 1882 | 247% |
84
- | 128 | 727 | 3282 | 451% |
85
- | 256 | 759 | 3459 | 456% |
86
- | 512 | 745 | 3792 | 509% |
87
- | 1024 | 741 | 3938 | 531% |
88
- | 1048576 | 745 | 4127 | 554% |
89
-
90
-
91
- [ fnv ] : https://github.com/servo/rust-fnv
92
-
93
- ## Contributing
94
-
95
- 1 . Fork it ( https://github.com/shepmaster/twox-hash/fork )
83
+ # Benchmarks
84
+
85
+ See benchmarks in the [ comparison] [ ] README.
86
+
87
+ [ comparison ] : https://github.com/shepmaster/twox-hash/tree/main/comparison
88
+
89
+ # Contributing
90
+
91
+ 1 . Fork it (< https://github.com/shepmaster/twox-hash/fork > )
96
92
2 . Create your feature branch (` git checkout -b my-new-feature ` )
97
93
3 . Add a failing test.
98
94
4 . Add code to pass the test.
0 commit comments