Skip to content

Commit

Permalink
Simplify clear() and don't iterate to destroy values with trivial d…
Browse files Browse the repository at this point in the history
…estructor.
  • Loading branch information
greg7mdp committed Sep 2, 2023
1 parent 7d30f71 commit d464cbc
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions include/gtl/phmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,22 +1529,15 @@ class raw_hash_set

GTL_ATTRIBUTE_REINITIALIZES void clear()
{
// Iterating over this container is O(bucket_count()). When bucket_count()
// is much greater than size(), iteration becomes prohibitively expensive.
// For clear() it is more important to reuse the allocated array when the
// container is small because allocation takes comparatively long time
// compared to destruction of the elements of the container. So we pick the
// largest bucket_count() threshold for which iteration is still fast and
// past that we simply deallocate the array.
// -----------------------------------------------------------------------
if (empty())
return;
if (capacity_ > 127) {
destroy_slots();
} else if (capacity_) {
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
if (capacity_) {
if constexpr (!std::is_trivially_destructible<typename PolicyTraits::value_type>::value) {
// not trivially destructible... we need to iterate and destroy values one by one
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
}
}
}
size_ = 0;
Expand Down Expand Up @@ -2335,9 +2328,13 @@ class raw_hash_set
{
if (!capacity_)
return;
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);

if constexpr (!std::is_trivially_destructible<typename PolicyTraits::value_type>::value) {
// not trivially destructible... we need to iterate and destroy values one by one
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
}
}
}
auto layout = MakeLayout(capacity_);
Expand Down

0 comments on commit d464cbc

Please sign in to comment.