mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Eliminate unneeded clones in find_or_create
...and use it to eliminate duplicate hash lookups and string copies in shape_text.
This commit is contained in:
parent
1c5ec6f3ec
commit
d2717c4475
3 changed files with 34 additions and 41 deletions
|
@ -165,31 +165,25 @@ impl Font {
|
|||
//FIXME: find the equivalent of Equiv and the old ShapeCacheEntryRef
|
||||
let shaper = &self.shaper;
|
||||
let lookup_key = ShapeCacheEntry {
|
||||
text: text.to_owned(),
|
||||
options: options.clone(),
|
||||
};
|
||||
if let Some(glyphs) = self.shape_cache.find(&lookup_key) {
|
||||
return glyphs.clone();
|
||||
}
|
||||
|
||||
let start_time = time::precise_time_ns();
|
||||
|
||||
let mut glyphs = GlyphStore::new(text.len(),
|
||||
options.flags.contains(IS_WHITESPACE_SHAPING_FLAG),
|
||||
options.flags.contains(RTL_FLAG));
|
||||
shaper.as_ref().unwrap().shape_text(text, options, &mut glyphs);
|
||||
|
||||
let glyphs = Arc::new(glyphs);
|
||||
self.shape_cache.insert(ShapeCacheEntry {
|
||||
text: text.to_owned(),
|
||||
options: *options,
|
||||
}, glyphs.clone());
|
||||
};
|
||||
self.shape_cache.find_or_create(lookup_key, || {
|
||||
let start_time = time::precise_time_ns();
|
||||
|
||||
let end_time = time::precise_time_ns();
|
||||
TEXT_SHAPING_PERFORMANCE_COUNTER.fetch_add((end_time - start_time) as usize,
|
||||
Ordering::Relaxed);
|
||||
let mut glyphs = GlyphStore::new(text.len(),
|
||||
options.flags.contains(IS_WHITESPACE_SHAPING_FLAG),
|
||||
options.flags.contains(RTL_FLAG));
|
||||
shaper.as_ref().unwrap().shape_text(text, options, &mut glyphs);
|
||||
|
||||
glyphs
|
||||
let glyphs = Arc::new(glyphs);
|
||||
|
||||
let end_time = time::precise_time_ns();
|
||||
TEXT_SHAPING_PERFORMANCE_COUNTER.fetch_add((end_time - start_time) as usize,
|
||||
Ordering::Relaxed);
|
||||
|
||||
glyphs
|
||||
})
|
||||
}
|
||||
|
||||
fn make_shaper<'a>(&'a mut self, options: &ShapingOptions) -> &'a Shaper {
|
||||
|
@ -231,8 +225,8 @@ impl Font {
|
|||
|
||||
pub fn glyph_h_advance(&mut self, glyph: GlyphId) -> FractionalPixel {
|
||||
let handle = &self.handle;
|
||||
self.glyph_advance_cache.find_or_create(&glyph, |glyph| {
|
||||
match handle.glyph_h_advance(*glyph) {
|
||||
self.glyph_advance_cache.find_or_create(glyph, || {
|
||||
match handle.glyph_h_advance(glyph) {
|
||||
Some(adv) => adv,
|
||||
None => 10f64 as FractionalPixel // FIXME: Need fallback strategy
|
||||
}
|
||||
|
|
|
@ -13,14 +13,14 @@ use std::slice::Iter;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct HashCache<K, V>
|
||||
where K: Clone + PartialEq + Eq + Hash,
|
||||
where K: PartialEq + Eq + Hash,
|
||||
V: Clone,
|
||||
{
|
||||
entries: HashMap<K, V, BuildHasherDefault<SipHasher>>,
|
||||
}
|
||||
|
||||
impl<K, V> HashCache<K, V>
|
||||
where K: Clone + PartialEq + Eq + Hash,
|
||||
where K: PartialEq + Eq + Hash,
|
||||
V: Clone,
|
||||
{
|
||||
pub fn new() -> HashCache<K, V> {
|
||||
|
@ -40,13 +40,13 @@ impl<K, V> HashCache<K, V>
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V {
|
||||
match self.entries.entry(key.clone()) {
|
||||
pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V {
|
||||
match self.entries.entry(key) {
|
||||
Occupied(occupied) => {
|
||||
(*occupied.get()).clone()
|
||||
}
|
||||
Vacant(vacant) => {
|
||||
(*vacant.insert(blk(key))).clone()
|
||||
(*vacant.insert(blk())).clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ pub struct LRUCache<K, V> {
|
|||
cache_size: usize,
|
||||
}
|
||||
|
||||
impl<K: Clone + PartialEq, V: Clone> LRUCache<K, V> {
|
||||
impl<K: PartialEq, V: Clone> LRUCache<K, V> {
|
||||
pub fn new(size: usize) -> LRUCache<K, V> {
|
||||
LRUCache {
|
||||
entries: vec!(),
|
||||
|
@ -97,12 +97,12 @@ impl<K: Clone + PartialEq, V: Clone> LRUCache<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V {
|
||||
match self.entries.iter().position(|&(ref k, _)| *k == *key) {
|
||||
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),
|
||||
None => {
|
||||
let val = blk(key);
|
||||
self.insert(key.clone(), val.clone());
|
||||
let val = blk();
|
||||
self.insert(key, val.clone());
|
||||
val
|
||||
}
|
||||
}
|
||||
|
@ -154,13 +154,12 @@ impl<K: Clone + Eq + Hash, V: Clone> SimpleHashCache<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_or_create<F>(&mut self, key: &K, blk: F) -> V where F: Fn(&K) -> V {
|
||||
match self.find(key) {
|
||||
Some(value) => return value,
|
||||
None => {}
|
||||
pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V {
|
||||
if let Some(value) = self.find(&key) {
|
||||
return value;
|
||||
}
|
||||
let value = blk(key);
|
||||
self.insert((*key).clone(), value.clone());
|
||||
let value = blk();
|
||||
self.insert(key, value.clone());
|
||||
value
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ fn test_hashcache() {
|
|||
assert!(cache.find(&1).is_some());
|
||||
assert!(cache.find(&2).is_none());
|
||||
|
||||
cache.find_or_create(&2, |_v| { Cell::new("two") });
|
||||
cache.find_or_create(2, || { Cell::new("two") });
|
||||
assert!(cache.find(&1).is_some());
|
||||
assert!(cache.find(&2).is_some());
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ fn test_lru_cache() {
|
|||
assert!(cache.find(&4).is_some()); // (2, 4) (no change)
|
||||
|
||||
// Test find_or_create.
|
||||
cache.find_or_create(&1, |_| { Cell::new("one") }); // (4, 1)
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue