[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 04:31:29 PDT 2026
https://github.com/xbcnn updated https://github.com/llvm/llvm-project/pull/216551
>From d6c074098ff978d447d96577df3879f7bb92c28c Mon Sep 17 00:00:00 2001
From: arbin <xbcnn at 163.com>
Date: Sun, 16 Aug 2026 16:24:05 +0800
Subject: [PATCH] [libc++] Avoid key_eq calls in hash_table rehash
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.
---
libcxx/include/__hash_table | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index ed58789378bc1..d2ce69fda8e57 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -1773,9 +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());
- __np = __np->__next_)
+ for (; __np->__next_ != nullptr && __cp->__hash() == __np->__next_->__hash(); __np = __np->__next_)
;
}
__pp->__next_ = __np->__next_;
More information about the libcxx-commits
mailing list