Skip to content

Commit f2f4fa2

Browse files
ezbrcopybara-github
authored andcommitted
Refactor find_or_prepare_insert_large to use a single return statement using a lambda.
This way, we deduplicate the generated code for iterator/pair construction. PiperOrigin-RevId: 802249759 Change-Id: If30a6d01ef4ae3ce899807b42dfc3d0da9b03536
1 parent f040e96 commit f2f4fa2

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

absl/container/internal/raw_hash_set.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,31 +3184,41 @@ class raw_hash_set {
31843184
auto seq = probe(common(), hash);
31853185
const h2_t h2 = H2(hash);
31863186
const ctrl_t* ctrl = control();
3187-
while (true) {
3187+
size_t index;
3188+
bool inserted;
3189+
// We use a lambda function to be able to exit from the nested loop without
3190+
// duplicating generated code for the return statement (e.g. iterator_at).
3191+
[&] {
3192+
while (true) {
31883193
#ifndef ABSL_HAVE_MEMORY_SANITIZER
3189-
absl::PrefetchToLocalCache(slot_array() + seq.offset());
3194+
absl::PrefetchToLocalCache(slot_array() + seq.offset());
31903195
#endif
3191-
Group g{ctrl + seq.offset()};
3192-
for (uint32_t i : g.Match(h2)) {
3193-
if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i))))
3194-
return {iterator_at(seq.offset(i)), false};
3195-
}
3196-
auto mask_empty = g.MaskEmpty();
3197-
if (ABSL_PREDICT_TRUE(mask_empty)) {
3198-
size_t target = seq.offset(mask_empty.LowestBitSet());
3199-
size_t index =
3200-
SwisstableGenerationsEnabled()
3201-
? PrepareInsertLargeGenerationsEnabled(
3202-
common(), GetPolicyFunctions(), hash,
3203-
FindInfo{target, seq.index()},
3204-
HashKey<hasher, K, kIsDefaultHash>{hash_ref(), key})
3205-
: PrepareInsertLarge(common(), GetPolicyFunctions(), hash,
3206-
FindInfo{target, seq.index()});
3207-
return {iterator_at(index), true};
3196+
Group g{ctrl + seq.offset()};
3197+
for (uint32_t i : g.Match(h2)) {
3198+
if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i)))) {
3199+
index = seq.offset(i);
3200+
inserted = false;
3201+
return;
3202+
}
3203+
}
3204+
auto mask_empty = g.MaskEmpty();
3205+
if (ABSL_PREDICT_TRUE(mask_empty)) {
3206+
size_t target = seq.offset(mask_empty.LowestBitSet());
3207+
index = SwisstableGenerationsEnabled()
3208+
? PrepareInsertLargeGenerationsEnabled(
3209+
common(), GetPolicyFunctions(), hash,
3210+
FindInfo{target, seq.index()},
3211+
HashKey<hasher, K, kIsDefaultHash>{hash_ref(), key})
3212+
: PrepareInsertLarge(common(), GetPolicyFunctions(), hash,
3213+
FindInfo{target, seq.index()});
3214+
inserted = true;
3215+
return;
3216+
}
3217+
seq.next();
3218+
ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity() && "full table!");
32083219
}
3209-
seq.next();
3210-
ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity() && "full table!");
3211-
}
3220+
}();
3221+
return {iterator_at(index), inserted};
32123222
}
32133223

32143224
protected:

0 commit comments

Comments
 (0)