aboutsummaryrefslogtreecommitdiff
path: root/absl/container/internal/raw_hash_map.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2024-01-12 12:31:42 -0800
committerCopybara-Service <copybara-worker@google.com>2024-01-12 12:32:40 -0800
commit27134f25b11e3119a2814988c3979fdc033e54e1 (patch)
tree89e5c35470166d84ac1e67025444954bb0cbbdd4 /absl/container/internal/raw_hash_map.h
parent41a2a2555588253dce0e10db4c545463c4a64731 (diff)
downloadabseil-27134f25b11e3119a2814988c3979fdc033e54e1.tar.gz
abseil-27134f25b11e3119a2814988c3979fdc033e54e1.tar.bz2
abseil-27134f25b11e3119a2814988c3979fdc033e54e1.zip
Speed up `raw_hash_map::[]` with ABSL hardening enabled by unchecking dereference of iterator returned by `try_emplace`.
PiperOrigin-RevId: 597920257 Change-Id: I1b2e8f10a2f1efa763a6f0760294beafdb6fd9c0
Diffstat (limited to 'absl/container/internal/raw_hash_map.h')
-rw-r--r--absl/container/internal/raw_hash_map.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/absl/container/internal/raw_hash_map.h b/absl/container/internal/raw_hash_map.h
index c1d4d4bc..97182bc7 100644
--- a/absl/container/internal/raw_hash_map.h
+++ b/absl/container/internal/raw_hash_map.h
@@ -177,13 +177,20 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
template <class K = key_type, class P = Policy, K* = nullptr>
MappedReference<P> operator[](key_arg<K>&& key)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
- return Policy::value(&*try_emplace(std::forward<K>(key)).first);
+ // It is safe to use unchecked_deref here because try_emplace
+ // will always return an iterator pointing to a valid item in the table,
+ // since it inserts if nothing is found for the given key.
+ return Policy::value(
+ &this->unchecked_deref(try_emplace(std::forward<K>(key)).first));
}
template <class K = key_type, class P = Policy>
MappedReference<P> operator[](const key_arg<K>& key)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
- return Policy::value(&*try_emplace(key).first);
+ // It is safe to use unchecked_deref here because try_emplace
+ // will always return an iterator pointing to a valid item in the table,
+ // since it inserts if nothing is found for the given key.
+ return Policy::value(&this->unchecked_deref(try_emplace(key).first));
}
private: