From 05c03d5104ae6d0790c153e4fa8df7ac7015db18 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Wed, 20 Sep 2017 19:07:12 -0700 Subject: [PATCH] Hoist lookup() into lru_cache. MozReview-Commit-ID: F5FbKFqpXEh --- components/lru_cache/lib.rs | 25 +++++++++++++++++++++++++ components/style/sharing/mod.rs | 27 ++------------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/components/lru_cache/lib.rs b/components/lru_cache/lib.rs index ada1ad3a950..53a165f2dde 100644 --- a/components/lru_cache/lib.rs +++ b/components/lru_cache/lib.rs @@ -89,6 +89,31 @@ impl>> LRUCache { } } + /// Performs a lookup on the cache with the given test routine. Touches + /// the result on a hit. + pub fn lookup(&mut self, mut test_one: F) -> Option + where + F: FnMut(&mut T) -> Option + { + let mut result = None; + for (i, candidate) in self.iter_mut() { + if let Some(r) = test_one(candidate) { + result = Some((i, r)); + break; + } + }; + + match result { + None => None, + Some((i, r)) => { + self.touch(i); + let front = self.front_mut().unwrap(); + debug_assert!(test_one(front).is_some()); + Some(r) + } + } + } + /// Insert a given key in the cache. pub fn insert(&mut self, val: T) { let entry = Entry { val, prev: 0, next: 0 }; diff --git a/components/style/sharing/mod.rs b/components/style/sharing/mod.rs index e6dabc7f153..c1b843993a7 100644 --- a/components/style/sharing/mod.rs +++ b/components/style/sharing/mod.rs @@ -442,29 +442,6 @@ impl SharingCache { }; self.entries.insert(StyleSharingCandidate { element, validation_data }); } - - fn lookup(&mut self, mut test_one: F) -> Option - where - F: FnMut(&mut StyleSharingCandidate) -> Option - { - let mut result = None; - for (i, candidate) in self.entries.iter_mut() { - if let Some(r) = test_one(candidate) { - result = Some((i, r)); - break; - } - }; - - match result { - None => None, - Some((i, r)) => { - self.entries.touch(i); - let front = self.entries.front_mut().unwrap(); - debug_assert!(test_one(front).is_some()); - Some(r) - } - } - } } /// Style sharing caches are are large allocations, so we store them in thread-local @@ -638,7 +615,7 @@ impl StyleSharingCache { return None; } - self.cache_mut().lookup(|candidate| { + self.cache_mut().entries.lookup(|candidate| { Self::test_candidate( target, candidate, @@ -755,7 +732,7 @@ impl StyleSharingCache { return None; } - self.cache_mut().lookup(|candidate| { + self.cache_mut().entries.lookup(|candidate| { debug_assert_ne!(candidate.element, target); if !candidate.parent_style_identity().eq(inherited) { return None;