Skip to content

Commit 282d0fc

Browse files
goldvitalycopybara-github
authored andcommitted
Simplify calls to EqualElement by introducing equal_to helper function.
I am marking one liner helper functions as `ABSL_ATTRIBUTE_ALWAYS_INLINE` to avoid them ending up as weak symbols for the linker. PiperOrigin-RevId: 761180450 Change-Id: I3593a5a9e8317df7714715608bc7309c3fcc8bbb
1 parent 5914831 commit 282d0fc

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

absl/container/internal/raw_hash_set.h

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,11 +2992,7 @@ class raw_hash_set {
29922992
template <class K = key_type>
29932993
iterator find_small(const key_arg<K>& key) {
29942994
ABSL_SWISSTABLE_ASSERT(is_small());
2995-
return empty() || !PolicyTraits::apply(
2996-
EqualElement<K, key_equal>{key, eq_ref()},
2997-
PolicyTraits::element(single_slot()))
2998-
? end()
2999-
: single_iterator();
2995+
return empty() || !equal_to(key, single_slot()) ? end() : single_iterator();
30002996
}
30012997

30022998
template <class K = key_type>
@@ -3011,9 +3007,7 @@ class raw_hash_set {
30113007
#endif
30123008
Group g{ctrl + seq.offset()};
30133009
for (uint32_t i : g.Match(h2)) {
3014-
if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
3015-
EqualElement<K, key_equal>{key, eq_ref()},
3016-
PolicyTraits::element(slot_array() + seq.offset(i)))))
3010+
if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i))))
30173011
return iterator_at(seq.offset(i));
30183012
}
30193013
if (ABSL_PREDICT_TRUE(g.MaskEmpty())) return end();
@@ -3094,17 +3088,25 @@ class raw_hash_set {
30943088
}
30953089

30963090
template <class K>
3097-
size_t hash_of(const K& key) const {
3091+
ABSL_ATTRIBUTE_ALWAYS_INLINE bool equal_to(const K& key,
3092+
slot_type* slot) const {
3093+
return PolicyTraits::apply(EqualElement<K, key_equal>{key, eq_ref()},
3094+
PolicyTraits::element(slot));
3095+
}
3096+
template <class K>
3097+
ABSL_ATTRIBUTE_ALWAYS_INLINE size_t hash_of(const K& key) const {
30983098
return HashElement<hasher>{hash_ref()}(key);
30993099
}
3100-
size_t hash_of(slot_type* slot) const {
3100+
ABSL_ATTRIBUTE_ALWAYS_INLINE size_t hash_of(slot_type* slot) const {
31013101
return PolicyTraits::apply(HashElement<hasher>{hash_ref()},
31023102
PolicyTraits::element(slot));
31033103
}
31043104

31053105
// Casting directly from e.g. char* to slot_type* can cause compilation errors
31063106
// on objective-C. This function converts to void* first, avoiding the issue.
3107-
static slot_type* to_slot(void* buf) { return static_cast<slot_type*>(buf); }
3107+
static ABSL_ATTRIBUTE_ALWAYS_INLINE slot_type* to_slot(void* buf) {
3108+
return static_cast<slot_type*>(buf);
3109+
}
31083110

31093111
// Requires that lhs does not have a full SOO slot.
31103112
static void move_common(bool rhs_is_full_soo, CharAlloc& rhs_alloc,
@@ -3220,8 +3222,7 @@ class raw_hash_set {
32203222
return {single_iterator(), true};
32213223
}
32223224
soo_slot_ctrl = ctrl_t::kEmpty;
3223-
} else if (PolicyTraits::apply(EqualElement<K, key_equal>{key, eq_ref()},
3224-
PolicyTraits::element(single_slot()))) {
3225+
} else if (equal_to(key, single_slot())) {
32253226
return {single_iterator(), false};
32263227
} else {
32273228
soo_slot_ctrl = static_cast<ctrl_t>(H2(hash_of(single_slot())));
@@ -3243,8 +3244,7 @@ class raw_hash_set {
32433244
return find_or_prepare_insert_soo(key);
32443245
}
32453246
if (!empty()) {
3246-
if (PolicyTraits::apply(EqualElement<K, key_equal>{key, eq_ref()},
3247-
PolicyTraits::element(single_slot()))) {
3247+
if (equal_to(key, single_slot())) {
32483248
return {single_iterator(), false};
32493249
}
32503250
}
@@ -3268,9 +3268,7 @@ class raw_hash_set {
32683268
#endif
32693269
Group g{ctrl + seq.offset()};
32703270
for (uint32_t i : g.Match(h2)) {
3271-
if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
3272-
EqualElement<K, key_equal>{key, eq_ref()},
3273-
PolicyTraits::element(slot_array() + seq.offset(i)))))
3271+
if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i))))
32743272
return {iterator_at(seq.offset(i)), false};
32753273
}
32763274
auto mask_empty = g.MaskEmpty();
@@ -3345,16 +3343,11 @@ class raw_hash_set {
33453343

33463344
const size_t hash_of_arg = hash_of(key);
33473345
const auto assert_consistent = [&](const ctrl_t*, void* slot) {
3348-
const value_type& element =
3349-
PolicyTraits::element(static_cast<slot_type*>(slot));
3350-
const bool is_key_equal = PolicyTraits::apply(
3351-
EqualElement<K, key_equal>{key, eq_ref()}, element);
3346+
const bool is_key_equal = equal_to(key, to_slot(slot));
33523347
if (!is_key_equal) return;
33533348

3354-
const size_t hash_of_slot =
3355-
PolicyTraits::apply(HashElement<hasher>{hash_ref()}, element);
33563349
ABSL_ATTRIBUTE_UNUSED const bool is_hash_equal =
3357-
hash_of_arg == hash_of_slot;
3350+
hash_of_arg == hash_of(to_slot(slot));
33583351
assert((!is_key_equal || is_hash_equal) &&
33593352
"eq(k1, k2) must imply that hash(k1) == hash(k2). "
33603353
"hash/eq functors are inconsistent.");
@@ -3715,10 +3708,7 @@ struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
37153708
while (true) {
37163709
container_internal::Group g{ctrl + seq.offset()};
37173710
for (uint32_t i : g.Match(h2)) {
3718-
if (Traits::apply(
3719-
EqualElement<typename Set::key_type, typename Set::key_equal>{
3720-
key, set.eq_ref()},
3721-
Traits::element(set.slot_array() + seq.offset(i))))
3711+
if (set.equal_to(key, set.slot_array() + seq.offset(i)))
37223712
return num_probes;
37233713
++num_probes;
37243714
}

0 commit comments

Comments
 (0)