Skip to content

Commit

Permalink
Make BTreeMap::clone() not allocate when cloning an empty tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Jul 2, 2018
1 parent a2be769 commit f46f05b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/liballoc/collections/btree/map.rs
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 f46f05b

Please sign in to comment.