From 7829e1889961cdcabfd327a2e71366a832fd2b0d Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Thu, 1 Oct 2020 15:02:40 +0200 Subject: [PATCH] BTreeMap: reuse BoxedNode instances directly instead of their contents --- library/alloc/src/collections/btree/node.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/library/alloc/src/collections/btree/node.rs b/library/alloc/src/collections/btree/node.rs index cf7961cbbfc..17ac5195fa7 100644 --- a/library/alloc/src/collections/btree/node.rs +++ b/library/alloc/src/collections/btree/node.rs @@ -127,10 +127,6 @@ impl BoxedNode { BoxedNode { ptr: Unique::from(&mut Box::leak(node).data) } } - unsafe fn from_ptr(ptr: NonNull>) -> Self { - BoxedNode { ptr: unsafe { Unique::new_unchecked(ptr.as_ptr()) } } - } - fn as_ptr(&self) -> NonNull> { NonNull::from(self.ptr) } @@ -198,7 +194,7 @@ impl Root { /// and is the opposite of `pop_internal_level`. pub fn push_internal_level(&mut self) -> NodeRef, K, V, marker::Internal> { let mut new_node = Box::new(unsafe { InternalNode::new() }); - new_node.edges[0].write(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) }); + new_node.edges[0].write(unsafe { ptr::read(&mut self.node) }); self.node = BoxedNode::from_internal(new_node); self.height += 1; @@ -224,8 +220,8 @@ impl Root { let top = self.node.ptr; - let internal_node = unsafe { self.internal_node_as_mut() }; - self.node = unsafe { BoxedNode::from_ptr(internal_node.first_edge().descend().node) }; + let mut internal_node = unsafe { self.internal_node_as_mut() }; + self.node = unsafe { internal_node.as_internal_mut().edges[0].assume_init_read() }; self.height -= 1; self.node_as_mut().as_leaf_mut().parent = None;