Commit 3abfc337 authored by leszeks's avatar leszeks Committed by Commit bot

[base] Probe hashmap using indices rather than pointers

Using indices rather than pointers to probe the hashmap lets us
unconditionally mask the index to ensure it wraps around, rather than
branching on the pointer value. This produces slightly more optimal
code.

Review-Url: https://codereview.chromium.org/2488423003
Cr-Commit-Position: refs/heads/master@{#40967}
parent 4e32419b
...@@ -229,9 +229,8 @@ template <typename Key, typename Value, typename MatchFun, ...@@ -229,9 +229,8 @@ template <typename Key, typename Value, typename MatchFun,
class AllocationPolicy> class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Clear() { void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Clear() {
// Mark all entries as empty. // Mark all entries as empty.
const Entry* end = map_end(); for (size_t i = 0; i < capacity_; ++i) {
for (Entry* entry = map_; entry < end; entry++) { map_[i].clear();
entry->clear();
} }
occupancy_ = 0; occupancy_ = 0;
} }
...@@ -264,19 +263,15 @@ typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* ...@@ -264,19 +263,15 @@ typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Probe( TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Probe(
const Key& key, uint32_t hash) const { const Key& key, uint32_t hash) const {
DCHECK(base::bits::IsPowerOfTwo32(capacity_)); DCHECK(base::bits::IsPowerOfTwo32(capacity_));
Entry* entry = map_ + (hash & (capacity_ - 1)); size_t i = hash & (capacity_ - 1);
const Entry* end = map_end(); DCHECK(i < capacity_);
DCHECK(map_ <= entry && entry < end);
DCHECK(occupancy_ < capacity_); // Guarantees loop termination. DCHECK(occupancy_ < capacity_); // Guarantees loop termination.
while (entry->exists() && !match_(hash, entry->hash, key, entry->key)) { while (map_[i].exists() && !match_(hash, map_[i].hash, key, map_[i].key)) {
entry++; i = (i + 1) & (capacity_ - 1);
if (entry >= end) {
entry = map_;
}
} }
return entry; return &map_[i];
} }
template <typename Key, typename Value, typename MatchFun, template <typename Key, typename Value, typename MatchFun,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment