diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-07-08 07:46:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-08 07:46:00 -0700 |
commit | d475a277eebaa7d91d1b070b169e205b4c533c4f (patch) | |
tree | 859dad73b11839931d5995645479e7a18482b71b | |
parent | 1fabfee27e16a237d8a6a6b2e4aaad5e67311104 (diff) | |
parent | e7f9959ad96c51afdd6b5e74e2285f67b1bda228 (diff) | |
download | servo-d475a277eebaa7d91d1b070b169e205b4c533c4f.tar.gz servo-d475a277eebaa7d91d1b070b169e205b4c533c4f.zip |
Auto merge of #12311 - Ms2ger:cache, r=nox
Move the cache module out of util.
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12311)
<!-- Reviewable:end -->
-rw-r--r-- | components/gfx/font.rs | 16 | ||||
-rw-r--r-- | components/servo/Cargo.lock | 1 | ||||
-rw-r--r-- | components/style/animation.rs | 2 | ||||
-rw-r--r-- | components/style/cache.rs (renamed from components/util/cache.rs) | 53 | ||||
-rw-r--r-- | components/style/lib.rs | 1 | ||||
-rw-r--r-- | components/style/matching.rs | 2 | ||||
-rw-r--r-- | components/util/Cargo.toml | 1 | ||||
-rw-r--r-- | components/util/lib.rs | 2 | ||||
-rw-r--r-- | ports/cef/Cargo.lock | 1 | ||||
-rw-r--r-- | ports/geckolib/Cargo.lock | 1 | ||||
-rw-r--r-- | tests/unit/style/cache.rs (renamed from tests/unit/util/cache.rs) | 15 | ||||
-rw-r--r-- | tests/unit/style/lib.rs | 1 | ||||
-rw-r--r-- | tests/unit/util/lib.rs | 1 |
13 files changed, 16 insertions, 81 deletions
diff --git a/components/gfx/font.rs b/components/gfx/font.rs index c401c616d8f..e92fff1c58f 100644 --- a/components/gfx/font.rs +++ b/components/gfx/font.rs @@ -12,6 +12,7 @@ use smallvec::SmallVec; use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::cell::RefCell; +use std::collections::HashMap; use std::rc::Rc; use std::str; use std::sync::Arc; @@ -22,7 +23,6 @@ use text::glyph::{ByteIndex, GlyphData, GlyphId, GlyphStore}; use text::shaping::ShaperMethods; use time; use unicode_script::Script; -use util::cache::HashCache; use webrender_traits; macro_rules! ot_tag { @@ -109,8 +109,8 @@ pub struct Font { pub requested_pt_size: Au, pub actual_pt_size: Au, shaper: Option<Shaper>, - shape_cache: RefCell<HashCache<ShapeCacheEntry, Arc<GlyphStore>>>, - glyph_advance_cache: RefCell<HashCache<u32, FractionalPixel>>, + shape_cache: RefCell<HashMap<ShapeCacheEntry, Arc<GlyphStore>>>, + glyph_advance_cache: RefCell<HashMap<u32, FractionalPixel>>, pub font_key: Option<webrender_traits::FontKey>, } @@ -130,8 +130,8 @@ impl Font { requested_pt_size: requested_pt_size, actual_pt_size: actual_pt_size, metrics: metrics, - shape_cache: RefCell::new(HashCache::new()), - glyph_advance_cache: RefCell::new(HashCache::new()), + shape_cache: RefCell::new(HashMap::new()), + glyph_advance_cache: RefCell::new(HashMap::new()), font_key: font_key, } } @@ -180,7 +180,7 @@ impl Font { text: text.to_owned(), options: *options, }; - let result = self.shape_cache.borrow_mut().find_or_create(lookup_key, || { + let result = self.shape_cache.borrow_mut().entry(lookup_key).or_insert_with(|| { let start_time = time::precise_time_ns(); let mut glyphs = GlyphStore::new(text.len(), options.flags.contains(IS_WHITESPACE_SHAPING_FLAG), @@ -201,7 +201,7 @@ impl Font { TEXT_SHAPING_PERFORMANCE_COUNTER.fetch_add((end_time - start_time) as usize, Ordering::Relaxed); Arc::new(glyphs) - }); + }).clone(); self.shaper = shaper; result } @@ -269,7 +269,7 @@ impl Font { } pub fn glyph_h_advance(&self, glyph: GlyphId) -> FractionalPixel { - self.glyph_advance_cache.borrow_mut().find_or_create(glyph, || { + *self.glyph_advance_cache.borrow_mut().entry(glyph).or_insert_with(|| { match self.handle.glyph_h_advance(glyph) { Some(adv) => adv, None => 10f64 as FractionalPixel // FIXME: Need fallback strategy diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 5f6e0da6de8..5f2d29cee62 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -2466,7 +2466,6 @@ dependencies = [ "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/style/animation.rs b/components/style/animation.rs index 1f0cbcaefde..9d8b88f0c1f 100644 --- a/components/style/animation.rs +++ b/components/style/animation.rs @@ -498,7 +498,7 @@ where Impl: SelectorImplExt, debug!("update_style_for_animation: entering"); debug_assert!(!animation.is_expired()); match *animation { - Animation::Transition(_, start_time, ref frame, expired) => { + Animation::Transition(_, start_time, ref frame, _) => { debug!("update_style_for_animation: transition found"); let now = time::precise_time_s(); let mut new_style = (*style).clone(); diff --git a/components/util/cache.rs b/components/style/cache.rs index f446f43b498..e15c99eea2c 100644 --- a/components/util/cache.rs +++ b/components/style/cache.rs @@ -2,60 +2,13 @@ * 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/. */ +//! Two simple cache data structures. + use rand; use rand::Rng; -use std::collections::HashMap; -use std::collections::hash_map::Entry::{Occupied, Vacant}; -use std::default::Default; -use std::hash::{BuildHasherDefault, Hash, Hasher, SipHasher}; +use std::hash::{Hash, Hasher, SipHasher}; use std::slice::Iter; - -#[derive(Debug)] -pub struct HashCache<K, V> - where K: PartialEq + Eq + Hash, - V: Clone, -{ - entries: HashMap<K, V, BuildHasherDefault<SipHasher>>, -} - -impl<K, V> HashCache<K, V> - where K: PartialEq + Eq + Hash, - V: Clone, -{ - pub fn new() -> HashCache<K, V> { - HashCache { - entries: HashMap::with_hasher(Default::default()), - } - } - - pub fn insert(&mut self, key: K, value: V) { - self.entries.insert(key, value); - } - - pub fn find(&self, key: &K) -> Option<V> { - match self.entries.get(key) { - Some(v) => Some(v.clone()), - None => None, - } - } - - 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())).clone() - } - } - } - - pub fn evict_all(&mut self) { - self.entries.clear(); - } -} - pub struct LRUCache<K, V> { entries: Vec<(K, V)>, cache_size: usize, diff --git a/components/style/lib.rs b/components/style/lib.rs index 6e45d9a9411..7fe5fe1bcca 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -72,6 +72,7 @@ extern crate util; pub mod animation; pub mod attr; pub mod bezier; +pub mod cache; pub mod context; pub mod custom_properties; pub mod data; diff --git a/components/style/matching.rs b/components/style/matching.rs index 5d4fa82b8ce..8b843844fbc 100644 --- a/components/style/matching.rs +++ b/components/style/matching.rs @@ -7,6 +7,7 @@ #![allow(unsafe_code)] use animation::{self, Animation}; +use cache::{LRUCache, SimpleHashCache}; use context::{StyleContext, SharedStyleContext}; use data::PrivateStyleData; use dom::{TElement, TNode, TRestyleDamage}; @@ -25,7 +26,6 @@ use std::slice::Iter; use std::sync::Arc; use string_cache::{Atom, Namespace}; use util::arc_ptr_eq; -use util::cache::{LRUCache, SimpleHashCache}; use util::opts; fn create_common_style_affecting_attributes_from_element<E: TElement>(element: &E) diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index f5c87179cfc..3a9478a49b3 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -24,7 +24,6 @@ ipc-channel = {git = "https://github.com/servo/ipc-channel", optional = true} lazy_static = "0.2" log = "0.3.5" num_cpus = "0.2.2" -rand = "0.3" rustc-serialize = "0.3" serde = {version = "0.7.11", optional = true} serde_macros = {version = "0.7.11", optional = true} diff --git a/components/util/lib.rs b/components/util/lib.rs index 6ffa4a4a2b5..bcad9ea3705 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -20,7 +20,6 @@ extern crate getopts; #[allow(unused_extern_crates)] #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate num_cpus; -extern crate rand; extern crate rustc_serialize; #[cfg(feature = "servo")] extern crate serde; extern crate url; @@ -30,7 +29,6 @@ extern crate xdg; use std::sync::Arc; pub mod basedir; -pub mod cache; pub mod geometry; #[cfg(feature = "servo")] #[allow(unsafe_code)] pub mod ipc; #[allow(unsafe_code)] pub mod opts; diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 471c3a6eb6d..b8258ccb8df 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -2335,7 +2335,6 @@ dependencies = [ "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/geckolib/Cargo.lock b/ports/geckolib/Cargo.lock index 1bb5d71923d..bec14181271 100644 --- a/ports/geckolib/Cargo.lock +++ b/ports/geckolib/Cargo.lock @@ -573,7 +573,6 @@ dependencies = [ "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/tests/unit/util/cache.rs b/tests/unit/style/cache.rs index 9bb207622bd..7456a87b55c 100644 --- a/tests/unit/util/cache.rs +++ b/tests/unit/style/cache.rs @@ -3,20 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::cell::Cell; -use util::cache::{HashCache, LRUCache}; - -#[test] -fn test_hashcache() { - let mut cache: HashCache<usize, Cell<&str>> = HashCache::new(); - - cache.insert(1, Cell::new("one")); - assert!(cache.find(&1).is_some()); - assert!(cache.find(&2).is_none()); - - cache.find_or_create(2, || { Cell::new("two") }); - assert!(cache.find(&1).is_some()); - assert!(cache.find(&2).is_some()); -} +use style::cache::LRUCache; #[test] fn test_lru_cache() { diff --git a/tests/unit/style/lib.rs b/tests/unit/style/lib.rs index fd725d79635..803dd7ce05e 100644 --- a/tests/unit/style/lib.rs +++ b/tests/unit/style/lib.rs @@ -18,6 +18,7 @@ extern crate url; extern crate util; mod attr; +mod cache; mod logical_geometry; mod media_queries; mod properties; diff --git a/tests/unit/util/lib.rs b/tests/unit/util/lib.rs index af5de45579d..1ad4e55d965 100644 --- a/tests/unit/util/lib.rs +++ b/tests/unit/util/lib.rs @@ -6,7 +6,6 @@ extern crate util; -mod cache; mod opts; mod prefs; mod thread; |