Using consteval from C++23, we implement efficient, new versions of std::map and std::unordered_map for use when the keys are known at compile time. We demonstrate superior performance of our unordered_map on three demonstration use-cases: Lookup of elemental mass from atomic symbol, lookup of amino acid from codon, and modification of stock prices from S&P 500 ticker symbols all produced runtimes <40%, <35%, <73% of the respective runtimes of the std implementations. Our library runimes were <80%, <45%, <97% of the lookup time of Frozen, an alternative perfect hashing implementation in C++ for problems also using constexpr keys. To our knowledge, this makes our library the overall fastest drop-in (i.e., with a similar API) alternative to std::unordered_map. On one arbitrarily chosen demo, we demonstrate runtimes <35% of PTHash and <89% gperf, state-of-the-art but not drop-in hashing libraries via external tools.
翻译:利用 C++23 中的 consteval 特性,我们实现了高效的新版本 std::map 和 std::unordered_map,适用于键值在编译时已知的场景。我们在三个示例用例中展示了我们实现的 unordered_map 的优越性能:根据原子符号查找元素质量、根据密码子查找氨基酸、根据标普 500 股票代码修改股价,其运行时间分别低于标准库实现的 40%、35% 和 73%。与同样使用 constexpr 键值的 C++ 替代完美哈希实现 Frozen 相比,我们库的查找时间分别低于其 80%、45% 和 97%。据我们所知,这使得我们的库成为与 std::unordered_map API 相似的直接替代方案中整体最快的选择。在一个任意选取的演示案例中,我们展示了运行时间低于 PTHash 的 35% 和低于 gperf 的 89%,这两者是通过外部工具实现的最新但非直接替代的哈希库。