From 1f22041f5f980446e102c2a709404999ad57aee8 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Thu, 7 Dec 2017 09:54:43 -0800 Subject: [PATCH] Improve LRU cache behavior in SelectorFlagsMap This code used to insert duplicate entries to avoid expensive shuffling of the LRU cache. With uluru this is no longer necessary, because reordering the cache is cheap. Now it uses the `LRUCache::find` method from uluru 0.2 to update entries in-place. This should improve the hit rate, because it eliminates unnecessary evictions. --- components/style/context.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/style/context.rs b/components/style/context.rs index 1b233fb7af28..7843c4240465 100644 --- a/components/style/context.rs +++ b/components/style/context.rs @@ -538,17 +538,17 @@ impl SelectorFlagsMap { pub fn insert_flags(&mut self, element: E, flags: ElementSelectorFlags) { let el = unsafe { SendElement::new(element) }; // Check the cache. If the flags have already been noted, we're done. - if self.cache.iter().find(|&(_, ref x)| x.0 == el) - .map_or(ElementSelectorFlags::empty(), |(_, x)| x.1) - .contains(flags) { + if let Some(item) = self.cache.find(|x| x.0 == el) { + if !item.1.contains(flags) { + item.1.insert(flags); + self.map.get_mut(&el).unwrap().insert(flags); + } return; } let f = self.map.entry(el).or_insert(ElementSelectorFlags::empty()); *f |= flags; - // Insert into the cache. We don't worry about duplicate entries, - // which lets us avoid reshuffling. self.cache.insert((unsafe { SendElement::new(element) }, *f)) }