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.
This commit is contained in:
Matt Brubeck 2017-12-07 09:54:43 -08:00
parent 99c2db0549
commit 1f22041f5f

View file

@ -538,17 +538,17 @@ impl<E: TElement> SelectorFlagsMap<E> {
pub fn insert_flags(&mut self, element: E, flags: ElementSelectorFlags) { pub fn insert_flags(&mut self, element: E, flags: ElementSelectorFlags) {
let el = unsafe { SendElement::new(element) }; let el = unsafe { SendElement::new(element) };
// Check the cache. If the flags have already been noted, we're done. // Check the cache. If the flags have already been noted, we're done.
if self.cache.iter().find(|&(_, ref x)| x.0 == el) if let Some(item) = self.cache.find(|x| x.0 == el) {
.map_or(ElementSelectorFlags::empty(), |(_, x)| x.1) if !item.1.contains(flags) {
.contains(flags) { item.1.insert(flags);
self.map.get_mut(&el).unwrap().insert(flags);
}
return; return;
} }
let f = self.map.entry(el).or_insert(ElementSelectorFlags::empty()); let f = self.map.entry(el).or_insert(ElementSelectorFlags::empty());
*f |= flags; *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)) self.cache.insert((unsafe { SendElement::new(element) }, *f))
} }