Skip to content

Commit

Permalink
style: Convert FnvHash{Set,Map} instances to FxHash{Set,Map}.
Browse files Browse the repository at this point in the history
Bug: 1477628
Reviewed-by: heycam
  • Loading branch information
nnethercote authored and emilio committed Aug 7, 2018
1 parent 0cab212 commit fc9df0b
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 44 deletions.
1 change: 1 addition & 0 deletions components/selectors/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ matches = "0.1"
cssparser = "0.24.0"
log = "0.4"
fnv = "1.0"
fxhash = "0.2"
phf = "0.7.18"
precomputed-hash = "0.1"
servo_arc = { version = "0.1", path = "../servo_arc" }
Expand Down
3 changes: 3 additions & 0 deletions components/selectors/bloom.rs
Expand Up @@ -297,6 +297,9 @@ impl Clone for BloomStorageBool {
}

fn hash<T: Hash>(elem: &T) -> u32 {
// We generally use FxHasher in Stylo because it's faster than FnvHasher,
// but the increased collision rate has outsized effect on the bloom
// filter, so we use FnvHasher instead here.
let mut hasher = FnvHasher::default();
elem.hash(&mut hasher);
let hash: u64 = hasher.finish();
Expand Down
1 change: 1 addition & 0 deletions components/selectors/lib.rs
Expand Up @@ -10,6 +10,7 @@ extern crate bitflags;
#[macro_use]
extern crate cssparser;
extern crate fnv;
extern crate fxhash;
#[macro_use]
extern crate log;
#[macro_use]
Expand Down
4 changes: 2 additions & 2 deletions components/selectors/nth_index_cache.rs
Expand Up @@ -2,7 +2,7 @@
* 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 fnv::FnvHashMap;
use fxhash::FxHashMap;
use tree::OpaqueElement;

/// A cache to speed up matching of nth-index-like selectors.
Expand Down Expand Up @@ -32,7 +32,7 @@ impl NthIndexCache {

/// The concrete per-pseudo-class cache.
#[derive(Default)]
pub struct NthIndexCacheInner(FnvHashMap<OpaqueElement, i32>);
pub struct NthIndexCacheInner(FxHashMap<OpaqueElement, i32>);

impl NthIndexCacheInner {
/// Does a lookup for a given element in the cache.
Expand Down
2 changes: 1 addition & 1 deletion components/style/Cargo.toml
Expand Up @@ -36,7 +36,7 @@ new_debug_unreachable = "1.0"
encoding_rs = {version = "0.7", optional = true}
euclid = "0.19"
fallible = { path = "../fallible" }
fnv = "1.0"
fxhash = "0.2"
hashglobe = { path = "../hashglobe" }
html5ever = {version = "0.22", optional = true}
itertools = "0.7.6"
Expand Down
12 changes: 6 additions & 6 deletions components/style/context.rs
Expand Up @@ -14,7 +14,7 @@ use dom::{SendElement, TElement};
use dom::OpaqueNode;
use euclid::Size2D;
use euclid::TypedScale;
use fnv::FnvHashMap;
use fxhash::FxHashMap;
use font_metrics::FontMetricsProvider;
#[cfg(feature = "gecko")]
use gecko_bindings::structs;
Expand Down Expand Up @@ -173,11 +173,11 @@ pub struct SharedStyleContext<'a> {

/// The animations that are currently running.
#[cfg(feature = "servo")]
pub running_animations: Arc<RwLock<FnvHashMap<OpaqueNode, Vec<Animation>>>>,
pub running_animations: Arc<RwLock<FxHashMap<OpaqueNode, Vec<Animation>>>>,

/// The list of animations that have expired since the last style recalculation.
#[cfg(feature = "servo")]
pub expired_animations: Arc<RwLock<FnvHashMap<OpaqueNode, Vec<Animation>>>>,
pub expired_animations: Arc<RwLock<FxHashMap<OpaqueNode, Vec<Animation>>>>,

/// Paint worklets
#[cfg(feature = "servo")]
Expand Down Expand Up @@ -570,7 +570,7 @@ type CacheItem<E> = (SendElement<E>, ElementSelectorFlags);
/// flags until after the traversal.
pub struct SelectorFlagsMap<E: TElement> {
/// The hashmap storing the flags to apply.
map: FnvHashMap<SendElement<E>, ElementSelectorFlags>,
map: FxHashMap<SendElement<E>, ElementSelectorFlags>,
/// An LRU cache to avoid hashmap lookups, which can be slow if the map
/// gets big.
cache: LRUCache<[Entry<CacheItem<E>>; 4 + 1]>,
Expand All @@ -587,7 +587,7 @@ impl<E: TElement> SelectorFlagsMap<E> {
/// Creates a new empty SelectorFlagsMap.
pub fn new() -> Self {
SelectorFlagsMap {
map: FnvHashMap::default(),
map: FxHashMap::default(),
cache: LRUCache::default(),
}
}
Expand Down Expand Up @@ -833,7 +833,7 @@ pub trait RegisteredSpeculativePainter: SpeculativePainter {
/// The name it was registered with
fn name(&self) -> Atom;
/// The properties it was registered with
fn properties(&self) -> &FnvHashMap<Atom, PropertyId>;
fn properties(&self) -> &FxHashMap<Atom, PropertyId>;
}

/// A set of registered painters
Expand Down
8 changes: 4 additions & 4 deletions components/style/gecko/wrapper.rs
Expand Up @@ -62,7 +62,7 @@ use gecko_bindings::structs::nsChangeHint;
use gecko_bindings::structs::nsIDocument_DocumentTheme as DocumentTheme;
use gecko_bindings::structs::nsRestyleHint;
use gecko_bindings::sugar::ownership::{HasArcFFI, HasSimpleFFI};
use hash::FnvHashMap;
use hash::FxHashMap;
use logical_geometry::WritingMode;
use media_queries::Device;
use properties::{ComputedValues, LonghandId};
Expand Down Expand Up @@ -867,12 +867,12 @@ impl<'le> GeckoElement<'le> {
}
}

fn css_transitions_info(&self) -> FnvHashMap<LonghandId, Arc<AnimationValue>> {
fn css_transitions_info(&self) -> FxHashMap<LonghandId, Arc<AnimationValue>> {
use gecko_bindings::bindings::Gecko_ElementTransitions_EndValueAt;
use gecko_bindings::bindings::Gecko_ElementTransitions_Length;

let collection_length = unsafe { Gecko_ElementTransitions_Length(self.0) } as usize;
let mut map = FnvHashMap::with_capacity_and_hasher(collection_length, Default::default());
let mut map = FxHashMap::with_capacity_and_hasher(collection_length, Default::default());

for i in 0..collection_length {
let raw_end_value = unsafe { Gecko_ElementTransitions_EndValueAt(self.0, i) };
Expand All @@ -893,7 +893,7 @@ impl<'le> GeckoElement<'le> {
combined_duration: f32,
before_change_style: &ComputedValues,
after_change_style: &ComputedValues,
existing_transitions: &FnvHashMap<LonghandId, Arc<AnimationValue>>,
existing_transitions: &FxHashMap<LonghandId, Arc<AnimationValue>>,
) -> bool {
use values::animated::{Animate, Procedure};
debug_assert!(!longhand_id.is_logical());
Expand Down
10 changes: 5 additions & 5 deletions components/style/hash.rs
Expand Up @@ -7,7 +7,7 @@
//! Can go away when the stdlib gets fallible collections
//! https://github.com/rust-lang/rfcs/pull/2116

use fnv;
use fxhash;

#[cfg(feature = "gecko")]
pub use hashglobe::hash_map::HashMap;
Expand All @@ -25,7 +25,7 @@ pub mod map {
pub use std::collections::hash_map::{Entry, Iter};
}

/// Hash map that uses the FNV hasher
pub type FnvHashMap<K, V> = HashMap<K, V, fnv::FnvBuildHasher>;
/// Hash set that uses the FNV hasher
pub type FnvHashSet<T> = HashSet<T, fnv::FnvBuildHasher>;
/// Hash map that uses the Fx hasher
pub type FxHashMap<K, V> = HashMap<K, V, fxhash::FxBuildHasher>;
/// Hash set that uses the Fx hasher
pub type FxHashSet<T> = HashSet<T, fxhash::FxBuildHasher>;
6 changes: 3 additions & 3 deletions components/style/invalidation/media_queries.rs
Expand Up @@ -5,7 +5,7 @@
//! Code related to the invalidation of media-query-affected rules.

use context::QuirksMode;
use fnv::FnvHashSet;
use fxhash::FxHashSet;
use media_queries::Device;
use shared_lock::SharedRwLockReadGuard;
use stylesheets::{DocumentRule, ImportRule, MediaRule};
Expand Down Expand Up @@ -54,14 +54,14 @@ impl ToMediaListKey for MediaRule {}
#[derive(Debug, MallocSizeOf, PartialEq)]
pub struct EffectiveMediaQueryResults {
/// The set of media lists that matched last time.
set: FnvHashSet<MediaListKey>,
set: FxHashSet<MediaListKey>,
}

impl EffectiveMediaQueryResults {
/// Trivially constructs an empty `EffectiveMediaQueryResults`.
pub fn new() -> Self {
Self {
set: FnvHashSet::default(),
set: FxHashSet::default(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions components/style/invalidation/stylesheets.rs
Expand Up @@ -11,7 +11,7 @@ use Atom;
use CaseSensitivityExt;
use LocalName as SelectorLocalName;
use dom::{TDocument, TElement, TNode};
use fnv::FnvHashSet;
use fxhash::FxHashSet;
use invalidation::element::element_wrapper::{ElementSnapshot, ElementWrapper};
use invalidation::element::restyle_hints::RestyleHint;
use media_queries::Device;
Expand Down Expand Up @@ -106,9 +106,9 @@ impl Invalidation {
#[derive(MallocSizeOf)]
pub struct StylesheetInvalidationSet {
/// The subtrees we know we have to restyle so far.
invalid_scopes: FnvHashSet<Invalidation>,
invalid_scopes: FxHashSet<Invalidation>,
/// The elements we know we have to restyle so far.
invalid_elements: FnvHashSet<Invalidation>,
invalid_elements: FxHashSet<Invalidation>,
/// Whether the whole document should be restyled.
fully_invalid: bool,
}
Expand All @@ -117,8 +117,8 @@ impl StylesheetInvalidationSet {
/// Create an empty `StylesheetInvalidationSet`.
pub fn new() -> Self {
Self {
invalid_scopes: FnvHashSet::default(),
invalid_elements: FnvHashSet::default(),
invalid_scopes: FxHashSet::default(),
invalid_elements: FxHashSet::default(),
fully_invalid: false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/style/lib.rs
Expand Up @@ -42,7 +42,7 @@ extern crate cssparser;
extern crate debug_unreachable;
extern crate euclid;
extern crate fallible;
extern crate fnv;
extern crate fxhash;
#[cfg(feature = "gecko")]
#[macro_use]
pub mod gecko_string_cache;
Expand Down
Expand Up @@ -25,7 +25,7 @@ use servo_arc::Arc;
use smallvec::SmallVec;
use std::{cmp, ptr};
use std::mem::{self, ManuallyDrop};
use hash::FnvHashMap;
use hash::FxHashMap;
use super::ComputedValues;
use values::CSSFloat;
use values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero};
Expand Down Expand Up @@ -232,7 +232,7 @@ impl AnimatedProperty {
/// A collection of AnimationValue that were composed on an element.
/// This HashMap stores the values that are the last AnimationValue to be
/// composed for each TransitionProperty.
pub type AnimationValueMap = FnvHashMap<LonghandId, AnimationValue>;
pub type AnimationValueMap = FxHashMap<LonghandId, AnimationValue>;

#[cfg(feature = "gecko")]
unsafe impl HasFFI for AnimationValueMap {
Expand Down
4 changes: 2 additions & 2 deletions components/style/properties/properties.mako.rs
Expand Up @@ -2387,7 +2387,7 @@ pub use gecko_properties::style_structs;
/// The module where all the style structs are defined.
#[cfg(feature = "servo")]
pub mod style_structs {
use fnv::FnvHasher;
use fx::FxHasher;
use super::longhands;
use std::hash::{Hash, Hasher};
use logical_geometry::WritingMode;
Expand Down Expand Up @@ -2534,7 +2534,7 @@ pub mod style_structs {
pub fn compute_font_hash(&mut self) {
// Corresponds to the fields in
// `gfx::font_template::FontTemplateDescriptor`.
let mut hasher: FnvHasher = Default::default();
let mut hasher: FxHasher = Default::default();
self.font_weight.hash(&mut hasher);
self.font_stretch.hash(&mut hasher);
self.font_style.hash(&mut hasher);
Expand Down
6 changes: 3 additions & 3 deletions components/style/rule_cache.rs
Expand Up @@ -5,7 +5,7 @@
//! A cache from rule node to computed values, in order to cache reset
//! properties.

use fnv::FnvHashMap;
use fxhash::FxHashMap;
use logical_geometry::WritingMode;
use properties::{ComputedValues, StyleBuilder};
use rule_tree::StrongRuleNode;
Expand Down Expand Up @@ -71,14 +71,14 @@ impl RuleCacheConditions {
/// A TLS cache from rules matched to computed values.
pub struct RuleCache {
// FIXME(emilio): Consider using LRUCache or something like that?
map: FnvHashMap<StrongRuleNode, SmallVec<[(RuleCacheConditions, Arc<ComputedValues>); 1]>>,
map: FxHashMap<StrongRuleNode, SmallVec<[(RuleCacheConditions, Arc<ComputedValues>); 1]>>,
}

impl RuleCache {
/// Creates an empty `RuleCache`.
pub fn new() -> Self {
Self {
map: FnvHashMap::default(),
map: FxHashMap::default(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/style/rule_tree/mod.rs
Expand Up @@ -1146,13 +1146,13 @@ impl StrongRuleNode {

unsafe fn assert_free_list_has_no_duplicates_or_null(&self) {
assert!(cfg!(debug_assertions), "This is an expensive check!");
use hash::FnvHashSet;
use hash::FxHashSet;

let me = &*self.ptr();
assert!(me.is_root());

let mut current = self.ptr();
let mut seen = FnvHashSet::default();
let mut seen = FxHashSet::default();
while current != FREE_LIST_SENTINEL {
let next = (*current).next_free.load(Ordering::Relaxed);
assert!(!next.is_null());
Expand Down
8 changes: 4 additions & 4 deletions components/style/servo/selector_parser.rs
Expand Up @@ -11,7 +11,7 @@ use attr::{AttrIdentifier, AttrValue};
use cssparser::{serialize_identifier, CowRcStr, Parser as CssParser, SourceLocation, ToCss};
use dom::{OpaqueNode, TElement, TNode};
use element_state::{DocumentState, ElementState};
use fnv::FnvHashMap;
use fxhash::FxHashMap;
use invalidation::element::document_state::InvalidationMatchingData;
use invalidation::element::element_wrapper::ElementSnapshot;
use properties::{ComputedValues, PropertyFlags};
Expand Down Expand Up @@ -617,12 +617,12 @@ impl SelectorImpl {

/// A map from elements to snapshots for the Servo style back-end.
#[derive(Debug)]
pub struct SnapshotMap(FnvHashMap<OpaqueNode, ServoElementSnapshot>);
pub struct SnapshotMap(FxHashMap<OpaqueNode, ServoElementSnapshot>);

impl SnapshotMap {
/// Create a new empty `SnapshotMap`.
pub fn new() -> Self {
SnapshotMap(FnvHashMap::default())
SnapshotMap(FxHashMap::default())
}

/// Get a snapshot given an element.
Expand All @@ -632,7 +632,7 @@ impl SnapshotMap {
}

impl Deref for SnapshotMap {
type Target = FnvHashMap<OpaqueNode, ServoElementSnapshot>;
type Target = FxHashMap<OpaqueNode, ServoElementSnapshot>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
4 changes: 2 additions & 2 deletions components/style/stylesheets/stylesheet.rs
Expand Up @@ -7,7 +7,7 @@ use context::QuirksMode;
use cssparser::{Parser, ParserInput, RuleListParser};
use error_reporting::{ContextualParseError, ParseErrorReporter};
use fallible::FallibleVec;
use fnv::FnvHashMap;
use fxhash::FxHashMap;
use invalidation::media_queries::{MediaListKey, ToMediaListKey};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct UserAgentStylesheets {
#[allow(missing_docs)]
pub struct Namespaces {
pub default: Option<Namespace>,
pub prefixes: FnvHashMap<Prefix, Namespace>,
pub prefixes: FxHashMap<Prefix, Namespace>,
}

/// The contents of a given stylesheet. This effectively maps to a
Expand Down
4 changes: 2 additions & 2 deletions components/style/values/specified/position.rs
Expand Up @@ -8,7 +8,7 @@
//! [position]: https://drafts.csswg.org/css-backgrounds-3/#position

use cssparser::Parser;
use hash::FnvHashMap;
use hash::FxHashMap;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
Expand Down Expand Up @@ -548,7 +548,7 @@ impl TemplateAreas {
let mut width = 0;
{
let mut row = 0u32;
let mut area_indices = FnvHashMap::<&str, usize>::default();
let mut area_indices = FxHashMap::<&str, usize>::default();
for string in &strings {
let mut current_area_index: Option<usize> = None;
row += 1;
Expand Down

0 comments on commit fc9df0b

Please sign in to comment.