[libcxx-commits] [libcxx] [libc++] Optimize hash table lookup (PR #230430)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 13 03:12:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

It seems that on modern hardware the extra branch for avoiding a division isn't worth the avoided compute anymore.

---
Full diff: https://github.com/llvm/llvm-project/pull/230430.diff


1 Files Affected:

- (modified) libcxx/include/__hash_table (+1-1) 


``````````diff
diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index a03aa03090424..5692705cb457d 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -171,7 +171,7 @@ public:
 inline _LIBCPP_HIDE_FROM_ABI bool __is_hash_power2(size_t __bc) { return __bc > 2 && !(__bc & (__bc - 1)); }
 
 inline _LIBCPP_HIDE_FROM_ABI size_t __constrain_hash(size_t __h, size_t __bc) {
-  return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : (__h < __bc ? __h : __h % __bc);
+  return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
 }
 
 inline _LIBCPP_HIDE_FROM_ABI size_t __next_hash_pow2(size_t __n) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/230430


More information about the libcxx-commits mailing list