Skip to main content

ketama_hash_ring/maglev/
struct.rs

1use super::*;
2
3/// Precomputed Granlund-Montgomery reciprocal multiplier for branchless `u64` modulo.
4#[derive(Clone, Copy, Debug)]
5pub(crate) struct FastDivU64 {
6    /// Original divisor.
7    pub(crate) divisor: u64,
8    /// Reciprocal multiplier.
9    pub(crate) mul: u64,
10    /// First post-subtraction right shift.
11    pub(crate) sh1: u32,
12    /// Final right shift.
13    pub(crate) sh2: u32,
14}
15
16/// Google Maglev O(1) consistent hashing lookup table with minimal disruption on node changes.
17#[derive(Clone, Debug)]
18pub struct MaglevHash<N> {
19    /// Prime lookup table size `M`.
20    pub(crate) table_size: usize,
21    /// Precomputed fast reciprocal divider for `hash % table_size`.
22    pub(crate) fast_div: FastDivU64,
23    /// Backend node payloads in stable registration order.
24    pub(crate) values: Vec<N>,
25    /// Compact `M`-entry lookup table mapping slot -> index in `values`.
26    pub(crate) lookup: MaglevTable,
27}