From fc9df0bcf3af1b42c624a8f869168f7648d66f5e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 27 Jul 2018 16:49:04 +1000 Subject: [PATCH] style: Convert FnvHash{Set,Map} instances to FxHash{Set,Map}. Bug: 1477628 Reviewed-by: heycam --- components/selectors/Cargo.toml | 1 + components/selectors/bloom.rs | 3 +++ components/selectors/lib.rs | 1 + components/selectors/nth_index_cache.rs | 4 ++-- components/style/Cargo.toml | 2 +- components/style/context.rs | 12 ++++++------ components/style/gecko/wrapper.rs | 8 ++++---- components/style/hash.rs | 10 +++++----- components/style/invalidation/media_queries.rs | 6 +++--- components/style/invalidation/stylesheets.rs | 10 +++++----- components/style/lib.rs | 2 +- .../properties/helpers/animated_properties.mako.rs | 4 ++-- components/style/properties/properties.mako.rs | 4 ++-- components/style/rule_cache.rs | 6 +++--- components/style/rule_tree/mod.rs | 4 ++-- components/style/servo/selector_parser.rs | 8 ++++---- components/style/stylesheets/stylesheet.rs | 4 ++-- components/style/values/specified/position.rs | 4 ++-- 18 files changed, 49 insertions(+), 44 deletions(-) diff --git a/components/selectors/Cargo.toml b/components/selectors/Cargo.toml index e8b4fbe2fe3..3663fcb7689 100644 --- a/components/selectors/Cargo.toml +++ b/components/selectors/Cargo.toml @@ -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" } diff --git a/components/selectors/bloom.rs b/components/selectors/bloom.rs index e4cd0a77f98..16dabf498f9 100644 --- a/components/selectors/bloom.rs +++ b/components/selectors/bloom.rs @@ -297,6 +297,9 @@ impl Clone for BloomStorageBool { } fn 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(); diff --git a/components/selectors/lib.rs b/components/selectors/lib.rs index eb73192daee..a41a71ef8fd 100644 --- a/components/selectors/lib.rs +++ b/components/selectors/lib.rs @@ -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] diff --git a/components/selectors/nth_index_cache.rs b/components/selectors/nth_index_cache.rs index c334365c60d..d1de6b0dc02 100644 --- a/components/selectors/nth_index_cache.rs +++ b/components/selectors/nth_index_cache.rs @@ -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. @@ -32,7 +32,7 @@ impl NthIndexCache { /// The concrete per-pseudo-class cache. #[derive(Default)] -pub struct NthIndexCacheInner(FnvHashMap); +pub struct NthIndexCacheInner(FxHashMap); impl NthIndexCacheInner { /// Does a lookup for a given element in the cache. diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 6cf0f2a6e22..2eabd9a0007 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -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" diff --git a/components/style/context.rs b/components/style/context.rs index b35b3dbf489..f2d79aa232b 100644 --- a/components/style/context.rs +++ b/components/style/context.rs @@ -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; @@ -173,11 +173,11 @@ pub struct SharedStyleContext<'a> { /// The animations that are currently running. #[cfg(feature = "servo")] - pub running_animations: Arc>>>, + pub running_animations: Arc>>>, /// The list of animations that have expired since the last style recalculation. #[cfg(feature = "servo")] - pub expired_animations: Arc>>>, + pub expired_animations: Arc>>>, /// Paint worklets #[cfg(feature = "servo")] @@ -570,7 +570,7 @@ type CacheItem = (SendElement, ElementSelectorFlags); /// flags until after the traversal. pub struct SelectorFlagsMap { /// The hashmap storing the flags to apply. - map: FnvHashMap, ElementSelectorFlags>, + map: FxHashMap, ElementSelectorFlags>, /// An LRU cache to avoid hashmap lookups, which can be slow if the map /// gets big. cache: LRUCache<[Entry>; 4 + 1]>, @@ -587,7 +587,7 @@ impl SelectorFlagsMap { /// Creates a new empty SelectorFlagsMap. pub fn new() -> Self { SelectorFlagsMap { - map: FnvHashMap::default(), + map: FxHashMap::default(), cache: LRUCache::default(), } } @@ -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; + fn properties(&self) -> &FxHashMap; } /// A set of registered painters diff --git a/components/style/gecko/wrapper.rs b/components/style/gecko/wrapper.rs index 6cdcf1acd1e..58768e17e83 100644 --- a/components/style/gecko/wrapper.rs +++ b/components/style/gecko/wrapper.rs @@ -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}; @@ -867,12 +867,12 @@ impl<'le> GeckoElement<'le> { } } - fn css_transitions_info(&self) -> FnvHashMap> { + fn css_transitions_info(&self) -> FxHashMap> { 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) }; @@ -893,7 +893,7 @@ impl<'le> GeckoElement<'le> { combined_duration: f32, before_change_style: &ComputedValues, after_change_style: &ComputedValues, - existing_transitions: &FnvHashMap>, + existing_transitions: &FxHashMap>, ) -> bool { use values::animated::{Animate, Procedure}; debug_assert!(!longhand_id.is_logical()); diff --git a/components/style/hash.rs b/components/style/hash.rs index af1ce729a39..71c5b1c8a45 100644 --- a/components/style/hash.rs +++ b/components/style/hash.rs @@ -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; @@ -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 = HashMap; -/// Hash set that uses the FNV hasher -pub type FnvHashSet = HashSet; +/// Hash map that uses the Fx hasher +pub type FxHashMap = HashMap; +/// Hash set that uses the Fx hasher +pub type FxHashSet = HashSet; diff --git a/components/style/invalidation/media_queries.rs b/components/style/invalidation/media_queries.rs index 105c1bdb7cd..879540d683f 100644 --- a/components/style/invalidation/media_queries.rs +++ b/components/style/invalidation/media_queries.rs @@ -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}; @@ -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, + set: FxHashSet, } impl EffectiveMediaQueryResults { /// Trivially constructs an empty `EffectiveMediaQueryResults`. pub fn new() -> Self { Self { - set: FnvHashSet::default(), + set: FxHashSet::default(), } } diff --git a/components/style/invalidation/stylesheets.rs b/components/style/invalidation/stylesheets.rs index 66a36de1a24..1889d325630 100644 --- a/components/style/invalidation/stylesheets.rs +++ b/components/style/invalidation/stylesheets.rs @@ -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; @@ -106,9 +106,9 @@ impl Invalidation { #[derive(MallocSizeOf)] pub struct StylesheetInvalidationSet { /// The subtrees we know we have to restyle so far. - invalid_scopes: FnvHashSet, + invalid_scopes: FxHashSet, /// The elements we know we have to restyle so far. - invalid_elements: FnvHashSet, + invalid_elements: FxHashSet, /// Whether the whole document should be restyled. fully_invalid: bool, } @@ -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, } } diff --git a/components/style/lib.rs b/components/style/lib.rs index 20a54c62680..a4de57228c4 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -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; diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index d00f1947d86..f6277a21271 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -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}; @@ -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; +pub type AnimationValueMap = FxHashMap; #[cfg(feature = "gecko")] unsafe impl HasFFI for AnimationValueMap { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index ad184cd0fcf..28bdb05739c 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -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; @@ -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); diff --git a/components/style/rule_cache.rs b/components/style/rule_cache.rs index 1bd65775e3a..5e386d711cb 100644 --- a/components/style/rule_cache.rs +++ b/components/style/rule_cache.rs @@ -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; @@ -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); 1]>>, + map: FxHashMap); 1]>>, } impl RuleCache { /// Creates an empty `RuleCache`. pub fn new() -> Self { Self { - map: FnvHashMap::default(), + map: FxHashMap::default(), } } diff --git a/components/style/rule_tree/mod.rs b/components/style/rule_tree/mod.rs index 2ce403a666d..5414b254aae 100644 --- a/components/style/rule_tree/mod.rs +++ b/components/style/rule_tree/mod.rs @@ -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()); diff --git a/components/style/servo/selector_parser.rs b/components/style/servo/selector_parser.rs index 894148a1786..2083539f51b 100644 --- a/components/style/servo/selector_parser.rs +++ b/components/style/servo/selector_parser.rs @@ -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}; @@ -617,12 +617,12 @@ impl SelectorImpl { /// A map from elements to snapshots for the Servo style back-end. #[derive(Debug)] -pub struct SnapshotMap(FnvHashMap); +pub struct SnapshotMap(FxHashMap); impl SnapshotMap { /// Create a new empty `SnapshotMap`. pub fn new() -> Self { - SnapshotMap(FnvHashMap::default()) + SnapshotMap(FxHashMap::default()) } /// Get a snapshot given an element. @@ -632,7 +632,7 @@ impl SnapshotMap { } impl Deref for SnapshotMap { - type Target = FnvHashMap; + type Target = FxHashMap; fn deref(&self) -> &Self::Target { &self.0 diff --git a/components/style/stylesheets/stylesheet.rs b/components/style/stylesheets/stylesheet.rs index ee24a918c73..02730f7090d 100644 --- a/components/style/stylesheets/stylesheet.rs +++ b/components/style/stylesheets/stylesheet.rs @@ -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}; @@ -42,7 +42,7 @@ pub struct UserAgentStylesheets { #[allow(missing_docs)] pub struct Namespaces { pub default: Option, - pub prefixes: FnvHashMap, + pub prefixes: FxHashMap, } /// The contents of a given stylesheet. This effectively maps to a diff --git a/components/style/values/specified/position.rs b/components/style/values/specified/position.rs index 17a5db17bcb..fcfa07ae1d8 100644 --- a/components/style/values/specified/position.rs +++ b/components/style/values/specified/position.rs @@ -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; @@ -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 = None; row += 1;