[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:21 PDT 2026
https://github.com/xbcnn created https://github.com/llvm/llvm-project/pull/216551
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.
>From 636acf7fe32ec377e24eabdbbba6569d9905fab7 Mon Sep 17 00:00:00 2001
From: arbin <xbcnn at 163.com>
Date: Sun, 16 Aug 2026 15:25:37 +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 | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
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_)
;
}
More information about the libcxx-commits
mailing list