#hash-table #binary-format #platform-independent #disk #structure #hash-map

odht

A Rust crate for hash tables that can be mapped from disk into memory without the need for up-front decoding

5 unstable releases

0.3.1 Oct 29, 2021
0.3.0 Sep 20, 2021
0.2.1 Sep 17, 2021
0.2.0 Sep 10, 2021
0.1.0 Jul 20, 2021

#414 in Data structures

Download history 15663/week @ 2024-07-19 16001/week @ 2024-07-26 15207/week @ 2024-08-02 15926/week @ 2024-08-09 14725/week @ 2024-08-16 14922/week @ 2024-08-23 15353/week @ 2024-08-30 14818/week @ 2024-09-06 14087/week @ 2024-09-13 15573/week @ 2024-09-20 14299/week @ 2024-09-27 15288/week @ 2024-10-04 15027/week @ 2024-10-11 15502/week @ 2024-10-18 16458/week @ 2024-10-25 17044/week @ 2024-11-01

66,292 downloads per month
Used in 3 crates

MIT/Apache

76KB
1.5K SLoC

CI Status

odht

A Rust crate for hash tables that can be mapped from disk into memory without the need for up-front decoding. The goal of the implementation is to provide a data structure that

  • can be used exactly in the format it is stored on disk,
  • provides roughly the same performance as a HashMap from Rust's standard library,
  • has a completely deterministic binary representation,
  • is platform and endianess independent, so that data serialized on one system can be used on any other system, and
  • is independent of alignment requirements so that
    • its use is not restricted to certain classes of CPUs, and
    • the data structure can be mapped to arbitrary memory addresses.

This crate is developed and maintained by the Rust compiler team for internal use within rustc. This crate will have regular breaking changes and provides no stability guarantees.


lib.rs:

This crate implements a hash table that can be used as is in its binary, on-disk format. The goal is to provide a high performance data structure that can be used without any significant up-front decoding. The implementation makes no assumptions about alignment or endianess of the underlying data, so a table encoded on one platform can be used on any other platform and the binary data can be mapped into memory at arbitrary addresses.

Usage

In order to use the hash table one needs to implement the Config trait. This trait defines how the table is encoded and what hash function is used. With a Config in place the HashTableOwned type can be used to build and serialize a hash table. The HashTable type can then be used to create an almost zero-cost view of the serialized hash table.


use odht::{HashTable, HashTableOwned, Config, FxHashFn};

struct MyConfig;

impl Config for MyConfig {

    type Key = u64;
    type Value = u32;

    type EncodedKey = [u8; 8];
    type EncodedValue = [u8; 4];

    type H = FxHashFn;

    #[inline] fn encode_key(k: &Self::Key) -> Self::EncodedKey { k.to_le_bytes() }
    #[inline] fn encode_value(v: &Self::Value) -> Self::EncodedValue { v.to_le_bytes() }
    #[inline] fn decode_key(k: &Self::EncodedKey) -> Self::Key { u64::from_le_bytes(*k) }
    #[inline] fn decode_value(v: &Self::EncodedValue) -> Self::Value { u32::from_le_bytes(*v)}
}

fn main() {
    let mut builder = HashTableOwned::<MyConfig>::with_capacity(3, 95);

    builder.insert(&1, &2);
    builder.insert(&3, &4);
    builder.insert(&5, &6);

    let serialized = builder.raw_bytes().to_owned();

    let table = HashTable::<MyConfig, &[u8]>::from_raw_bytes(
        &serialized[..]
    ).unwrap();

    assert_eq!(table.get(&1), Some(2));
    assert_eq!(table.get(&3), Some(4));
    assert_eq!(table.get(&5), Some(6));
}

Dependencies