Today I thought I would see if I could optimise the implementation of Clone for my BTreeMap. Previously I was using a CursorMut to append cloned values ( which was already slightly faster than the std implementation incidentally), so I thought I would see what would happen with a more "direct" implementation, and it was indeed much faster ( about 5 or 6 times faster ), probably partly because there are no re-allocations. As part of that, I had to implement Clone for my Vec types, here is one:
impl<T> Clone for ShortVec<T>
where
T: Clone,
{
fn clone(&self) -> Self {
let mut c = Self::new(self.cap as usize);
c.allocate(self.alloc as usize);
for i in 0..self.len() {
c.push(self.ix(i).clone());
}
c
}
}
( From btree_experiment 0.1.94 - Docs.rs )
So it is allocating the same as the original, then pushing the cloned values. This is working well enough, but I wondered if there was a way to clone more directly using raw pointers somehow that would be more efficient, especially for types that are copy (and so can just be copied with a ptr copy operation ). Am I missing something?