mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
style: Simplify style sharing code.
This commit is contained in:
parent
1eb24f1adc
commit
727be58b4a
4 changed files with 17 additions and 80 deletions
|
@ -11,14 +11,14 @@ use std::slice::{Iter, IterMut};
|
||||||
/// 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.
|
||||||
///
|
///
|
||||||
/// Currently used for the style sharing candidate cache.
|
/// Currently used for the style sharing candidate cache.
|
||||||
pub struct LRUCache<K, V> {
|
pub struct LRUCache<K> {
|
||||||
entries: Vec<(K, V)>,
|
entries: Vec<K>,
|
||||||
cache_size: usize,
|
cache_size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialEq, V: Clone> LRUCache<K, V> {
|
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) -> LRUCache<K, V> {
|
pub fn new(size: usize) -> Self {
|
||||||
LRUCache {
|
LRUCache {
|
||||||
entries: vec![],
|
entries: vec![],
|
||||||
cache_size: size,
|
cache_size: size,
|
||||||
|
@ -27,54 +27,30 @@ impl<K: PartialEq, V: Clone> LRUCache<K, V> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Touch a given position, and put it in the last item on the list.
|
/// Touch a given position, and put it in the last item on the list.
|
||||||
pub fn touch(&mut self, pos: usize) -> &V {
|
pub fn touch(&mut self, pos: usize) {
|
||||||
let last_index = self.entries.len() - 1;
|
let last_index = self.entries.len() - 1;
|
||||||
if pos != last_index {
|
if pos != last_index {
|
||||||
let entry = self.entries.remove(pos);
|
let entry = self.entries.remove(pos);
|
||||||
self.entries.push(entry);
|
self.entries.push(entry);
|
||||||
}
|
}
|
||||||
&self.entries[last_index].1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over the contents of this cache.
|
/// Iterate over the contents of this cache.
|
||||||
pub fn iter(&self) -> Iter<(K, V)> {
|
pub fn iter(&self) -> Iter<K> {
|
||||||
self.entries.iter()
|
self.entries.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate mutably over the contents of this cache.
|
/// Iterate mutably over the contents of this cache.
|
||||||
pub fn iter_mut(&mut self) -> IterMut<(K, V)> {
|
pub fn iter_mut(&mut self) -> IterMut<K> {
|
||||||
self.entries.iter_mut()
|
self.entries.iter_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a given key and value in the cache.
|
/// Insert a given key in the cache.
|
||||||
pub fn insert(&mut self, key: K, val: V) {
|
pub fn insert(&mut self, key: K) {
|
||||||
if self.entries.len() == self.cache_size {
|
if self.entries.len() == self.cache_size {
|
||||||
self.entries.remove(0);
|
self.entries.remove(0);
|
||||||
}
|
}
|
||||||
self.entries.push((key, val));
|
self.entries.push(key);
|
||||||
}
|
|
||||||
|
|
||||||
/// Try to find a key in the cache.
|
|
||||||
pub fn find(&mut self, key: &K) -> Option<V> {
|
|
||||||
match self.entries.iter().position(|&(ref k, _)| key == k) {
|
|
||||||
Some(pos) => Some(self.touch(pos).clone()),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Try to find a given key, or create a given item with that key executing
|
|
||||||
/// `blk`.
|
|
||||||
pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V
|
|
||||||
where F: FnMut() -> V,
|
|
||||||
{
|
|
||||||
match self.entries.iter().position(|&(ref k, _)| *k == key) {
|
|
||||||
Some(pos) => self.touch(pos).clone(),
|
|
||||||
None => {
|
|
||||||
let val = blk();
|
|
||||||
self.insert(key, val.clone());
|
|
||||||
val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Evict all elements from the cache.
|
/// Evict all elements from the cache.
|
||||||
|
|
|
@ -110,7 +110,7 @@ impl<E: TElement> PartialEq<StyleSharingCandidate<E>> for StyleSharingCandidate<
|
||||||
/// Note that this cache is flushed every time we steal work from the queue, so
|
/// Note that this cache is flushed every time we steal work from the queue, so
|
||||||
/// storing nodes here temporarily is safe.
|
/// storing nodes here temporarily is safe.
|
||||||
pub struct StyleSharingCandidateCache<E: TElement> {
|
pub struct StyleSharingCandidateCache<E: TElement> {
|
||||||
cache: LRUCache<StyleSharingCandidate<E>, ()>,
|
cache: LRUCache<StyleSharingCandidate<E>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A cache miss result.
|
/// A cache miss result.
|
||||||
|
@ -366,7 +366,7 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter_mut(&mut self) -> IterMut<(StyleSharingCandidate<E>, ())> {
|
fn iter_mut(&mut self) -> IterMut<StyleSharingCandidate<E>> {
|
||||||
self.cache.iter_mut()
|
self.cache.iter_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,7 +410,7 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
|
||||||
element: unsafe { SendElement::new(*element) },
|
element: unsafe { SendElement::new(*element) },
|
||||||
common_style_affecting_attributes: None,
|
common_style_affecting_attributes: None,
|
||||||
class_attributes: None,
|
class_attributes: None,
|
||||||
}, ());
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Touch a given index in the style sharing candidate cache.
|
/// Touch a given index in the style sharing candidate cache.
|
||||||
|
@ -881,8 +881,10 @@ pub trait MatchMethods : TElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut should_clear_cache = false;
|
let mut should_clear_cache = false;
|
||||||
for (i, &mut (ref mut candidate, ())) in style_sharing_candidate_cache.iter_mut().enumerate() {
|
for (i, candidate) in style_sharing_candidate_cache.iter_mut().enumerate() {
|
||||||
let sharing_result = self.share_style_with_candidate_if_possible(shared_context, candidate);
|
let sharing_result =
|
||||||
|
self.share_style_with_candidate_if_possible(shared_context,
|
||||||
|
candidate);
|
||||||
match sharing_result {
|
match sharing_result {
|
||||||
Ok(shared_style) => {
|
Ok(shared_style) => {
|
||||||
// Yay, cache hit. Share the style.
|
// Yay, cache hit. Share the style.
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
use std::cell::Cell;
|
|
||||||
use style::cache::LRUCache;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_lru_cache() {
|
|
||||||
let one = Cell::new("one");
|
|
||||||
let two = Cell::new("two");
|
|
||||||
let three = Cell::new("three");
|
|
||||||
let four = Cell::new("four");
|
|
||||||
|
|
||||||
// Test normal insertion.
|
|
||||||
let mut cache: LRUCache<usize, Cell<&str>> = LRUCache::new(2); // (_, _) (cache is empty)
|
|
||||||
cache.insert(1, one); // (1, _)
|
|
||||||
cache.insert(2, two); // (1, 2)
|
|
||||||
cache.insert(3, three); // (2, 3)
|
|
||||||
|
|
||||||
assert!(cache.find(&1).is_none()); // (2, 3) (no change)
|
|
||||||
assert!(cache.find(&3).is_some()); // (2, 3)
|
|
||||||
assert!(cache.find(&2).is_some()); // (3, 2)
|
|
||||||
|
|
||||||
// Test that LRU works (this insertion should replace 3, not 2).
|
|
||||||
cache.insert(4, four); // (2, 4)
|
|
||||||
|
|
||||||
assert!(cache.find(&1).is_none()); // (2, 4) (no change)
|
|
||||||
assert!(cache.find(&2).is_some()); // (4, 2)
|
|
||||||
assert!(cache.find(&3).is_none()); // (4, 2) (no change)
|
|
||||||
assert!(cache.find(&4).is_some()); // (2, 4) (no change)
|
|
||||||
|
|
||||||
// Test find_or_create.
|
|
||||||
cache.find_or_create(1, || { Cell::new("one") }); // (4, 1)
|
|
||||||
|
|
||||||
assert!(cache.find(&1).is_some()); // (4, 1) (no change)
|
|
||||||
assert!(cache.find(&2).is_none()); // (4, 1) (no change)
|
|
||||||
assert!(cache.find(&3).is_none()); // (4, 1) (no change)
|
|
||||||
assert!(cache.find(&4).is_some()); // (1, 4)
|
|
||||||
}
|
|
|
@ -23,7 +23,6 @@ extern crate test;
|
||||||
|
|
||||||
mod animated_properties;
|
mod animated_properties;
|
||||||
mod attr;
|
mod attr;
|
||||||
mod cache;
|
|
||||||
mod keyframes;
|
mod keyframes;
|
||||||
mod logical_geometry;
|
mod logical_geometry;
|
||||||
mod media_queries;
|
mod media_queries;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue