From 98156bb8e0337cc8c2e0480ebc14fee208a2f08c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 3 Jan 2024 11:41:38 -0800 Subject: 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 --- absl/container/internal/raw_hash_set.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'absl/container/internal/raw_hash_set.h') diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index b7295c84..9c65984d 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -2016,6 +2016,12 @@ class raw_hash_set { union { 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 { @@ -2060,6 +2066,10 @@ class raw_hash_set { slot_type* slot() const { return inner_.slot(); } iterator inner_; + + bool unchecked_equals(const const_iterator& b) { + return inner_.unchecked_equals(b.inner_); + } }; using node_type = node_handle, Alloc>; @@ -2707,7 +2717,11 @@ class raw_hash_set { template bool contains(const key_arg& 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 -- cgit v1.2.3