Add BtreeDtorPolicy and move btree_node::clear_and_delete to internal/btree.cc. Add a 16-byte constexpr BtreeDtorPolicy descriptor shared across containers with identical slot layout and trivial-destruction properties, and implement ClearAndDeleteSubtree and BtreeDestroy<FieldType> in internal/btree.cc instantiated for uint8_t and uint16_t. PiperOrigin-RevId: 1022151776
diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index 4404d08..2c6f8f3 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc
@@ -436,6 +436,11 @@ return Btree::params_type::template destroy_is_trivial< typename Btree::allocator_type>(); } + + template <typename Btree> + constexpr static const BtreeDtorPolicy* DtorPolicy() { + return &btree<typename Btree::params_type>::GetDtorPolicy(); + } }; namespace { @@ -504,6 +509,23 @@ EXPECT_FALSE((IsLinear<std::string, std::less<std::string>>())); } +TEST(Btree, DtorPolicySharing) { + // Matching slot layouts share a policy; trivial types omit the destructor. + EXPECT_TRUE(BtreeNodePeer::DtorPolicy<absl::btree_set<int64_t>>() == + (BtreeNodePeer::DtorPolicy< + absl::btree_multiset<int64_t, std::greater<int64_t>>>())); + EXPECT_TRUE(BtreeNodePeer::DtorPolicy<absl::btree_set<int64_t>>() == + BtreeNodePeer::DtorPolicy<absl::btree_set<double>>()); + EXPECT_TRUE((BtreeNodePeer::DtorPolicy<absl::btree_map<int32_t, int32_t>>() == + BtreeNodePeer::DtorPolicy<absl::btree_map<uint32_t, float>>())); + EXPECT_TRUE( + BtreeNodePeer::DtorPolicy<absl::btree_set<int64_t>>()->destroy_n == + nullptr); + EXPECT_TRUE( + BtreeNodePeer::DtorPolicy<absl::btree_set<std::string>>()->destroy_n != + nullptr); +} + TEST(Btree, BtreeMapCanHoldMoveOnlyTypes) { EXPECT_TRUE(BtreeNodePeer::DestroyIsTrivial<absl::btree_set<int>>()); EXPECT_FALSE(BtreeNodePeer::DestroyIsTrivial<absl::btree_set<std::string>>());
diff --git a/absl/container/internal/btree.cc b/absl/container/internal/btree.cc index 6faa3b7..3183575 100644 --- a/absl/container/internal/btree.cc +++ b/absl/container/internal/btree.cc
@@ -14,10 +14,15 @@ #include "absl/container/internal/btree.h" +#include <cassert> +#include <cstddef> #include <cstdint> +#include <memory> #include "absl/base/attributes.h" #include "absl/base/config.h" +#include "absl/base/optimization.h" +#include "absl/container/internal/container_memory.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -26,6 +31,158 @@ ABSL_CONST_INIT ABSL_DLL const BtreeEmptyNode<uint8_t> kBtreeEmptyNode8{}; ABSL_CONST_INIT ABSL_DLL const BtreeEmptyNode<uint16_t> kBtreeEmptyNode16{}; +namespace { + +// Returns the aligned byte size of a leaf node for the given slot count. +inline size_t LeafSize(const BtreeDtorPolicy& policy, size_t slot_count) { + return policy.LeafSize(slot_count); +} + +// Returns the byte size of an internal node. +inline size_t InternalSize(size_t children_offset, size_t node_slots) { + return children_offset + (node_slots + 1) * sizeof(void*); +} + +// Returns a pointer to the slot at the given index within node. +inline void* NodeSlot(void* node, size_t slots_offset, size_t slot_size, + size_t i) { + return static_cast<char*>(node) + slots_offset + i * slot_size; +} + +// Returns the slot capacity of a leaf or internal node. +template <typename FieldType> +FieldType NodeMaxCount(const btree_node_base<FieldType>* node, + uint16_t node_slots) { + return node->max_count(static_cast<FieldType>(node_slots)); +} + +// Null callback selects default pointer-aligned node deletion. +inline void DeallocateNodeWithFn(void* node, size_t size, + BtreeDeallocFn dealloc, void* alloc) { + SanitizerUnpoisonMemoryRegion(node, size); + if (ABSL_PREDICT_TRUE(dealloc == nullptr)) { + std::allocator<char> default_alloc; + Deallocate<alignof(void*)>(&default_alloc, node, size); + return; + } + dealloc(alloc, node, size); +} + +// Destroys all elements and frees every node in the subtree. +template <typename FieldType> +void ClearAndDeleteSubtree(btree_node_base<FieldType>* node, + const BtreeDtorPolicy& policy, + BtreeDeallocFn dealloc, void* alloc) { + assert(node->count() > 0); + const size_t slot_size = policy.slot_size; + const size_t slots_offset = policy.slots_offset; + const uint16_t node_slots = policy.node_slots; + const BtreeDestroyNFn destroy_n = policy.destroy_n; + + const auto destroy_node_slots = [&](btree_node_base<FieldType>* n) { + n->next_generation(); + const size_t count = n->count(); + if (count == 0) return; + void* slots = NodeSlot(n, slots_offset, slot_size, n->start()); + if (destroy_n != nullptr) { + destroy_n(alloc, slots, count); + } + SanitizerPoisonMemoryRegion(slots, count * slot_size); + }; + + if (node->is_leaf()) { + destroy_node_slots(node); + DeallocateNodeWithFn(node, LeafSize(policy, NodeMaxCount(node, node_slots)), + dealloc, alloc); + return; + } + + const size_t coff = policy.ComputeChildrenOffset(); + const size_t internal_size = InternalSize(coff, node_slots); + + btree_node_base<FieldType>* delete_root_parent = node->parent(); + + // Navigate to the leftmost leaf under node, and then delete upwards. + while (node->is_internal()) node = node->start_child(coff); + // When generations are enabled, we delete the leftmost leaf last in case it's + // the parent of the root and we need to check whether it's a leaf before we + // can update the root's generation. + btree_node_base<FieldType>* leftmost_leaf = node; + + // Use `size_t` because `pos` needs to be able to hold `kNodeSlots+1`, + // which isn't guaranteed to be a valid `FieldType`. + size_t pos = node->position(); + btree_node_base<FieldType>* parent = node->parent(); + for (;;) { + // In each iteration of the next loop, we delete one leaf node and go right. + assert(pos <= parent->finish()); + do { + node = parent->child(coff, pos); + if (node->is_internal()) { + // Navigate to the leftmost leaf under node. + while (node->is_internal()) node = node->start_child(coff); + pos = node->position(); + parent = node->parent(); + } + destroy_node_slots(node); + if (!BtreeGenerationsEnabled() || leftmost_leaf != node) { + DeallocateNodeWithFn(node, + LeafSize(policy, NodeMaxCount(node, node_slots)), + dealloc, alloc); + } + ++pos; + } while (pos <= parent->finish()); + + // Once we've deleted all children of parent, delete parent and go up/right. + assert(pos > parent->finish()); + do { + node = parent; + pos = node->position(); + parent = node->parent(); + destroy_node_slots(node); + DeallocateNodeWithFn(node, internal_size, dealloc, alloc); + if (parent == delete_root_parent) { + if constexpr (BtreeGenerationsEnabled()) { + DeallocateNodeWithFn( + leftmost_leaf, + LeafSize(policy, NodeMaxCount(leftmost_leaf, node_slots)), + dealloc, alloc); + } + return; + } + ++pos; + } while (pos > parent->finish()); + } +} + +} // namespace + +// Destroys all elements and frees every node using the default deallocator. +template <typename FieldType> +void BtreeDestroy(btree_node_base<FieldType>* root, + const BtreeDtorPolicy& policy) { + ClearAndDeleteSubtree(root, policy, nullptr, nullptr); +} + +// Destroys all elements and frees every node using a custom deallocator. +template <typename FieldType> +void BtreeDestroy(btree_node_base<FieldType>* root, + const BtreeDtorPolicy& policy, BtreeDeallocFn dealloc, + void* alloc) { + ClearAndDeleteSubtree(root, policy, dealloc, alloc); +} + +template void BtreeDestroy<uint8_t>(btree_node_base<uint8_t>*, + const BtreeDtorPolicy&); +template void BtreeDestroy<uint16_t>(btree_node_base<uint16_t>*, + const BtreeDtorPolicy&); +template void BtreeDestroy<uint8_t>(btree_node_base<uint8_t>*, + const BtreeDtorPolicy&, BtreeDeallocFn, + void*); +template void BtreeDestroy<uint16_t>(btree_node_base<uint16_t>*, + const BtreeDtorPolicy&, BtreeDeallocFn, + void*); + } // namespace container_internal ABSL_NAMESPACE_END } // namespace absl
diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index b169e6e..d518b2c 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h
@@ -54,6 +54,7 @@ #include <iterator> #include <limits> #include <memory> +#include <new> #include <string> #include <type_traits> #include <utility> @@ -1521,6 +1522,97 @@ bool empty() const { return size == 0; } }; +using BtreeDestroyNFn = void (*)(void* alloc, void* slots, size_t count); +using BtreeDeallocFn = void (*)(void* alloc, void* node, size_t size); + +// Compact 16-byte policy for destroying or clearing a B-tree, kept separate +// from BtreePolicy so translation units that only destroy or clear a B-tree do +// not instantiate transfer or allocation callbacks. +struct BtreeDtorPolicy { + uint32_t slot_size; + uint16_t node_slots; + uint16_t slots_offset; + BtreeDestroyNFn destroy_n; + + static constexpr size_t LeafSize(uint16_t slots_offset, size_t slot_count, + uint32_t slot_size) { + const size_t raw = slots_offset + slot_count * slot_size; + return (raw + alignof(void*) - 1) & ~(alignof(void*) - 1); + } + + constexpr size_t LeafSize(size_t slot_count) const { + return LeafSize(slots_offset, slot_count, slot_size); + } + + static constexpr uint32_t ComputeChildrenOffset(uint16_t slots_offset, + uint16_t node_slots, + uint32_t slot_size) { + return static_cast<uint32_t>(LeafSize(slots_offset, node_slots, slot_size)); + } + + constexpr uint32_t ComputeChildrenOffset() const { + return ComputeChildrenOffset(slots_offset, node_slots, slot_size); + } +}; + +template <uint32_t kSlotSize, uint16_t kNodeSlots, uint16_t kSlotsOffset, + BtreeDestroyNFn kDestroyN> +inline constexpr BtreeDtorPolicy kBtreeDtor = { + kSlotSize, + kNodeSlots, + kSlotsOffset, + kDestroyN, +}; + +// Invokes fn with a typed allocator pointer or a temporary default allocator. +template <typename Alloc, typename Fn> +decltype(auto) WithBtreeAlloc(void* alloc, Fn&& fn) { + if constexpr (std::is_same_v<Alloc, std::allocator<char>>) { + (void)alloc; + std::allocator<char> a; + return fn(&a); + } else { + return fn(static_cast<Alloc*>(alloc)); + } +} + +template <size_t Alignment, typename Alloc> +void BtreeDeallocate(void* alloc, void* node, size_t size) { + WithBtreeAlloc<Alloc>(alloc, + [&](auto* a) { Deallocate<Alignment>(a, node, size); }); +} + +// Type-erased callback destroying a contiguous run of initialized slots. +template <typename SlotPolicy, typename Alloc> +void BtreeDestroyN(void* alloc, void* slots, size_t count) { + using Traits = common_policy_traits<SlotPolicy>; + auto* s = static_cast<typename SlotPolicy::slot_type*>(slots); + WithBtreeAlloc<Alloc>(alloc, [&](auto* a) { + for (size_t i = 0; i < count; ++i) { + Traits::destroy(a, std::launder(s + i)); + } + }); +} + +// Destroys all elements and frees every node in the subtree. +template <typename FieldType> +void BtreeDestroy(btree_node_base<FieldType>* root, + const BtreeDtorPolicy& policy); +template <typename FieldType> +void BtreeDestroy(btree_node_base<FieldType>* root, + const BtreeDtorPolicy& policy, BtreeDeallocFn dealloc, + void* alloc); +extern template void BtreeDestroy<uint8_t>(btree_node_base<uint8_t>*, + const BtreeDtorPolicy&); +extern template void BtreeDestroy<uint16_t>(btree_node_base<uint16_t>*, + const BtreeDtorPolicy&); +extern template void BtreeDestroy<uint8_t>(btree_node_base<uint8_t>*, + const BtreeDtorPolicy&, + BtreeDeallocFn, void*); +extern template void BtreeDestroy<uint16_t>(btree_node_base<uint16_t>*, + const BtreeDtorPolicy&, + BtreeDeallocFn, void*); + template <typename Params> class btree { using node_type = btree_node<Params>; @@ -1618,7 +1710,7 @@ // is complete. static_assert(static_assert_validation(), "This call must be elided."); if (!empty()) { - node_type::clear_and_delete(root(), mutable_allocator()); + destroy_subtree(root()); } } @@ -1843,7 +1935,46 @@ allocator_type get_allocator() const { return allocator(); } private: + friend class btree_node<Params>; friend struct btree_access; + friend class BtreeNodePeer; + + // Erase element type from default allocators to share policies and helpers. + static constexpr bool kIsDefaultAlloc = + std::is_same_v<allocator_type, std::allocator<value_type>>; + static constexpr bool kUsesDefaultNodeAlloc = + kIsDefaultAlloc && node_type::Alignment() <= alignof(void*); + using CanonicalAlloc = + std::conditional_t<kIsDefaultAlloc, std::allocator<char>, allocator_type>; + using SlotPolicy = typename Params::slot_policy; + + static constexpr BtreeDestroyNFn get_destroy_n_fn() { + if constexpr (params_type::template destroy_is_trivial<CanonicalAlloc>()) { + return nullptr; + } else { + return &BtreeDestroyN<SlotPolicy, CanonicalAlloc>; + } + } + + static constexpr BtreeDeallocFn get_dealloc_fn() { + return &BtreeDeallocate<node_type::Alignment(), CanonicalAlloc>; + } + + static constexpr const BtreeDtorPolicy& GetDtorPolicy() { + constexpr field_type kSlots = field_type{kNodeSlots}; + if constexpr (kNodeSlots <= (std::numeric_limits<field_type>::max)()) { + static_assert(BtreeDtorPolicy::ComputeChildrenOffset( + node_type::kSlotsOffset, kSlots, sizeof(slot_type)) == + node_type::kChildrenOffset); + } + return kBtreeDtor<sizeof(slot_type), kSlots, + static_cast<uint16_t>(node_type::kSlotsOffset), + get_destroy_n_fn()>; + } + + void destroy_subtree(node_type* node) { + node_type::clear_and_delete(node, mutable_allocator()); + } // Internal accessor routines. node_type* root() { return static_cast<node_type*>(common_.root); } @@ -2022,7 +2153,7 @@ const field_type to_erase, allocator_type* alloc) { value_destroy_n(i, to_erase, alloc); - if (is_internal()) { + if (ABSL_PREDICT_FALSE(is_internal())) { // Delete all children between begin and end. for (field_type j = 0; j < to_erase; ++j) { clear_and_delete(child(i + j + 1), alloc); @@ -2210,65 +2341,12 @@ template <typename P> void btree_node<P>::clear_and_delete(btree_node* node, allocator_type* alloc) { - assert(node->count() > 0); - if (node->is_leaf()) { - node->value_destroy_n(node->start(), node->count(), alloc); - deallocate(LeafSize(node->max_count()), node, alloc); - return; - } - - // The parent of the root of the subtree we are deleting. - btree_node* delete_root_parent = node->parent(); - - // Navigate to the leftmost leaf under node, and then delete upwards. - while (node->is_internal()) node = node->start_child(); -#ifdef ABSL_BTREE_ENABLE_GENERATIONS - // When generations are enabled, we delete the leftmost leaf last in case it's - // the parent of the root and we need to check whether it's a leaf before we - // can update the root's generation. - // TODO(ezb): if we change btree_node::is_root to check a bool inside the node - // instead of checking whether the parent is a leaf, we can remove this logic. - btree_node* leftmost_leaf = node; -#endif - // Use `size_type` because `pos` needs to be able to hold `kNodeSlots+1`, - // which isn't guaranteed to be a valid `field_type`. - size_type pos = node->position(); - btree_node* parent = node->parent(); - for (;;) { - // In each iteration of the next loop, we delete one leaf node and go right. - assert(pos <= parent->finish()); - do { - node = parent->child(static_cast<field_type>(pos)); - if (node->is_internal()) { - // Navigate to the leftmost leaf under node. - while (node->is_internal()) node = node->start_child(); - pos = node->position(); - parent = node->parent(); - } - node->value_destroy_n(node->start(), node->count(), alloc); -#ifdef ABSL_BTREE_ENABLE_GENERATIONS - if (leftmost_leaf != node) -#endif - deallocate(LeafSize(node->max_count()), node, alloc); - ++pos; - } while (pos <= parent->finish()); - - // Once we've deleted all children of parent, delete parent and go up/right. - assert(pos > parent->finish()); - do { - node = parent; - pos = node->position(); - parent = node->parent(); - node->value_destroy_n(node->start(), node->count(), alloc); - deallocate(InternalSize(), node, alloc); - if (parent == delete_root_parent) { -#ifdef ABSL_BTREE_ENABLE_GENERATIONS - deallocate(LeafSize(leftmost_leaf->max_count()), leftmost_leaf, alloc); -#endif - return; - } - ++pos; - } while (pos > parent->finish()); + if constexpr (btree<P>::kUsesDefaultNodeAlloc) { + (void)alloc; + BtreeDestroy<field_type>(node, btree<P>::GetDtorPolicy()); + } else { + BtreeDestroy<field_type>(node, btree<P>::GetDtorPolicy(), + btree<P>::get_dealloc_fn(), alloc); } } @@ -2817,7 +2895,7 @@ return {0, begin}; } - if (static_cast<size_type>(count) == size()) { + if (ABSL_PREDICT_FALSE(static_cast<size_type>(count) == size())) { clear(); return {count, this->end()}; } @@ -2855,7 +2933,7 @@ template <typename P> void btree<P>::clear() { if (!empty()) { - node_type::clear_and_delete(root(), mutable_allocator()); + destroy_subtree(root()); } mutable_root() = mutable_rightmost() = EmptyNode(); mutable_size() = 0;