aboutsummaryrefslogtreecommitdiff
path: root/absl/container
diff options
context:
space:
mode:
authorVitaly Goldshteyn <goldvitaly@google.com>2024-02-22 17:06:14 -0800
committerCopybara-Service <copybara-worker@google.com>2024-02-22 17:06:59 -0800
commitd87dc03cee90a0cac2dbf254217b346ca693bb83 (patch)
tree7a545945f18f9cc0d1e006a74acc81a779254263 /absl/container
parent0e72e54fa6beabe188f44d6d1ed82010600359f5 (diff)
downloadabseil-d87dc03cee90a0cac2dbf254217b346ca693bb83.tar.gz
abseil-d87dc03cee90a0cac2dbf254217b346ca693bb83.tar.bz2
abseil-d87dc03cee90a0cac2dbf254217b346ca693bb83.zip
Optimize `prepare_insert`, when resize happens. It removes single unnecessary probing before resize that is beneficial for small tables the most.
PiperOrigin-RevId: 609547787 Change-Id: If6584919b4c93945ea078b1c1a9f57b355dce924
Diffstat (limited to 'absl/container')
-rw-r--r--absl/container/internal/raw_hash_set.h24
1 files changed, 13 insertions, 11 deletions
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h
index e07d5460..7b33de63 100644
--- a/absl/container/internal/raw_hash_set.h
+++ b/absl/container/internal/raw_hash_set.h
@@ -1623,8 +1623,10 @@ class HashSetResizeHelper {
old_capacity_(c.capacity()),
had_infoz_(c.has_infoz()) {}
- // Optimized for small groups version of `find_first_non_full` applicable
- // only right after calling `raw_hash_set::resize`.
+ // Optimized for small groups version of `find_first_non_full`.
+ // Beneficial only right after calling `raw_hash_set::resize`.
+ // It is safe to call in case capacity is big or was not changed, but there
+ // will be no performance benefit.
// It has implicit assumption that `resize` will call
// `GrowSizeIntoSingleGroup*` in case `IsGrowingIntoSingleGroupApplicable`.
// Falls back to `find_first_non_full` in case of big groups, so it is
@@ -3244,21 +3246,21 @@ class raw_hash_set {
const size_t cap = capacity();
resize(growth_left() > 0 ? cap : NextCapacity(cap));
}
- auto target = find_first_non_full(common(), hash);
- if (!rehash_for_bug_detection &&
- ABSL_PREDICT_FALSE(growth_left() == 0 &&
- !IsDeleted(control()[target.offset]))) {
- size_t old_capacity = capacity();
+ FindInfo target;
+ if (!rehash_for_bug_detection && ABSL_PREDICT_FALSE(growth_left() == 0)) {
+ const size_t old_capacity = capacity();
rehash_and_grow_if_necessary();
- // NOTE: It is safe to use `FindFirstNonFullAfterResize`.
- // `FindFirstNonFullAfterResize` must be called right after resize.
+ // NOTE: It is safe to use `FindFirstNonFullAfterResize` after
+ // `rehash_and_grow_if_necessary`, whether capacity changes or not.
// `rehash_and_grow_if_necessary` may *not* call `resize`
// and perform `drop_deletes_without_resize` instead. But this
- // could happen only on big tables.
+ // could happen only on big tables and will not change capacity.
// For big tables `FindFirstNonFullAfterResize` will always
- // fallback to normal `find_first_non_full`, so it is safe to use it.
+ // fallback to normal `find_first_non_full`.
target = HashSetResizeHelper::FindFirstNonFullAfterResize(
common(), old_capacity, hash);
+ } else {
+ target = find_first_non_full(common(), hash);
}
common().increment_size();
set_growth_left(growth_left() - IsEmpty(control()[target.offset]));