perf(header): speed up VecMap insert and get

This commit is contained in:
Sean McArthur
2017-06-23 18:56:42 -07:00
parent 42f5f78de0
commit 7d9dfee8ce

View File

@@ -13,11 +13,15 @@ impl<K: PartialEq, V> VecMap<K, V> {
#[inline] #[inline]
pub fn insert(&mut self, key: K, value: V) { pub fn insert(&mut self, key: K, value: V) {
match self.find(&key) { // not using entry or find_mut because of borrowck
Some(pos) => self.vec[pos] = (key, value), for entry in &mut self.vec {
None => self.vec.push((key, value)) if key == entry.0 {
*entry = (key, value);
return;
} }
} }
self.vec.push((key, value));
}
#[inline] #[inline]
pub fn append(&mut self, key: K, value: V) { pub fn append(&mut self, key: K, value: V) {
@@ -26,13 +30,13 @@ impl<K: PartialEq, V> VecMap<K, V> {
#[inline] #[inline]
pub fn entry(&mut self, key: K) -> Entry<K, V> { pub fn entry(&mut self, key: K) -> Entry<K, V> {
match self.find(&key) { match self.pos(&key) {
Some(pos) => Entry::Occupied(OccupiedEntry { Some(pos) => Entry::Occupied(OccupiedEntry {
vec: self, vec: &mut self.vec,
pos: pos, pos: pos,
}), }),
None => Entry::Vacant(VacantEntry { None => Entry::Vacant(VacantEntry {
vec: self, vec: &mut self.vec,
key: key, key: key,
}) })
} }
@@ -40,12 +44,12 @@ impl<K: PartialEq, V> VecMap<K, V> {
#[inline] #[inline]
pub fn get<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<&V> { pub fn get<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<&V> {
self.find(key).map(move |pos| &self.vec[pos].1) self.find(key).map(|entry| &entry.1)
} }
#[inline] #[inline]
pub fn get_mut<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<&mut V> { pub fn get_mut<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<&mut V> {
self.find(key).map(move |pos| &mut self.vec[pos].1) self.find_mut(key).map(|entry| &mut entry.1)
} }
#[inline] #[inline]
@@ -63,7 +67,7 @@ impl<K: PartialEq, V> VecMap<K, V> {
#[inline] #[inline]
pub fn remove<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<V> { pub fn remove<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<V> {
self.find(key).map(|pos| self.vec.remove(pos)).map(|(_, v)| v) self.pos(key).map(|pos| self.vec.remove(pos)).map(|(_, v)| v)
} }
#[inline] #[inline]
@@ -76,14 +80,33 @@ impl<K: PartialEq, V> VecMap<K, V> {
} }
} }
#[inline] #[inline]
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.vec.clear(); self.vec.clear();
} }
#[inline] #[inline]
fn find<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<usize> { fn find<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<&(K, V)> {
for entry in &self.vec {
if key == &entry.0 {
return Some(entry);
}
}
None
}
#[inline]
fn find_mut<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<&mut (K, V)> {
for entry in &mut self.vec {
if key == &entry.0 {
return Some(entry);
}
}
None
}
#[inline]
fn pos<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<usize> {
self.vec.iter().position(|entry| key == &entry.0) self.vec.iter().position(|entry| key == &entry.0)
} }
} }
@@ -94,26 +117,25 @@ pub enum Entry<'a, K: 'a, V: 'a> {
} }
pub struct VacantEntry<'a, K: 'a, V: 'a> { pub struct VacantEntry<'a, K: 'a, V: 'a> {
vec: &'a mut VecMap<K, V>, vec: &'a mut Vec<(K, V)>,
key: K, key: K,
} }
impl<'a, K, V> VacantEntry<'a, K, V> { impl<'a, K, V> VacantEntry<'a, K, V> {
pub fn insert(self, val: V) -> &'a mut V { pub fn insert(self, val: V) -> &'a mut V {
let mut vec = self.vec; self.vec.push((self.key, val));
vec.vec.push((self.key, val)); let pos = self.vec.len() - 1;
let pos = vec.vec.len() - 1; &mut self.vec[pos].1
&mut vec.vec[pos].1
} }
} }
pub struct OccupiedEntry<'a, K: 'a, V: 'a> { pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
vec: &'a mut VecMap<K, V>, vec: &'a mut Vec<(K, V)>,
pos: usize, pos: usize,
} }
impl<'a, K, V> OccupiedEntry<'a, K, V> { impl<'a, K, V> OccupiedEntry<'a, K, V> {
pub fn into_mut(self) -> &'a mut V { pub fn into_mut(self) -> &'a mut V {
&mut self.vec.vec[self.pos].1 &mut self.vec[self.pos].1
} }
} }