Crates.io | hulahoop |
lib.rs | hulahoop |
version | 0.2.0 |
source | src |
created_at | 2022-10-08 10:35:14.207043 |
updated_at | 2022-10-29 10:27:24.553102 |
description | An efficient consistent hash ring implementation supporting virtual nodes. |
homepage | |
repository | https://github.com/ajesipow/hulahoop |
max_upload_size | |
id | 683454 |
size | 30,747 |
A fast and efficient consistent hashing implementation, with support for virtual nodes.
use hulahoop::HashRing;
let mut ring: HashRing<&str, _> = HashRing::default();
// Nodes only need to implement Hash
// Provide a weight to define the number of virtual nodes
ring.insert("10.0.0.1:1234", 10);
ring.insert("10.0.0.2:1234", 10);
// Keys also only need to implement Hash
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.2:1234"));
ring.remove(&"10.0.0.2:1234");
assert_eq!(ring.get("Some key"), Some(&"10.0.0.1:1234"));
assert_eq!(ring.get("Another key"), Some(&"10.0.0.1:1234"));
HashRing
uses Arc
under the hood to allocate memory only per node and not for every virtual node added via the weight parameter.
The Hashring
is Send + Sync
.
Per default, hulahoop
uses std::collections::hash_map::DefaultHasher
to hash values.
Custom hashers can be used with the HashRing::with_hasher()
method:
use rustc_hash::FxHasher;
let mut ring: HashRing<&str, _> = HashRing::with_hasher(BuildHasherDefault::<FxHasher>::default());
For convenience, the faster hasher FxHasher can be used by activating the fxhash
feature of this crate.
DefaultHasher | FxHasher (feature=fxhash) | |
---|---|---|
Get (key length = 10) | 13ns | 8ns |
Get (key length = 100) | 31ns | 12ns |
Get (key length = 1000) | 305ns | 137ns |
Add (weight = 1) | 290ns | 210ns |
Add (weight = 10) | 1.4us | 1.0us |
Add (weight = 100) | 17.0us | 14.3us |
This project is licensed optionally under either: