Lobsters | 📄 原文链接 | 2026-10-29 收录

每秒十亿次查表的完美哈希:把字符串查找压到五个周期

来源:lemire.me — 2026-10-26

📋 概述

当键在写代码时就已经确定,运行时为处理碰撞付出的代价全是浪费。Daniel Lemire 介绍了与微软 Francisco Geiman Thiesen 合作的 C++23 单头文件库:用 constexpr 在编译期搜索完美哈希函数,运行时算哈希只要四条指令,再用 16 字节向量比较一次完成键校验。在 Apple M3 Max 上查 20 个 HTTP 头名约 1.3 纳秒(约 5 个周期),gperf 要 7.7 纳秒,Abseil flat_hash_map 要 10.9 纳秒,std::unordered_map 要 16.6 纳秒。在 M4 上处理 URL 协议集时突破每秒十亿次。

🔑 核心要点

💡 金句

If your keys are known at compile time, you should not pay for collision handling at runtime. A perfect hash function gets you down to one comparison.
← 返回 Lobsters 首页