#hash #positive #64-bit #performance #hash-functions #hash-map #t1ah

no-std t1ha

T1AH(快速正哈希)哈希函数的实现

3个版本

0.1.2 2024年2月19日
0.1.1 2024年2月19日
0.1.0 2019年3月7日

#112算法

Download history 1140/week @ 2024-04-23 553/week @ 2024-04-30 725/week @ 2024-05-07 932/week @ 2024-05-14 860/week @ 2024-05-21 782/week @ 2024-05-28 675/week @ 2024-06-04 429/week @ 2024-06-11 972/week @ 2024-06-18 706/week @ 2024-06-25 708/week @ 2024-07-02 489/week @ 2024-07-09 560/week @ 2024-07-16 783/week @ 2024-07-23 745/week @ 2024-07-30 904/week @ 2024-08-06

3,091 每月下载量
8 软件包中使用(2个直接使用)

Zlib 许可协议

82KB
2K SLoC

rust-t1ha 持续集成 appveyor crate docs

T1HA(快速正哈希) 哈希函数的实现。

简而言之,它是一个可移植的64位哈希函数

  • 专为64位小端平台设计,主要是Elbrus和x86_64,但无需任何惩罚即可在任何64位CPU上运行。
  • 通常比StadtX哈希、xxHash、mum-hash、metro-hash等快15%以上,以及所有其他可移植哈希函数(这些函数不使用特定的硬件技巧)。
  • 提供了一套分级的哈希函数。
  • 目前不适用于加密。
  • 根据 zlib License 许可。

使用方法

要将此软件包包含到您的程序中,请将以下内容添加到您的 Cargo.toml

[dependencies]
t1ha = "0.1"

HashMap 中使用 t1ha

T1haHashMap 类型别名是使用标准库的 HashMapt1ha 的最简单方法。

use t1ha::T1haHashMap;

let mut map = T1haHashMap::default();
map.insert(1, "one");
map.insert(2, "two");

map = T1haHashMap::with_capacity_and_hasher(10, Default::default());
map.insert(1, "one");
map.insert(2, "two");

注意:标准库的 HashMap::newHashMap::with_capacity 仅实现了 RandomState 哈希器,因此使用 Default 获取哈希器是次优选择。

HashSet 中使用 t1ha

同样,T1haHashSet 是标准库的 HashSet 使用 `t1ha` 的类型别名。

use t1ha::T1haHashSet;

let mut set = T1haHashSet::default();
set.insert(1);
set.insert(2);

set = T1haHashSet::with_capacity_and_hasher(10, Default::default());
set.insert(1);
set.insert(2);

性能

t1ha 可以使用AES、AVX或AVX2指令作为硬件加速。

实现 平台/CPU
t1ha0_ia32aes_avx() 带有AES-NI和AVX扩展的x86
t1ha0_ia32aes_avx2() 带有AES-NI和AVX2扩展的x86
t1ha0_ia32aes_noavx() 带有AES-NI但不带AVX扩展的x86
t1ha0_32le() 32位小端模式
t1h0a_32be() 32位大端模式
t1ha1_le() 64位小端模式
t1ha1_be() 64位大端模式
t1ha2_atonce() 64位小端模式

您可以根据您的 target_cpu 选择合适的实现。

$ RUSTFLAGS="-C target-cpu=native" cargo build

基准测试

rust-t1ha 提供了与其他 Rust 实现的非加密哈希函数的 粗略性能比较,您可以根据您的环境和使用场景运行基准测试。

$ RUSTFLAGS="-C target-cpu=native" cargo bench

原生 t1ha

rust-t1ha 主要关注 Rust 实现,如果您打算使用原始的本地 t1ha 库,请检查 rust-fasthash 项目及其基准测试,它提供了一系列来自 SMHasher 的非加密哈希函数。

依赖项

~170KB