Crates.io | ckey |
lib.rs | ckey |
version | 0.4.3 |
source | src |
created_at | 2022-12-28 13:18:29.3733 |
updated_at | 2024-03-17 13:06:50.383885 |
description | CKey is a consistent hash key library. |
homepage | https://gitlab.com/liberecofr/ckey |
repository | https://gitlab.com/liberecofr/ckey/tree/main |
max_upload_size | |
id | 746840 |
size | 123,536 |
CKey is an experimental consistent hash key library, implemented in Rust.
See the theory about consistent hashing. This library could be used as a brick to implement a Chord ring. It provides 256-bit keys, with some helpers to add, compare, and know their position on the ring. Internally, keys are stored on 4 unsigned integers of 64-bit each.
For now this is a toy project, clearly NOT suitable for production use.
use ckey::CKey;
let k1 = CKey::rand();
let k2 = k1.next();
let k3 = k2 + 10u8;
assert!(k2.inside(k1, k3));
let k4 = CKey::from(1000u16);
assert_eq!("0.015258789", format!("{}", k4));
Taken from a random CI job:
running 6 tests
test tests::bench_bytes ... bench: 28 ns/iter (+/- 0)
test tests::bench_display ... bench: 204 ns/iter (+/- 3)
test tests::bench_from_f64 ... bench: 10 ns/iter (+/- 0)
test tests::bench_incr ... bench: 2 ns/iter (+/- 0)
test tests::bench_inside ... bench: 10 ns/iter (+/- 1)
test tests::bench_serial ... bench: 468 ns/iter (+/- 106)
test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured; 0 filtered out; finished in 6.37s
This is not the result of extensive, thorough benchmarking, just a random snapshot at some point in development history.
TL;DR relatively fast, though slower than standard integer ops.
The implementation tries to optimize for the speed of operations like incr
or inside
which
are typically repeated a lot of times in a real-world program. OTOH creation and serialization
of keys are rather costly.
To run the benchmarks:
cd bench
rustup default nightly
cargo bench
Ckey is licensed under the MIT license.