style: Iterate the LRU cache contents from back to front.

We put the more recently used item last, so iterating then from left to right is
pointless.
This commit is contained in:
Emilio Cobos Álvarez 2017-03-09 15:39:17 +01:00
parent 2f8cf6afc3
commit d3e7f1f0f4
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 15 additions and 9 deletions

View file

@ -6,7 +6,7 @@
#![deny(missing_docs)] #![deny(missing_docs)]
use std::slice::{Iter, IterMut}; use std::{iter, slice};
/// A LRU cache used to store a set of at most `n` elements at the same time. /// A LRU cache used to store a set of at most `n` elements at the same time.
/// ///
@ -16,6 +16,12 @@ pub struct LRUCache<K> {
cache_size: usize, cache_size: usize,
} }
/// A iterator over the items of the LRU cache.
pub type LRUCacheIterator<'a, K> = iter::Rev<slice::Iter<'a, K>>;
/// A iterator over the mutable items of the LRU cache.
pub type LRUCacheMutIterator<'a, K> = iter::Rev<slice::IterMut<'a, K>>;
impl<K: PartialEq> LRUCache<K> { impl<K: PartialEq> LRUCache<K> {
/// Create a new LRU cache with `size` elements at most. /// Create a new LRU cache with `size` elements at most.
pub fn new(size: usize) -> Self { pub fn new(size: usize) -> Self {
@ -35,14 +41,15 @@ impl<K: PartialEq> LRUCache<K> {
} }
} }
/// Iterate over the contents of this cache. /// Iterate over the contents of this cache, from more to less recently
pub fn iter(&self) -> Iter<K> { /// used.
self.entries.iter() pub fn iter(&self) -> LRUCacheIterator<K> {
self.entries.iter().rev()
} }
/// Iterate mutably over the contents of this cache. /// Iterate mutably over the contents of this cache.
pub fn iter_mut(&mut self) -> IterMut<K> { pub fn iter_mut(&mut self) -> LRUCacheMutIterator<K> {
self.entries.iter_mut() self.entries.iter_mut().rev()
} }
/// Insert a given key in the cache. /// Insert a given key in the cache.

View file

@ -10,7 +10,7 @@
use {Atom, LocalName}; use {Atom, LocalName};
use animation::{self, Animation, PropertyAnimation}; use animation::{self, Animation, PropertyAnimation};
use atomic_refcell::AtomicRefMut; use atomic_refcell::AtomicRefMut;
use cache::LRUCache; use cache::{LRUCache, LRUCacheMutIterator};
use cascade_info::CascadeInfo; use cascade_info::CascadeInfo;
use context::{SequentialTask, SharedStyleContext, StyleContext}; use context::{SequentialTask, SharedStyleContext, StyleContext};
use data::{ComputedStyle, ElementData, ElementStyles, RestyleData}; use data::{ComputedStyle, ElementData, ElementStyles, RestyleData};
@ -27,7 +27,6 @@ use selectors::matching::AFFECTED_BY_PSEUDO_ELEMENTS;
use servo_config::opts; use servo_config::opts;
use sink::ForgetfulSink; use sink::ForgetfulSink;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::slice::IterMut;
use std::sync::Arc; use std::sync::Arc;
use stylist::ApplicableDeclarationBlock; use stylist::ApplicableDeclarationBlock;
@ -366,7 +365,7 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
} }
} }
fn iter_mut(&mut self) -> IterMut<StyleSharingCandidate<E>> { fn iter_mut(&mut self) -> LRUCacheMutIterator<StyleSharingCandidate<E>> {
self.cache.iter_mut() self.cache.iter_mut()
} }