Skip to content

Commit b3b568d

Browse files
ezbrcopybara-github
authored andcommitted
Refactor to make it clear that H2 computation is not repeated in each iteration of the probe loop.
This is a no-op for generated code. PiperOrigin-RevId: 737024397 Change-Id: I81bdeee8d6a1b64b86d12747d9866d450c55b2a6
1 parent 5f28934 commit b3b568d

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

absl/container/internal/raw_hash_set.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ size_t DropDeletesWithoutResizeAndPrepareInsert(CommonFields& common,
320320
// If they do, we don't need to move the object as it falls already in the
321321
// best probe we can.
322322
const size_t probe_offset = probe(common, hash).offset();
323+
const h2_t h2 = H2(hash);
323324
const auto probe_index = [probe_offset, capacity](size_t pos) {
324325
return ((pos - probe_offset) & capacity) / Group::kWidth;
325326
};
326327

327328
// Element doesn't move.
328329
if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
329-
SetCtrlInLargeTable(common, i, H2(hash), slot_size);
330+
SetCtrlInLargeTable(common, i, h2, slot_size);
330331
continue;
331332
}
332333

@@ -335,14 +336,14 @@ size_t DropDeletesWithoutResizeAndPrepareInsert(CommonFields& common,
335336
// Transfer element to the empty spot.
336337
// SetCtrl poisons/unpoisons the slots so we have to call it at the
337338
// right time.
338-
SetCtrlInLargeTable(common, new_i, H2(hash), slot_size);
339+
SetCtrlInLargeTable(common, new_i, h2, slot_size);
339340
(*transfer)(set, new_slot_ptr, slot_ptr, 1);
340341
SetCtrlInLargeTable(common, i, ctrl_t::kEmpty, slot_size);
341342
// Initialize or change empty space id.
342343
tmp_space_id = i;
343344
} else {
344345
assert(IsDeleted(ctrl[new_i]));
345-
SetCtrlInLargeTable(common, new_i, H2(hash), slot_size);
346+
SetCtrlInLargeTable(common, new_i, h2, slot_size);
346347
// Until we are done rehashing, DELETED marks previously FULL slots.
347348

348349
if (tmp_space_id == kUnknownId) {

absl/container/internal/raw_hash_set.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,10 +3110,11 @@ class raw_hash_set {
31103110
iterator find_non_soo(const key_arg<K>& key, size_t hash) {
31113111
ABSL_SWISSTABLE_ASSERT(!is_soo());
31123112
auto seq = probe(common(), hash);
3113+
const h2_t h2 = H2(hash);
31133114
const ctrl_t* ctrl = control();
31143115
while (true) {
31153116
Group g{ctrl + seq.offset()};
3116-
for (uint32_t i : g.Match(H2(hash))) {
3117+
for (uint32_t i : g.Match(h2)) {
31173118
if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
31183119
EqualElement<K>{key, eq_ref()},
31193120
PolicyTraits::element(slot_array() + seq.offset(i)))))
@@ -3345,12 +3346,13 @@ class raw_hash_set {
33453346
std::pair<iterator, bool> find_or_prepare_insert_non_soo(const K& key) {
33463347
ABSL_SWISSTABLE_ASSERT(!is_soo());
33473348
prefetch_heap_block();
3348-
auto hash = hash_ref()(key);
3349+
const size_t hash = hash_ref()(key);
33493350
auto seq = probe(common(), hash);
3351+
const h2_t h2 = H2(hash);
33503352
const ctrl_t* ctrl = control();
33513353
while (true) {
33523354
Group g{ctrl + seq.offset()};
3353-
for (uint32_t i : g.Match(H2(hash))) {
3355+
for (uint32_t i : g.Match(h2)) {
33543356
if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
33553357
EqualElement<K>{key, eq_ref()},
33563358
PolicyTraits::element(slot_array() + seq.offset(i)))))
@@ -3740,12 +3742,13 @@ struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
37403742
const typename Set::key_type& key) {
37413743
if (set.is_soo()) return 0;
37423744
size_t num_probes = 0;
3743-
size_t hash = set.hash_ref()(key);
3745+
const size_t hash = set.hash_ref()(key);
37443746
auto seq = probe(set.common(), hash);
3747+
const h2_t h2 = H2(hash);
37453748
const ctrl_t* ctrl = set.control();
37463749
while (true) {
37473750
container_internal::Group g{ctrl + seq.offset()};
3748-
for (uint32_t i : g.Match(container_internal::H2(hash))) {
3751+
for (uint32_t i : g.Match(h2)) {
37493752
if (Traits::apply(
37503753
typename Set::template EqualElement<typename Set::key_type>{
37513754
key, set.eq_ref()},

0 commit comments

Comments
 (0)