1- // ignore-tidy-filelength
2-
31use core:: borrow:: Borrow ;
42use core:: cmp:: Ordering ;
53use core:: fmt:: { self , Debug } ;
@@ -1288,11 +1286,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
12881286 pub ( super ) fn drain_filter_inner ( & mut self ) -> DrainFilterInner < ' _ , K , V > {
12891287 let root_node = self . root . as_mut ( ) . map ( |r| r. node_as_mut ( ) ) ;
12901288 let front = root_node. map ( |rn| rn. first_leaf_edge ( ) ) ;
1291- DrainFilterInner {
1292- length : & mut self . length ,
1293- cur_leaf_edge : front,
1294- emptied_internal_root : false ,
1295- }
1289+ DrainFilterInner { length : & mut self . length , cur_leaf_edge : front }
12961290 }
12971291
12981292 /// Calculates the number of elements if it is incorrect.
@@ -1708,7 +1702,6 @@ where
17081702pub ( super ) struct DrainFilterInner < ' a , K : ' a , V : ' a > {
17091703 length : & ' a mut usize ,
17101704 cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1711- emptied_internal_root : bool ,
17121705}
17131706
17141707#[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
@@ -1749,17 +1742,6 @@ where
17491742 }
17501743}
17511744
1752- impl < K , V > Drop for DrainFilterInner < ' _ , K , V > {
1753- fn drop ( & mut self ) {
1754- if self . emptied_internal_root {
1755- if let Some ( handle) = self . cur_leaf_edge . take ( ) {
1756- let root = handle. into_node ( ) . into_root_mut ( ) ;
1757- root. pop_internal_level ( ) ;
1758- }
1759- }
1760- }
1761- }
1762-
17631745impl < ' a , K : ' a , V : ' a > DrainFilterInner < ' a , K , V > {
17641746 /// Allow Debug implementations to predict the next element.
17651747 pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
@@ -1776,7 +1758,7 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
17761758 let ( k, v) = kv. kv_mut ( ) ;
17771759 if pred ( k, v) {
17781760 * self . length -= 1 ;
1779- let ( kv, pos) = kv. remove_kv_tracking ( |_| self . emptied_internal_root = true ) ;
1761+ let ( kv, pos) = kv. remove_kv_tracking ( ) ;
17801762 self . cur_leaf_edge = Some ( pos) ;
17811763 return Some ( kv) ;
17821764 }
@@ -2799,39 +2781,32 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
27992781 fn remove_kv ( self ) -> ( K , V ) {
28002782 * self . length -= 1 ;
28012783
2802- let ( old_kv, _) =
2803- self . handle . remove_kv_tracking ( |root| root. into_root_mut ( ) . pop_internal_level ( ) ) ;
2784+ let ( old_kv, _) = self . handle . remove_kv_tracking ( ) ;
28042785 old_kv
28052786 }
28062787}
28072788
28082789impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
2809- /// Removes a key/value-pair from the tree, and returns that pair, as well as
2810- /// the leaf edge corresponding to that former pair. It's possible this leaves
2811- /// an empty internal root node, which the caller should subsequently pop from
2812- /// the map holding the tree. The caller should also decrement the map's length.
2813- fn remove_kv_tracking < F > (
2790+ /// Removes a key/value-pair from the map, and returns that pair, as well as
2791+ /// the leaf edge corresponding to that former pair.
2792+ fn remove_kv_tracking (
28142793 self ,
2815- handle_emptied_internal_root : F ,
2816- ) -> ( ( K , V ) , Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > )
2817- where
2818- F : FnOnce ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > ) ,
2819- {
2794+ ) -> ( ( K , V ) , Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > ) {
28202795 let ( old_kv, mut pos, was_internal) = match self . force ( ) {
28212796 Leaf ( leaf) => {
28222797 let ( old_kv, pos) = leaf. remove ( ) ;
28232798 ( old_kv, pos, false )
28242799 }
28252800 Internal ( mut internal) => {
2826- // Replace the location freed in the internal node with the next KV,
2827- // and remove that next KV from its leaf.
2801+ // Replace the location freed in the internal node with an
2802+ // adjacent KV, and remove that adjacent KV from its leaf.
2803+ // Always choose the adjacent KV on the left side because
2804+ // it is typically faster to pop an element from the end
2805+ // of the KV arrays without needing to shift other elements.
28282806
28292807 let key_loc = internal. kv_mut ( ) . 0 as * mut K ;
28302808 let val_loc = internal. kv_mut ( ) . 1 as * mut V ;
28312809
2832- // Deleting from the left side is typically faster since we can
2833- // just pop an element from the end of the KV array without
2834- // needing to shift the other values.
28352810 let to_remove = internal. left_edge ( ) . descend ( ) . last_leaf_edge ( ) . left_kv ( ) . ok ( ) ;
28362811 let to_remove = unsafe { unwrap_unchecked ( to_remove) } ;
28372812
@@ -2867,8 +2842,8 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
28672842 if parent. len ( ) == 0 {
28682843 // The parent that was just emptied must be the root,
28692844 // because nodes on a lower level would not have been
2870- // left underfull. It has to be popped off the tree soon .
2871- handle_emptied_internal_root ( parent) ;
2845+ // left with a single child .
2846+ parent. into_root_mut ( ) . pop_internal_level ( ) ;
28722847 break ;
28732848 } else {
28742849 cur_node = parent. forget_type ( ) ;
@@ -2972,19 +2947,15 @@ fn handle_underfull_node<K, V>(
29722947 Err ( _) => return AtRoot ,
29732948 } ;
29742949
2950+ // Prefer the left KV if it exists. Merging with the left side is faster,
2951+ // since merging happens towards the left and `node` has fewer elements.
2952+ // Stealing from the left side is faster, since we can pop from the end of
2953+ // the KV arrays.
29752954 let ( is_left, mut handle) = match parent. left_kv ( ) {
29762955 Ok ( left) => ( true , left) ,
29772956 Err ( parent) => {
2978- match parent. right_kv ( ) {
2979- Ok ( right) => ( false , right) ,
2980- Err ( _) => {
2981- // The underfull node has an empty parent, so it is the only child
2982- // of an empty root. It is destined to become the new root, thus
2983- // allowed to be underfull. The empty parent should be removed later
2984- // by `pop_internal_level`.
2985- return AtRoot ;
2986- }
2987- }
2957+ let right = unsafe { unwrap_unchecked ( parent. right_kv ( ) . ok ( ) ) } ;
2958+ ( false , right)
29882959 }
29892960 } ;
29902961
0 commit comments