[libcxx-commits] [libcxx] [libc++] Avoid key_eq calls in hash_table rehash (PR #216551)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 16 01:58:54 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: xbcnn

<details>
<summary>Changes</summary>

We can use cached hash to avoid calling key_eq which might be costly.

For unordered_multiset and unordered_multimap, rehashing preserves the relative ordering of equivalent elements. These elements are contiguous and share the same key and hash.
For elements with the same hash but different keys, if any, their relative order is not required, but we preserve it for simplicity.

This is still not applied to unique-key containers (unordered_set and unordered_map), where same-hash contiguous blocks are rare and the extra check would not be worthwhile.



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


1 Files Affected:

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


``````````diff
diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index ed58789378bc1..55a520a81c728 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -1773,8 +1773,7 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __bucket_co
       } else {
         __next_pointer __np = __cp;
         if _LIBCPP_CONSTEXPR (!_UniqueKeys) {
-          for (; __np->__next_ != nullptr &&
-                 key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
+          for (; __np->__next_ != nullptr && __cp->__hash() == __np->__next_->__hash();
                __np = __np->__next_)
             ;
         }

``````````

</details>


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


More information about the libcxx-commits mailing list