Commit 98156bb8 by Abseil Team Committed by Copybara-Service

Speed up `raw_hash_set::contains()` when ABSL hardening is enabled by removing…

Speed up `raw_hash_set::contains()` when ABSL hardening is enabled by removing the iterator invalidation check from the comparison that contains performs.

PiperOrigin-RevId: 595460301
Change-Id: I9a5d6c81385e38184f4848c58209adc5d32bb7be
parent 4038192a
...@@ -2016,6 +2016,12 @@ class raw_hash_set { ...@@ -2016,6 +2016,12 @@ class raw_hash_set {
union { union {
slot_type* slot_; slot_type* slot_;
}; };
// An equality check which skips ABSL Hardening iterator invalidation
// checks.
// Should be used when the lifetimes of the iterators are well-enough
// understood to prove that they cannot be invalid.
bool unchecked_equals(const iterator& b) { return ctrl_ == b.control(); }
}; };
class const_iterator { class const_iterator {
...@@ -2060,6 +2066,10 @@ class raw_hash_set { ...@@ -2060,6 +2066,10 @@ class raw_hash_set {
slot_type* slot() const { return inner_.slot(); } slot_type* slot() const { return inner_.slot(); }
iterator inner_; iterator inner_;
bool unchecked_equals(const const_iterator& b) {
return inner_.unchecked_equals(b.inner_);
}
}; };
using node_type = node_handle<Policy, hash_policy_traits<Policy>, Alloc>; using node_type = node_handle<Policy, hash_policy_traits<Policy>, Alloc>;
...@@ -2707,7 +2717,11 @@ class raw_hash_set { ...@@ -2707,7 +2717,11 @@ class raw_hash_set {
template <class K = key_type> template <class K = key_type>
bool contains(const key_arg<K>& key) const { bool contains(const key_arg<K>& key) const {
return find(key) != end(); // Here neither the iterator returned by `find()` nor `end()` can be invalid
// outside of potential thread-safety issues.
// `find()`'s return value is constructed, used, and then destructed
// all in this context.
return !find(key).unchecked_equals(end());
} }
template <class K = key_type> template <class K = key_type>
......
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