Skip to content

Commit

Permalink
Auto merge of #51893 - nnethercote:BTreeMap-clone-noalloc, r=nnethercote
Browse files Browse the repository at this point in the history
Make `BTreeMap::clone()` not allocate when cloning an empty tree.

r? @gankro

CC @porglezomp
  • Loading branch information
bors committed Jul 2, 2018
2 parents a53bd20 + f46f05b commit a96c88e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,16 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
}
}

clone_subtree(self.root.as_ref())
if self.len() == 0 {
// Ideally we'd call `BTreeMap::new` here, but that has the `K:
// Ord` constraint, which this method lacks.
BTreeMap {
root: node::Root::shared_empty_root(),
length: 0,
}
} else {
clone_subtree(self.root.as_ref())
}
}
}

Expand Down

0 comments on commit a96c88e

Please sign in to comment.