10 releases

0.4.3 Mar 17, 2024
0.4.2 Oct 2, 2023
0.4.1 Feb 6, 2023
0.4.0 Jan 21, 2023
0.1.1 Dec 31, 2022

#271 in Algorithms


Used in 2 crates (via confitul)

MIT license

74KB
793 lines

CKey

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.

CKey icon

Status

For now this is a toy project, clearly NOT suitable for production use.

Build Status Crates.io Gitlab License

Usage

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));

Benchmarks

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

Links

License

Ckey is licensed under the MIT license.

Dependencies

~0.7–1.6MB
~34K SLoC