Add type-erased increment/decrement and begin/end helpers for B-trees.

- Share iterator increment/decrement fast paths on BtreeIteratorBase and begin/end accessors on BtreeCommonFields so upcoming type-erased lookup and mutation helpers can advance iterators and form end iterators without duplicating fast-path logic.

PiperOrigin-RevId: 1031808588
diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc
index 4404d08..eea7014 100644
--- a/absl/container/btree_test.cc
+++ b/absl/container/btree_test.cc
@@ -436,6 +436,17 @@
     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();
+  }
+  template <typename Btree1, typename Btree2>
+  constexpr static bool SearchOpsSame() {
+    return std::is_same_v<
+        typename btree_node<typename Btree1::params_type>::SearchOps,
+        typename btree_node<typename Btree2::params_type>::SearchOps>;
+  }
 };
 
 namespace {
@@ -504,6 +515,37 @@
   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, SearchOpsSharing) {
+  EXPECT_TRUE((BtreeNodePeer::SearchOpsSame<absl::btree_set<int64_t>,
+                                            absl::btree_multiset<int64_t>>()));
+  EXPECT_TRUE(
+      (BtreeNodePeer::SearchOpsSame<absl::btree_map<int64_t, int64_t>,
+                                    absl::btree_map<int64_t, double>>()));
+  EXPECT_TRUE(
+      (BtreeNodePeer::SearchOpsSame<absl::btree_map<int64_t, void*>,
+                                    absl::btree_multimap<int64_t, int64_t>>()));
+  EXPECT_TRUE((BtreeNodePeer::SearchOpsSame<
+               absl::btree_map<std::string, int64_t>,
+               absl::btree_map<std::string, std::unique_ptr<int>>>()));
+}
+
 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..ee4bcaa 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,299 @@
 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*);
+
+// Advances an iterator past the end of a leaf or into an internal node's child.
+template <typename FieldType>
+BtreeIteratorBase BtreeIteratorIncrementSlow(BtreeIteratorBase it,
+                                             size_t children_offset) {
+  auto* node = it.node->as<FieldType>();
+  int position = it.position;
+  if (node->is_leaf()) {
+    assert(position >= node->finish());
+    while (position == node->finish() && !node->is_root()) {
+      assert(node->parent()->child(children_offset, node->position()) == node);
+      position = node->position();
+      node = node->parent();
+    }
+    // TODO(ezb): assert we aren't incrementing end() instead of handling.
+    if (position == node->finish()) {
+      return it;
+    }
+  } else {
+    assert(position < node->finish());
+    node = node->child(children_offset, static_cast<size_t>(position + 1));
+    while (node->is_internal()) {
+      node = node->start_child(children_offset);
+    }
+    position = node->start();
+  }
+  return {node, position};
+}
+
+template BtreeIteratorBase BtreeIteratorIncrementSlow<uint8_t>(
+    BtreeIteratorBase, size_t);
+template BtreeIteratorBase BtreeIteratorIncrementSlow<uint16_t>(
+    BtreeIteratorBase, size_t);
+
+// Moves an iterator before a leaf start or into an internal node's child.
+template <typename FieldType>
+BtreeIteratorBase BtreeIteratorDecrementSlow(BtreeIteratorBase it,
+                                             size_t children_offset) {
+  auto* node = it.node->as<FieldType>();
+  int position = it.position;
+  if (node->is_leaf()) {
+    assert(position <= -1);
+    while (position < node->start() && !node->is_root()) {
+      assert(node->parent()->child(children_offset, node->position()) == node);
+      position = static_cast<int>(node->position()) - 1;
+      node = node->parent();
+    }
+    // TODO(ezb): assert we aren't decrementing begin() instead of handling.
+    if (position < node->start()) {
+      return it;
+    }
+  } else {
+    assert(position >= node->start());
+    node = node->child(children_offset, static_cast<size_t>(position));
+    while (node->is_internal()) {
+      node = node->child(children_offset, node->finish());
+    }
+    position = static_cast<int>(node->finish()) - 1;
+  }
+  return {node, position};
+}
+
+template BtreeIteratorBase BtreeIteratorDecrementSlow<uint8_t>(
+    BtreeIteratorBase, size_t);
+template BtreeIteratorBase BtreeIteratorDecrementSlow<uint16_t>(
+    BtreeIteratorBase, size_t);
+
+// Computes the number of elements from begin to end across multiple nodes.
+// Note: the implementation here is based on btree_node::clear_and_delete.
+template <typename FieldType>
+ptrdiff_t BtreeIteratorDistanceSlow(BtreeIteratorBase begin,
+                                    BtreeIteratorBase end,
+                                    size_t children_offset) {
+  const auto* node = begin.node->as<FieldType>();
+  const auto* end_node = end.node->as<FieldType>();
+  assert(node != end_node || !node->is_leaf() ||
+         begin.position != end.position);
+
+  // We need to compensate for double counting if begin.node is a leaf node.
+  ptrdiff_t count = node->is_leaf() ? -begin.position : 0;
+
+  // First navigate to the leftmost leaf node past begin.
+  if (node->is_internal()) {
+    ++count;
+    node =
+        node->child(children_offset, static_cast<size_t>(begin.position + 1));
+  }
+  while (node->is_internal()) node = node->start_child(children_offset);
+
+  // 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();
+  const btree_node_base<FieldType>* parent = node->parent();
+  const auto* children = parent->children(children_offset);
+  for (;;) {
+    // In each iteration of the next loop, we count one leaf node and go right.
+    assert(pos <= parent->finish());
+    for (;;) {
+      node = children[pos];
+      if (ABSL_PREDICT_FALSE(node->is_internal())) {
+        // Navigate to the leftmost leaf under node.
+        while (node->is_internal()) node = node->start_child(children_offset);
+        pos = node->position();
+        parent = node->parent();
+        children = parent->children(children_offset);
+      }
+      if (ABSL_PREDICT_FALSE(node == end_node)) return count + end.position;
+      count += node->count();
+      if (ABSL_PREDICT_FALSE(parent == end_node &&
+                             pos == static_cast<size_t>(end.position))) {
+        return count;
+      }
+      if (pos == parent->finish()) break;
+      // +1 is for the next internal node value.
+      ++count;
+      ++pos;
+    }
+
+    // Once we've counted all children of parent, go up/right.
+    do {
+      node = parent;
+      pos = node->position();
+      parent = node->parent();
+      if (ABSL_PREDICT_FALSE(parent == end_node &&
+                             pos == static_cast<size_t>(end.position))) {
+        return count;
+      }
+      ++pos;
+    } while (pos > parent->finish());
+    // +1 is for the internal node value separating the subtrees.
+    ++count;
+    children = parent->children(children_offset);
+  }
+}
+
+template ptrdiff_t BtreeIteratorDistanceSlow<uint8_t>(BtreeIteratorBase,
+                                                      BtreeIteratorBase,
+                                                      size_t);
+template ptrdiff_t BtreeIteratorDistanceSlow<uint16_t>(BtreeIteratorBase,
+                                                       BtreeIteratorBase,
+                                                       size_t);
+
 }  // namespace container_internal
 ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h
index b169e6e..06ab4d5 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>
@@ -634,10 +635,13 @@
     set_parent(parent()->parent());
   }
 
-  btree_node_base* child(size_t children_offset, size_t i) const {
+  btree_node_base* const* children(size_t children_offset) const {
     assert(is_internal());
     return reinterpret_cast<btree_node_base* const*>(this_char() +
-                                                     children_offset)[i];
+                                                     children_offset);
+  }
+  btree_node_base* child(size_t children_offset, size_t i) const {
+    return children(children_offset)[i];
   }
   btree_node_base* start_child(size_t children_offset) const {
     return child(children_offset, start());
@@ -667,6 +671,11 @@
   }
 };
 
+template <typename SlotKey, typename KeyCompare, typename FieldType,
+          size_t kSlotSize, size_t kSlotsOffset, size_t kChildrenOffset,
+          bool kUseLinearSearch>
+class BtreeOps;
+
 // A node in the btree holding. The same node type is used for both internal
 // and leaf nodes in the btree, though the nodes are allocated in such a way
 // that the children array is only valid in internal nodes.
@@ -909,123 +918,24 @@
     Base::init_child(kChildrenOffset, i, c);
   }
 
+  using SearchOps = BtreeOps<std::remove_const_t<key_type>, key_compare,
+                             field_type, sizeof(slot_type), kSlotsOffset,
+                             kChildrenOffset, use_linear_search::value>;
+
   // Returns the position of the first value whose key is not less than k.
   template <typename K>
   SearchResult<size_type, is_key_compare_to::value> lower_bound(
       const K& k, const key_compare& comp) const {
-    return use_linear_search::value ? linear_search(k, comp)
-                                    : binary_search(k, comp);
+    return SearchOps::template NodeSearch<
+        params_type::template can_have_multiple_equivalent_keys<K>()>(this, k,
+                                                                      comp);
   }
   // Returns the position of the first value whose key is greater than k.
   template <typename K>
   size_type upper_bound(const K& k, const key_compare& comp) const {
-    auto upper_compare = upper_bound_adapter<key_compare>(comp);
-    return use_linear_search::value ? linear_search(k, upper_compare).value
-                                    : binary_search(k, upper_compare).value;
-  }
-
-  template <typename K, typename Compare>
-  SearchResult<size_type, btree_is_key_compare_to<Compare, key_type>::value>
-  linear_search(const K& k, const Compare& comp) const {
-    return linear_search_impl(k, start(), finish(), comp,
-                              btree_is_key_compare_to<Compare, key_type>());
-  }
-
-  template <typename K, typename Compare>
-  SearchResult<size_type, btree_is_key_compare_to<Compare, key_type>::value>
-  binary_search(const K& k, const Compare& comp) const {
-    return binary_search_impl(k, start(), finish(), comp,
-                              btree_is_key_compare_to<Compare, key_type>());
-  }
-
-  // Returns the position of the first value whose key is not less than k using
-  // linear search performed using plain compare.
-  template <typename K, typename Compare>
-  SearchResult<size_type, false> linear_search_impl(
-      const K& k, size_type s, const size_type e, const Compare& comp,
-      std::false_type /* IsCompareTo */) const {
-    while (s < e) {
-      if (!comp(key(s), k)) {
-        break;
-      }
-      ++s;
-    }
-    return SearchResult<size_type, false>{s};
-  }
-
-  // Returns the position of the first value whose key is not less than k using
-  // linear search performed using compare-to.
-  template <typename K, typename Compare>
-  SearchResult<size_type, true> linear_search_impl(
-      const K& k, size_type s, const size_type e, const Compare& comp,
-      std::true_type /* IsCompareTo */) const {
-    while (s < e) {
-      const absl::weak_ordering c = comp(key(s), k);
-      if (c == 0) {
-        return {s, MatchKind::kEq};
-      } else if (c > 0) {
-        break;
-      }
-      ++s;
-    }
-    return {s, MatchKind::kNe};
-  }
-
-  // Returns the position of the first value whose key is not less than k using
-  // binary search performed using plain compare.
-  template <typename K, typename Compare>
-  SearchResult<size_type, false> binary_search_impl(
-      const K& k, size_type s, size_type e, const Compare& comp,
-      std::false_type /* IsCompareTo */) const {
-    while (s != e) {
-      const size_type mid = (s + e) >> 1;
-      if (comp(key(mid), k)) {
-        s = mid + 1;
-      } else {
-        e = mid;
-      }
-    }
-    return SearchResult<size_type, false>{s};
-  }
-
-  // Returns the position of the first value whose key is not less than k using
-  // binary search performed using compare-to.
-  template <typename K, typename CompareTo>
-  SearchResult<size_type, true> binary_search_impl(
-      const K& k, size_type s, size_type e, const CompareTo& comp,
-      std::true_type /* IsCompareTo */) const {
-    if (params_type::template can_have_multiple_equivalent_keys<K>()) {
-      MatchKind exact_match = MatchKind::kNe;
-      while (s != e) {
-        const size_type mid = (s + e) >> 1;
-        const absl::weak_ordering c = comp(key(mid), k);
-        if (c < 0) {
-          s = mid + 1;
-        } else {
-          e = mid;
-          if (c == 0) {
-            // Need to return the first value whose key is not less than k,
-            // which requires continuing the binary search if there could be
-            // multiple equivalent keys.
-            exact_match = MatchKind::kEq;
-          }
-        }
-      }
-      return {s, exact_match};
-    } else {  // Can't have multiple equivalent keys.
-      while (s != e) {
-        const size_type mid = (s + e) >> 1;
-        const absl::weak_ordering c = comp(key(mid), k);
-        if (c < 0) {
-          s = mid + 1;
-        } else if (c > 0) {
-          e = mid;
-        } else {
-          return {mid, MatchKind::kEq};
-        }
-      }
-      return {s, MatchKind::kNe};
-    }
+    return SearchOps::template NodeSearch</*kCanHaveMultipleEquivKeys=*/false>(
+               this, k, upper_bound_adapter<key_compare>(comp))
+        .value;
   }
 
   // Returns whether key i is ordered correctly with respect to the other keys
@@ -1208,6 +1118,96 @@
   return node_a == node_b;
 }
 
+struct BtreeIteratorBase {
+  btree_node_header* node;
+  int position;
+
+  template <typename FieldType, typename Node>
+  static void Increment(Node*& node, int& position, size_t children_offset);
+
+  template <typename FieldType, typename Node>
+  static void Decrement(Node*& node, int& position, size_t children_offset);
+
+  template <typename FieldType>
+  void increment(size_t children_offset) {
+    Increment<FieldType>(node, position, children_offset);
+  }
+
+  template <typename FieldType>
+  void decrement(size_t children_offset) {
+    Decrement<FieldType>(node, position, children_offset);
+  }
+
+  friend bool operator==(BtreeIteratorBase a, BtreeIteratorBase b) {
+    return a.node == b.node && a.position == b.position;
+  }
+  friend bool operator!=(BtreeIteratorBase a, BtreeIteratorBase b) {
+    return !(a == b);
+  }
+};
+
+// Returns n such that n calls to ++begin yields end.
+// Precondition: n exists && (begin.node != end.node || !begin.node->is_leaf()
+// || begin.position != end.position).
+template <typename FieldType>
+ptrdiff_t BtreeIteratorDistanceSlow(BtreeIteratorBase begin,
+                                    BtreeIteratorBase end,
+                                    size_t children_offset);
+extern template ptrdiff_t BtreeIteratorDistanceSlow<uint8_t>(BtreeIteratorBase,
+                                                             BtreeIteratorBase,
+                                                             size_t);
+extern template ptrdiff_t BtreeIteratorDistanceSlow<uint16_t>(BtreeIteratorBase,
+                                                              BtreeIteratorBase,
+                                                              size_t);
+
+template <typename FieldType>
+BtreeIteratorBase BtreeIteratorIncrementSlow(BtreeIteratorBase it,
+                                             size_t children_offset);
+extern template BtreeIteratorBase BtreeIteratorIncrementSlow<uint8_t>(
+    BtreeIteratorBase, size_t);
+extern template BtreeIteratorBase BtreeIteratorIncrementSlow<uint16_t>(
+    BtreeIteratorBase, size_t);
+
+template <typename FieldType>
+BtreeIteratorBase BtreeIteratorDecrementSlow(BtreeIteratorBase it,
+                                             size_t children_offset);
+extern template BtreeIteratorBase BtreeIteratorDecrementSlow<uint8_t>(
+    BtreeIteratorBase, size_t);
+extern template BtreeIteratorBase BtreeIteratorDecrementSlow<uint16_t>(
+    BtreeIteratorBase, size_t);
+
+template <typename FieldType, typename Node>
+inline void BtreeIteratorBase::Increment(Node*& node, int& position,
+                                         size_t children_offset) {
+  const auto* n = node->template as<FieldType>();
+  if (ABSL_PREDICT_TRUE(n->is_leaf() && ++position < n->finish())) {
+    return;
+  }
+  const BtreeIteratorBase res = BtreeIteratorIncrementSlow<FieldType>(
+      {const_cast<btree_node_header*>(
+           static_cast<const btree_node_header*>(node)),
+       position},
+      children_offset);
+  node = static_cast<Node*>(res.node);
+  position = res.position;
+}
+
+template <typename FieldType, typename Node>
+inline void BtreeIteratorBase::Decrement(Node*& node, int& position,
+                                         size_t children_offset) {
+  const auto* n = node->template as<FieldType>();
+  if (ABSL_PREDICT_TRUE(n->is_leaf() && --position >= n->start())) {
+    return;
+  }
+  const BtreeIteratorBase res = BtreeIteratorDecrementSlow<FieldType>(
+      {const_cast<btree_node_header*>(
+           static_cast<const btree_node_header*>(node)),
+       position},
+      children_offset);
+  node = static_cast<Node*>(res.node);
+  position = res.position;
+}
+
 class btree_iterator_generation_info_enabled {
  public:
   explicit btree_iterator_generation_info_enabled(uint32_t g)
@@ -1316,7 +1316,8 @@
       if (node_->is_leaf()) return position_ - other.position_;
       if (position_ == other.position_) return 0;
     }
-    return distance_slow(other);
+    return BtreeIteratorDistanceSlow<field_type>(other.raw(), raw(),
+                                                 node_type::kChildrenOffset);
   }
 
   // Advances the iterator by `n`. Values of `n` must not result in going past
@@ -1432,30 +1433,23 @@
     return true;
   }
 
-  // Returns n such that n calls to ++other yields *this.
-  // Precondition: n exists && (this->node_ != other.node_ ||
-  // !this->node_->is_leaf() || this->position_ != other.position_).
-  difference_type distance_slow(const_iterator other) const;
+  BtreeIteratorBase raw() const {
+    return {const_cast<normal_node*>(node_), position_};
+  }
 
   // Increment/decrement the iterator.
   void increment() {
     assert_valid_generation(node_);
-    if (node_->is_leaf() && ++position_ < node_->finish()) {
-      return;
-    }
-    increment_slow();
+    BtreeIteratorBase::Increment<field_type>(node_, position_,
+                                             node_type::kChildrenOffset);
   }
-  void increment_slow();
   btree_iterator& increment_n_slow(difference_type n);
 
   void decrement() {
     assert_valid_generation(node_);
-    if (node_->is_leaf() && --position_ >= node_->start()) {
-      return;
-    }
-    decrement_slow();
+    BtreeIteratorBase::Decrement<field_type>(node_, position_,
+                                             node_type::kChildrenOffset);
   }
-  void decrement_slow();
   btree_iterator& decrement_n_slow(difference_type n);
 
   const key_type& key() const {
@@ -1519,6 +1513,243 @@
   size_t size;
 
   bool empty() const { return size == 0; }
+
+  template <typename FieldType>
+  BtreeIteratorBase begin() const {
+    auto* lm = root->as<FieldType>()->parent();
+    return {lm, lm->start()};
+  }
+
+  template <typename FieldType>
+  BtreeIteratorBase end() const {
+    auto* rm = rightmost->as<FieldType>();
+    return {rm, rm->finish()};
+  }
+};
+
+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*);
+
+// Shared search and lookup operations parameterized only on key type,
+// comparator, node field_type, and slot layout (independent of mapped value
+// type, allocator, IsMulti, and IsMap).
+template <typename SlotKey, typename KeyCompare, typename FieldType,
+          size_t kSlotSize, size_t kSlotsOffset, size_t kChildrenOffset,
+          bool kUseLinearSearch>
+class BtreeOps {
+  using NodeBase = btree_node_base<FieldType>;
+  using CompParam =
+      type_traits_internal::AddConstRefIfExpensiveToCopy<KeyCompare>;
+  static constexpr bool kIsCompareTo =
+      btree_is_key_compare_to<KeyCompare, SlotKey>::value;
+
+  static const SlotKey& Key(const void* node, size_t i) {
+    return *std::launder(reinterpret_cast<const SlotKey*>(
+        static_cast<const char*>(node) + kSlotsOffset + i * kSlotSize));
+  }
+
+  // Returns the position of the first value whose key is not less than k using
+  // linear search performed using plain compare.
+  template <typename K, typename Compare>
+  static SearchResult<size_t, false> LinearSearchPlain(const NodeBase* node,
+                                                       const K& k, size_t s,
+                                                       const size_t e,
+                                                       const Compare& comp) {
+    while (s < e) {
+      if (!comp(Key(node, s), k)) {
+        break;
+      }
+      ++s;
+    }
+    return SearchResult<size_t, false>{s};
+  }
+
+  // Returns the position of the first value whose key is not less than k using
+  // linear search performed using compare-to.
+  template <typename K, typename Compare>
+  static SearchResult<size_t, true> LinearSearchCompareTo(const NodeBase* node,
+                                                          const K& k, size_t s,
+                                                          const size_t e,
+                                                          const Compare& comp) {
+    while (s < e) {
+      const absl::weak_ordering c = comp(Key(node, s), k);
+      if (c == 0) {
+        return {s, MatchKind::kEq};
+      } else if (c > 0) {
+        break;
+      }
+      ++s;
+    }
+    return {s, MatchKind::kNe};
+  }
+
+  // Returns the position of the first value whose key is not less than k using
+  // binary search performed using plain compare.
+  template <typename K, typename Compare>
+  static SearchResult<size_t, false> BinarySearchPlain(const NodeBase* node,
+                                                       const K& k, size_t s,
+                                                       size_t e,
+                                                       const Compare& comp) {
+    while (s != e) {
+      const size_t mid = (s + e) >> 1;
+      if (comp(Key(node, mid), k)) {
+        s = mid + 1;
+      } else {
+        e = mid;
+      }
+    }
+    return SearchResult<size_t, false>{s};
+  }
+
+  // Returns the position of the first value whose key is not less than k using
+  // binary search performed using compare-to.
+  template <bool kCanHaveMultipleEquivKeys, typename K, typename CompareTo>
+  static SearchResult<size_t, true> BinarySearchCompareTo(
+      const NodeBase* node, const K& k, size_t s, size_t e,
+      const CompareTo& comp) {
+    if constexpr (kCanHaveMultipleEquivKeys) {
+      MatchKind exact_match = MatchKind::kNe;
+      while (s != e) {
+        const size_t mid = (s + e) >> 1;
+        const absl::weak_ordering c = comp(Key(node, mid), k);
+        if (c < 0) {
+          s = mid + 1;
+        } else {
+          e = mid;
+          if (c == 0) {
+            // Need to return the first value whose key is not less than k,
+            // which requires continuing the binary search if there could be
+            // multiple equivalent keys.
+            exact_match = MatchKind::kEq;
+          }
+        }
+      }
+      return {s, exact_match};
+    } else {  // Can't have multiple equivalent keys.
+      while (s != e) {
+        const size_t mid = (s + e) >> 1;
+        const absl::weak_ordering c = comp(Key(node, mid), k);
+        if (c < 0) {
+          s = mid + 1;
+        } else if (c > 0) {
+          e = mid;
+        } else {
+          return {mid, MatchKind::kEq};
+        }
+      }
+      return {s, MatchKind::kNe};
+    }
+  }
+
+ public:
+  template <bool kCanHaveMultipleEquivKeys, typename Compare, typename K>
+  static SearchResult<size_t, btree_is_key_compare_to<Compare, SlotKey>::value>
+  NodeSearch(const NodeBase* node, const K& k, const Compare& comp) {
+    constexpr bool kCompTo = btree_is_key_compare_to<Compare, SlotKey>::value;
+    if constexpr (kUseLinearSearch) {
+      if constexpr (kCompTo) {
+        return LinearSearchCompareTo(node, k, node->start(), node->finish(),
+                                     comp);
+      } else {
+        return LinearSearchPlain(node, k, node->start(), node->finish(), comp);
+      }
+    } else {
+      if constexpr (kCompTo) {
+        return BinarySearchCompareTo<kCanHaveMultipleEquivKeys>(
+            node, k, node->start(), node->finish(), comp);
+      } else {
+        return BinarySearchPlain(node, k, node->start(), node->finish(), comp);
+      }
+    }
+  }
 };
 
 template <typename Params>
@@ -1618,7 +1849,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());
     }
   }
 
@@ -1626,12 +1857,10 @@
   btree& operator=(const btree& other);
   btree& operator=(btree&& other) noexcept;
 
-  iterator begin() { return iterator(leftmost()); }
-  const_iterator begin() const { return const_iterator(leftmost()); }
-  iterator end() { return iterator(rightmost(), rightmost()->finish()); }
-  const_iterator end() const {
-    return const_iterator(rightmost(), rightmost()->finish());
-  }
+  iterator begin() { return to_iter(common_.begin<field_type>()); }
+  const_iterator begin() const { return to_iter(common_.begin<field_type>()); }
+  iterator end() { return to_iter(common_.end<field_type>()); }
+  const_iterator end() const { return to_iter(common_.end<field_type>()); }
   reverse_iterator rbegin() { return reverse_iterator(end()); }
   const_reverse_iterator rbegin() const {
     return const_reverse_iterator(end());
@@ -1843,7 +2072,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); }
@@ -1911,6 +2179,14 @@
   // Tries to shrink the height of the tree by 1.
   void try_shrink();
 
+  iterator to_iter(BtreeIteratorBase raw) {
+    return iterator(static_cast<node_type*>(raw.node), raw.position);
+  }
+  const_iterator to_iter(BtreeIteratorBase raw) const {
+    return const_iterator(static_cast<const node_type*>(raw.node),
+                          raw.position);
+  }
+
   iterator internal_end(iterator iter) {
     return iter.node_ != nullptr ? iter : end();
   }
@@ -2022,7 +2298,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,180 +2486,18 @@
 
 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);
   }
 }
 
 ////
 // btree_iterator methods
 
-// Note: the implementation here is based on btree_node::clear_and_delete.
-template <typename N, typename R, typename P>
-auto btree_iterator<N, R, P>::distance_slow(const_iterator other) const
-    -> difference_type {
-  const_iterator begin = other;
-  const_iterator end = *this;
-  assert(begin.node_ != end.node_ || !begin.node_->is_leaf() ||
-         begin.position_ != end.position_);
-
-  const node_type* node = begin.node_;
-  // We need to compensate for double counting if begin.node_ is a leaf node.
-  difference_type count = node->is_leaf() ? -begin.position_ : 0;
-
-  // First navigate to the leftmost leaf node past begin.
-  if (node->is_internal()) {
-    ++count;
-    node = node->child(begin.position_ + 1);
-  }
-  while (node->is_internal()) node = node->start_child();
-
-  // 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();
-  const node_type* parent = node->parent();
-  for (;;) {
-    // In each iteration of the next loop, we count 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();
-      }
-      if (node == end.node_) return count + end.position_;
-      if (parent == end.node_ && pos == static_cast<size_type>(end.position_))
-        return count + node->count();
-      // +1 is for the next internal node value.
-      count += node->count() + 1;
-      ++pos;
-    } while (pos <= parent->finish());
-
-    // Once we've counted all children of parent, go up/right.
-    assert(pos > parent->finish());
-    do {
-      node = parent;
-      pos = node->position();
-      parent = node->parent();
-      // -1 because we counted the value at end and shouldn't.
-      if (parent == end.node_ && pos == static_cast<size_type>(end.position_))
-        return count - 1;
-      ++pos;
-    } while (pos > parent->finish());
-  }
-}
-
-template <typename N, typename R, typename P>
-void btree_iterator<N, R, P>::increment_slow() {
-  N* node = node_;
-  int position = position_;
-  if (node->is_leaf()) {
-    assert(position >= node->finish());
-    while (position == node->finish() && !node->is_root()) {
-      assert(node->parent()->child(node->position()) == node);
-      position = node->position();
-      node = node->parent();
-    }
-    // TODO(ezb): assert we aren't incrementing end() instead of handling.
-    if (position == node->finish()) {
-      return;
-    }
-  } else {
-    assert(position < node->finish());
-    node = node->child(static_cast<field_type>(position + 1));
-    while (node->is_internal()) {
-      node = node->start_child();
-    }
-    position = node->start();
-  }
-  *this = {node, position};
-}
-
-template <typename N, typename R, typename P>
-void btree_iterator<N, R, P>::decrement_slow() {
-  N* node = node_;
-  int position = position_;
-  if (node->is_leaf()) {
-    assert(position <= -1);
-    while (position < node->start() && !node->is_root()) {
-      assert(node->parent()->child(node->position()) == node);
-      position = node->position() - 1;
-      node = node->parent();
-    }
-    // TODO(ezb): assert we aren't decrementing begin() instead of handling.
-    if (position < node->start()) {
-      return;
-    }
-  } else {
-    assert(position >= node->start());
-    node = node->child(static_cast<field_type>(position));
-    while (node->is_internal()) {
-      node = node->child(node->finish());
-    }
-    position = node->finish() - 1;
-  }
-  *this = {node, position};
-}
-
 template <typename N, typename R, typename P>
 btree_iterator<N, R, P>& btree_iterator<N, R, P>::increment_n_slow(
     difference_type n) {
@@ -2492,6 +2606,10 @@
                 "iterator not trivially copyable.");
   static_assert(sizeof(node_type) == 1 && alignof(node_type) == 1,
                 "btree_node must remain an empty 1-byte type.");
+  if constexpr (params_type::is_map_container::value) {
+    static_assert(memory_internal::OffsetOf<value_type>::kFirst == 0,
+                  "Map key must be at offset 0 of value_type.");
+  }
 
   // Note: We assert that kTargetValues, which is computed from
   // Params::kTargetNodeSize, must fit the node_type::field_type.
@@ -2817,7 +2935,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 +2973,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;