mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Add HashCache and switch all caches from Copy to Clone
This commit is contained in:
parent
4b172a312d
commit
39c3a6ff1d
3 changed files with 81 additions and 31 deletions
|
@ -86,7 +86,7 @@ pub struct FontMetrics {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(Issue #200): use enum from CSS bindings for 'font-weight'
|
// TODO(Issue #200): use enum from CSS bindings for 'font-weight'
|
||||||
#[deriving(Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub enum CSSFontWeight {
|
pub enum CSSFontWeight {
|
||||||
FontWeight100,
|
FontWeight100,
|
||||||
FontWeight200,
|
FontWeight200,
|
||||||
|
@ -114,7 +114,7 @@ impl CSSFontWeight {
|
||||||
// the instance's properties.
|
// the instance's properties.
|
||||||
//
|
//
|
||||||
// For now, the cases are differentiated with a typedef
|
// For now, the cases are differentiated with a typedef
|
||||||
#[deriving(Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub struct FontStyle {
|
pub struct FontStyle {
|
||||||
pt_size: float,
|
pt_size: float,
|
||||||
weight: CSSFontWeight,
|
weight: CSSFontWeight,
|
||||||
|
@ -139,7 +139,7 @@ struct ResolvedFont {
|
||||||
// It's used to swizzle/unswizzle gfx::Font instances when
|
// It's used to swizzle/unswizzle gfx::Font instances when
|
||||||
// communicating across tasks, such as the display list between layout
|
// communicating across tasks, such as the display list between layout
|
||||||
// and render tasks.
|
// and render tasks.
|
||||||
#[deriving(Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub struct FontDescriptor {
|
pub struct FontDescriptor {
|
||||||
style: UsedFontStyle,
|
style: UsedFontStyle,
|
||||||
selector: FontSelector,
|
selector: FontSelector,
|
||||||
|
@ -155,7 +155,7 @@ impl FontDescriptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// A FontSelector is a platform-specific strategy for serializing face names.
|
// A FontSelector is a platform-specific strategy for serializing face names.
|
||||||
#[deriving(Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub enum FontSelector {
|
pub enum FontSelector {
|
||||||
SelectorPlatformIdentifier(~str),
|
SelectorPlatformIdentifier(~str),
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@ use font::{Font, FontDescriptor, FontGroup, FontHandleMethods, FontStyle,
|
||||||
SelectorPlatformIdentifier};
|
SelectorPlatformIdentifier};
|
||||||
use font::{SpecifiedFontStyle, UsedFontStyle};
|
use font::{SpecifiedFontStyle, UsedFontStyle};
|
||||||
use font_list::FontList;
|
use font_list::FontList;
|
||||||
use servo_util::cache::Cache;
|
use servo_util::cache::{Cache, LRUCache};
|
||||||
use servo_util::cache::LRUCache;
|
|
||||||
use servo_util::time::ProfilerChan;
|
use servo_util::time::ProfilerChan;
|
||||||
|
|
||||||
use platform::font::FontHandle;
|
use platform::font::FontHandle;
|
||||||
|
@ -90,7 +89,7 @@ impl<'self> FontContext {
|
||||||
None => {
|
None => {
|
||||||
debug!("font group cache miss");
|
debug!("font group cache miss");
|
||||||
let fg = self.create_font_group(style);
|
let fg = self.create_font_group(style);
|
||||||
self.group_cache.insert(style, fg);
|
self.group_cache.insert(style.clone(), fg);
|
||||||
fg
|
fg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +106,7 @@ impl<'self> FontContext {
|
||||||
let result = self.create_font_instance(desc);
|
let result = self.create_font_instance(desc);
|
||||||
match result {
|
match result {
|
||||||
Ok(font) => {
|
Ok(font) => {
|
||||||
self.instance_cache.insert(desc, font);
|
self.instance_cache.insert(desc.clone(), font);
|
||||||
}, _ => {}
|
}, _ => {}
|
||||||
};
|
};
|
||||||
result
|
result
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
pub trait Cache<K: Copy + Eq, V: Copy> {
|
use std::hashmap::HashMap;
|
||||||
fn insert(&mut self, key: &K, value: V);
|
|
||||||
|
pub trait Cache<K: Eq, V: Clone> {
|
||||||
|
fn insert(&mut self, key: K, value: V);
|
||||||
fn find(&mut self, key: &K) -> Option<V>;
|
fn find(&mut self, key: &K) -> Option<V>;
|
||||||
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V;
|
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V;
|
||||||
fn evict_all(&mut self);
|
fn evict_all(&mut self);
|
||||||
|
@ -13,34 +15,35 @@ pub struct MonoCache<K, V> {
|
||||||
entry: Option<(K,V)>,
|
entry: Option<(K,V)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Copy + Eq, V: Copy> MonoCache<K,V> {
|
impl<K: Clone + Eq, V: Clone> MonoCache<K,V> {
|
||||||
pub fn new(_size: uint) -> MonoCache<K,V> {
|
pub fn new(_size: uint) -> MonoCache<K,V> {
|
||||||
MonoCache { entry: None }
|
MonoCache { entry: None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Copy + Eq, V: Copy> Cache<K,V> for MonoCache<K,V> {
|
impl<K: Clone + Eq, V: Clone> Cache<K,V> for MonoCache<K,V> {
|
||||||
fn insert(&mut self, key: &K, value: V) {
|
fn insert(&mut self, key: K, value: V) {
|
||||||
self.entry = Some((copy *key, value));
|
self.entry = Some((key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find(&mut self, key: &K) -> Option<V> {
|
fn find(&mut self, key: &K) -> Option<V> {
|
||||||
match self.entry {
|
match self.entry {
|
||||||
None => None,
|
None => None,
|
||||||
Some((ref k, ref v)) => if *k == *key { Some(copy *v) } else { None }
|
Some((ref k, ref v)) => if *k == *key { Some(v.clone()) } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
|
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
|
||||||
return match self.find(key) {
|
match self.entry {
|
||||||
None => {
|
None => {
|
||||||
let value = blk(key);
|
let value = blk(key);
|
||||||
self.entry = Some((copy *key, copy value));
|
self.entry = Some((key.clone(), value.clone()));
|
||||||
value
|
value
|
||||||
},
|
},
|
||||||
Some(v) => v
|
Some((ref _k, ref v)) => v.clone()
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evict_all(&mut self) {
|
fn evict_all(&mut self) {
|
||||||
self.entry = None;
|
self.entry = None;
|
||||||
}
|
}
|
||||||
|
@ -60,12 +63,60 @@ fn test_monocache() {
|
||||||
assert!(cache.find(&1).is_none());
|
assert!(cache.find(&1).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct HashCache<K, V> {
|
||||||
|
entries: HashMap<K, V>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: Clone + Eq + Hash, V: Clone> HashCache<K,V> {
|
||||||
|
pub fn new() -> HashCache<K, V> {
|
||||||
|
HashCache {
|
||||||
|
entries: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: Clone + Eq + Hash, V: Clone> Cache<K,V> for HashCache<K,V> {
|
||||||
|
fn insert(&mut self, key: K, value: V) {
|
||||||
|
self.entries.insert(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find(&mut self, key: &K) -> Option<V> {
|
||||||
|
match self.entries.find(key) {
|
||||||
|
Some(v) => Some(v.clone()),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
|
||||||
|
self.entries.find_or_insert_with(key.clone(), blk).clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evict_all(&mut self) {
|
||||||
|
self.entries.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hashcache() {
|
||||||
|
let cache = HashCache::new();
|
||||||
|
let one = @"one";
|
||||||
|
let two = @"two";
|
||||||
|
|
||||||
|
cache.insert(&1, one);
|
||||||
|
assert!(cache.find(&1).is_some());
|
||||||
|
assert!(cache.find(&2).is_none());
|
||||||
|
|
||||||
|
cache.find_or_create(&2, |_v| { two });
|
||||||
|
assert!(cache.find(&1).is_some());
|
||||||
|
assert!(cache.find(&2).is_some());
|
||||||
|
}
|
||||||
|
|
||||||
pub struct LRUCache<K, V> {
|
pub struct LRUCache<K, V> {
|
||||||
entries: ~[(K, V)],
|
entries: ~[(K, V)],
|
||||||
cache_size: uint,
|
cache_size: uint,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Copy + Eq, V: Copy> LRUCache<K,V> {
|
impl<K: Clone + Eq, V: Clone> LRUCache<K,V> {
|
||||||
pub fn new(size: uint) -> LRUCache<K, V> {
|
pub fn new(size: uint) -> LRUCache<K, V> {
|
||||||
LRUCache {
|
LRUCache {
|
||||||
entries: ~[],
|
entries: ~[],
|
||||||
|
@ -74,21 +125,21 @@ impl<K: Copy + Eq, V: Copy> LRUCache<K,V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn touch(&mut self, pos: uint) -> V {
|
pub fn touch(&mut self, pos: uint) -> V {
|
||||||
let (key, val) = copy self.entries[pos];
|
let last_index = self.entries.len() - 1;
|
||||||
if pos != self.cache_size {
|
if pos != last_index {
|
||||||
self.entries.remove(pos);
|
let entry = self.entries.remove(pos);
|
||||||
self.entries.push((key, copy val));
|
self.entries.push(entry);
|
||||||
}
|
}
|
||||||
val
|
self.entries[last_index].second_ref().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Copy + Eq, V: Copy> Cache<K,V> for LRUCache<K,V> {
|
impl<K: Clone + Eq, V: Clone> Cache<K,V> for LRUCache<K,V> {
|
||||||
fn insert(&mut self, key: &K, val: V) {
|
fn insert(&mut self, key: K, val: V) {
|
||||||
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((copy *key, val));
|
self.entries.push((key, val));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find(&mut self, key: &K) -> Option<V> {
|
fn find(&mut self, key: &K) -> Option<V> {
|
||||||
|
@ -102,9 +153,9 @@ impl<K: Copy + Eq, V: Copy> Cache<K,V> for LRUCache<K,V> {
|
||||||
match self.entries.position(|&(k, _)| k == *key) {
|
match self.entries.position(|&(k, _)| k == *key) {
|
||||||
Some(pos) => self.touch(pos),
|
Some(pos) => self.touch(pos),
|
||||||
None => {
|
None => {
|
||||||
let val = blk(key);
|
let val = blk(key);
|
||||||
self.insert(key, copy val);
|
self.insert(key.clone(), val.clone());
|
||||||
val
|
val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue