servo: Merge #1040 - Fix eviction in MonoCache::find_or_create (from kmcallister:monocache); r=metajack
authorKeegan McAllister <kmcallister@mozilla.com>
Mon, 14 Oct 2013 08:07:17 -0700
changeset 380357 9c960a25ba5e86b860b6a0b7399c624022635c9c
parent 380356 ae3e7bc339b3825e79cbfdd6587cccc0c5621ef7
child 380358 25e124e7f152c429fc0c2863524eb160d19a4874
push id7198
push userjlorenzo@mozilla.com
push dateTue, 18 Apr 2017 12:07:49 +0000
treeherdermozilla-beta@d57aa49c3948 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmetajack
servo: Merge #1040 - Fix eviction in MonoCache::find_or_create (from kmcallister:monocache); r=metajack Source-Repo: https://github.com/servo/servo Source-Revision: 33e938a965558a86e539df86f33de7c5e17c0771
servo/src/components/util/cache.rs
--- a/servo/src/components/util/cache.rs
+++ b/servo/src/components/util/cache.rs
@@ -29,23 +29,23 @@ impl<K: Clone + Eq, V: Clone> Cache<K,V>
     fn find(&mut self, key: &K) -> Option<V> {
         match self.entry {
             None => None,
             Some((ref k, ref v)) => if *k == *key { Some(v.clone()) } else { None }
         }
     }
 
     fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
-        match self.entry {
-            None => { 
+        match self.find(key) {
+            Some(value) => value,
+            None => {
                 let value = blk(key);
                 self.entry = Some((key.clone(), value.clone()));
                 value
-            },
-            Some((ref _k, ref v)) => v.clone()
+            }
         }
     }
 
     fn evict_all(&mut self) {
         self.entry = None;
     }
 }
 
@@ -53,21 +53,19 @@ impl<K: Clone + Eq, V: Clone> Cache<K,V>
 fn test_monocache() {
     let mut cache = MonoCache::new(10);
     let one = @"one";
     let two = @"two";
     cache.insert(1, one);
 
     assert!(cache.find(&1).is_some());
     assert!(cache.find(&2).is_none());
-    /* FIXME: clarify behavior here:
     cache.find_or_create(&2, |_v| { two });
     assert!(cache.find(&2).is_some());
     assert!(cache.find(&1).is_none());
-    */
 }
 
 pub struct HashCache<K, V> {
     entries: HashMap<K, V>,
 }
 
 impl<K: Clone + Eq + Hash, V: Clone> HashCache<K,V> {
     pub fn new() -> HashCache<K, V> {