Daniel Lemire 与 Francisco Geiman Thiesen 合作开发了名为 ConstexprCore/perfect_hash 的开源单头文件库 1。该库旨在通过完美哈希技术处理编译期已知的静态键值对,从而消除运行时碰撞处理 1。成果于 2026 年 10 月 26 日发布,并在 P99 CONF 2026 会议上展示 1。
在技术实现上,该方法采用贪心算法在编译时确定关联值,哈希槽计算遵循公式 slot = (length + asso[key[i]] + ...) % table_size 1。为加速查找,开发团队利用 ARM/x64 架构的 16 字节寄存器加载并配合 shuffle 指令进行向量化字符串比较 1。这一优化将原本约需 40 条指令和 8 个分支的过程减少至 5 条指令且无分支执行 1。
基准测试显示,ConstexprCore/perfect_hash 显著优于 std::unordered_map 和传统 gperf 工具,查找速度快 7 到 13 倍 1。具体数据表明,在 Apple M3 Max 上针对标准 HTTP 头名称查找耗时约 1.3 ns/次,而 std::unordered_map 需 16.6 ns,速度快 13 倍 1。在峰值性能方面,该库在 Apple M4 上处理 URL 协议时实现了超过每秒十亿次(>1 billion lookups per second)的查找操作 1。
Daniel Lemire and Francisco Geiman Thiesen have introduced a new technique for implementing ultra-fast lookup tables in C++, capable of exceeding one billion queries per second 1. Published on October 26, 2026, the method leverages perfect hashing to eliminate runtime collision handling by generating collision-free hash functions specifically for static key-value pairs known at compile time 1. This approach significantly outperforms standard library containers like std::unordered_map and traditional tools such as gperf 1.
The core innovation involves calculating hash slots using the formula slot = (length + asso[key[i]] + ...) % table_size, where associated values are determined via a greedy algorithm during compilation 1. To further accelerate string comparisons, the implementation utilizes 16-byte register loads combined with shuffle instructions available on ARM and x64 architectures 1. This optimization reduces the processing overhead from approximately 40 instructions and 8 branches down to just 5 instructions with no branching 1.
Benchmark tests conducted on an Apple M3 Max processor demonstrate the performance gains clearly. For looking up standard HTTP header names, the new method achieves a latency of about 1.3 nanoseconds per query, whereas std::unordered_map requires 16.6 nanoseconds, marking a speed increase of 13 times 1. When testing URL protocol lookups on an Apple M4 chip, the system sustained a peak throughput of over one billion lookups per second 1. The results were presented at the P99 CONF 2026 conference, and the technology is available as a single-header open-source library named ConstexprCore/perfect_hash, hosted on GitHub 1.
评论
还没有评论,欢迎留下第一条。