diff options
author | Matt Brubeck <mbrubeck@limpet.net> | 2016-05-13 17:29:13 -0700 |
---|---|---|
committer | Matt Brubeck <mbrubeck@limpet.net> | 2016-05-13 18:20:25 -0700 |
commit | d2717c44750ebbecabba90b5dfc6cbca116fc5b4 (patch) | |
tree | fe51fcc544c349a2737db0497a725f055a349c46 /components/util | |
parent | 1c5ec6f3ec0081b576a12b89fb6ae97c65c56799 (diff) | |
download | servo-d2717c44750ebbecabba90b5dfc6cbca116fc5b4.tar.gz servo-d2717c44750ebbecabba90b5dfc6cbca116fc5b4.zip |
Eliminate unneeded clones in find_or_create
...and use it to eliminate duplicate hash lookups and string copies in shape_text.
Diffstat (limited to 'components/util')
-rw-r--r-- | components/util/cache.rs | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/components/util/cache.rs b/components/util/cache.rs index 7b8d7bfb1a0..7af590b015e 100644 --- a/components/util/cache.rs +++ b/components/util/cache.rs @@ -13,14 +13,14 @@ use std::slice::Iter; #[derive(Debug)] pub struct HashCache<K, V> - where K: Clone + PartialEq + Eq + Hash, + where K: PartialEq + Eq + Hash, V: Clone, { entries: HashMap<K, V, BuildHasherDefault<SipHasher>>, } impl<K, V> HashCache<K, V> - where K: Clone + PartialEq + Eq + Hash, + where K: PartialEq + Eq + Hash, V: Clone, { pub fn new() -> HashCache<K, V> { @@ -40,13 +40,13 @@ impl<K, V> HashCache<K, V> } } - pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V { - match self.entries.entry(key.clone()) { + pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V { + match self.entries.entry(key) { Occupied(occupied) => { (*occupied.get()).clone() } Vacant(vacant) => { - (*vacant.insert(blk(key))).clone() + (*vacant.insert(blk())).clone() } } } @@ -61,7 +61,7 @@ pub struct LRUCache<K, V> { cache_size: usize, } -impl<K: Clone + PartialEq, V: Clone> LRUCache<K, V> { +impl<K: PartialEq, V: Clone> LRUCache<K, V> { pub fn new(size: usize) -> LRUCache<K, V> { LRUCache { entries: vec!(), @@ -97,12 +97,12 @@ impl<K: Clone + PartialEq, V: Clone> LRUCache<K, V> { } } - pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V { - match self.entries.iter().position(|&(ref k, _)| *k == *key) { + pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V { + match self.entries.iter().position(|&(ref k, _)| *k == key) { Some(pos) => self.touch(pos), None => { - let val = blk(key); - self.insert(key.clone(), val.clone()); + let val = blk(); + self.insert(key, val.clone()); val } } @@ -154,13 +154,12 @@ impl<K: Clone + Eq + Hash, V: Clone> SimpleHashCache<K, V> { } } - pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V { - match self.find(key) { - Some(value) => return value, - None => {} + pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V { + if let Some(value) = self.find(&key) { + return value; } - let value = blk(key); - self.insert((*key).clone(), value.clone()); + let value = blk(); + self.insert(key, value.clone()); value } |