Use rustc-hash for most of our HashMap and HashSet instances

This commit is contained in:
Héctor Ramón Jiménez 2024-04-01 11:59:46 +02:00
parent 14ed71e09b
commit f5bcfec821
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
19 changed files with 60 additions and 49 deletions

View file

@ -1,6 +1,7 @@
//! Load and draw raster graphics. //! Load and draw raster graphics.
use crate::{Hasher, Rectangle, Size}; use crate::{Rectangle, Size};
use rustc_hash::FxHasher;
use std::hash::{Hash, Hasher as _}; use std::hash::{Hash, Hasher as _};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -50,7 +51,7 @@ impl Handle {
} }
fn from_data(data: Data) -> Handle { fn from_data(data: Data) -> Handle {
let mut hasher = Hasher::default(); let mut hasher = FxHasher::default();
data.hash(&mut hasher); data.hash(&mut hasher);
Handle { Handle {

View file

@ -41,7 +41,6 @@ mod background;
mod color; mod color;
mod content_fit; mod content_fit;
mod element; mod element;
mod hasher;
mod length; mod length;
mod padding; mod padding;
mod pixels; mod pixels;
@ -64,7 +63,6 @@ pub use element::Element;
pub use event::Event; pub use event::Event;
pub use font::Font; pub use font::Font;
pub use gradient::Gradient; pub use gradient::Gradient;
pub use hasher::Hasher;
pub use layout::Layout; pub use layout::Layout;
pub use length::Length; pub use length::Length;
pub use overlay::Overlay; pub use overlay::Overlay;

View file

@ -1,6 +1,7 @@
//! Load and draw vector graphics. //! Load and draw vector graphics.
use crate::{Color, Hasher, Rectangle, Size}; use crate::{Color, Rectangle, Size};
use rustc_hash::FxHasher;
use std::borrow::Cow; use std::borrow::Cow;
use std::hash::{Hash, Hasher as _}; use std::hash::{Hash, Hasher as _};
use std::path::PathBuf; use std::path::PathBuf;
@ -30,7 +31,7 @@ impl Handle {
} }
fn from_data(data: Data) -> Handle { fn from_data(data: Data) -> Handle {
let mut hasher = Hasher::default(); let mut hasher = FxHasher::default();
data.hash(&mut hasher); data.hash(&mut hasher);
Handle { Handle {

View file

@ -22,6 +22,7 @@ iced_core.workspace = true
futures.workspace = true futures.workspace = true
log.workspace = true log.workspace = true
rustc-hash.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
async-std.workspace = true async-std.workspace = true

View file

@ -18,8 +18,7 @@ impl crate::Executor for Executor {
pub mod time { pub mod time {
//! Listen and react to time. //! Listen and react to time.
use crate::core::Hasher; use crate::subscription::{self, Hasher, Subscription};
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval. /// Returns a [`Subscription`] that produces messages at a set interval.
/// ///

View file

@ -17,8 +17,7 @@ impl crate::Executor for Executor {
pub mod time { pub mod time {
//! Listen and react to time. //! Listen and react to time.
use crate::core::Hasher; use crate::subscription::{self, Hasher, Subscription};
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval. /// Returns a [`Subscription`] that produces messages at a set interval.
/// ///

View file

@ -22,8 +22,7 @@ impl crate::Executor for Executor {
pub mod time { pub mod time {
//! Listen and react to time. //! Listen and react to time.
use crate::core::Hasher; use crate::subscription::{self, Hasher, Subscription};
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval. /// Returns a [`Subscription`] that produces messages at a set interval.
/// ///

View file

@ -4,7 +4,6 @@ mod tracker;
pub use tracker::Tracker; pub use tracker::Tracker;
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::Hasher;
use crate::futures::{Future, Stream}; use crate::futures::{Future, Stream};
use crate::{BoxStream, MaybeSend}; use crate::{BoxStream, MaybeSend};
@ -18,6 +17,9 @@ use std::hash::Hash;
/// It is the input of a [`Subscription`]. /// It is the input of a [`Subscription`].
pub type EventStream = BoxStream<(Event, event::Status)>; pub type EventStream = BoxStream<(Event, event::Status)>;
/// The hasher used for identifying subscriptions.
pub type Hasher = rustc_hash::FxHasher;
/// A request to listen to external events. /// A request to listen to external events.
/// ///
/// Besides performing async actions on demand with `Command`, most /// Besides performing async actions on demand with `Command`, most

View file

@ -1,12 +1,11 @@
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::Hasher; use crate::subscription::{Hasher, Recipe};
use crate::subscription::Recipe;
use crate::{BoxFuture, MaybeSend}; use crate::{BoxFuture, MaybeSend};
use futures::channel::mpsc; use futures::channel::mpsc;
use futures::sink::{Sink, SinkExt}; use futures::sink::{Sink, SinkExt};
use rustc_hash::FxHashMap;
use std::collections::HashMap;
use std::hash::Hasher as _; use std::hash::Hasher as _;
/// A registry of subscription streams. /// A registry of subscription streams.
@ -18,7 +17,7 @@ use std::hash::Hasher as _;
/// [`Subscription`]: crate::Subscription /// [`Subscription`]: crate::Subscription
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Tracker { pub struct Tracker {
subscriptions: HashMap<u64, Execution>, subscriptions: FxHashMap<u64, Execution>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -31,7 +30,7 @@ impl Tracker {
/// Creates a new empty [`Tracker`]. /// Creates a new empty [`Tracker`].
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
subscriptions: HashMap::new(), subscriptions: FxHashMap::default(),
} }
} }

View file

@ -9,10 +9,12 @@ pub use crate::core::renderer::{self, Renderer};
pub use crate::core::svg; pub use crate::core::svg;
pub use crate::core::text::{self, Text}; pub use crate::core::text::{self, Text};
pub use crate::core::widget::{self, Widget}; pub use crate::core::widget::{self, Widget};
pub use crate::core::{Hasher, Shell}; pub use crate::core::Shell;
pub use crate::renderer::graphics; pub use crate::renderer::graphics;
pub mod subscription { pub mod subscription {
//! Write your own subscriptions. //! Write your own subscriptions.
pub use crate::runtime::futures::subscription::{EventStream, Recipe}; pub use crate::runtime::futures::subscription::{
EventStream, Hasher, Recipe,
};
} }

View file

@ -32,6 +32,7 @@ glyphon.workspace = true
guillotiere.workspace = true guillotiere.workspace = true
log.workspace = true log.workspace = true
once_cell.workspace = true once_cell.workspace = true
rustc-hash.workspace = true
thiserror.workspace = true thiserror.workspace = true
wgpu.workspace = true wgpu.workspace = true

View file

@ -4,7 +4,7 @@ use crate::graphics;
use crate::graphics::image::image_rs; use crate::graphics::image::image_rs;
use crate::image::atlas::{self, Atlas}; use crate::image::atlas::{self, Atlas};
use std::collections::{HashMap, HashSet}; use rustc_hash::{FxHashMap, FxHashSet};
/// Entry in cache corresponding to an image handle /// Entry in cache corresponding to an image handle
#[derive(Debug)] #[derive(Debug)]
@ -38,8 +38,8 @@ impl Memory {
/// Caches image raster data /// Caches image raster data
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Cache { pub struct Cache {
map: HashMap<u64, Memory>, map: FxHashMap<u64, Memory>,
hits: HashSet<u64>, hits: FxHashSet<u64>,
} }
impl Cache { impl Cache {

View file

@ -5,7 +5,7 @@ use crate::image::atlas::{self, Atlas};
use resvg::tiny_skia; use resvg::tiny_skia;
use resvg::usvg::{self, TreeTextToPath}; use resvg::usvg::{self, TreeTextToPath};
use std::collections::{HashMap, HashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use std::fs; use std::fs;
/// Entry in cache corresponding to an svg handle /// Entry in cache corresponding to an svg handle
@ -33,10 +33,10 @@ impl Svg {
/// Caches svg vector and raster data /// Caches svg vector and raster data
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Cache { pub struct Cache {
svgs: HashMap<u64, Svg>, svgs: FxHashMap<u64, Svg>,
rasterized: HashMap<(u64, u32, u32, ColorFilter), atlas::Entry>, rasterized: FxHashMap<(u64, u32, u32, ColorFilter), atlas::Entry>,
svg_hits: HashSet<u64>, svg_hits: FxHashSet<u64>,
rasterized_hits: HashSet<(u64, u32, u32, ColorFilter)>, rasterized_hits: FxHashSet<(u64, u32, u32, ColorFilter)>,
} }
type ColorFilter = Option<[u8; 4]>; type ColorFilter = Option<[u8; 4]>;

View file

@ -1,8 +1,8 @@
//! Draw primitives using custom pipelines. //! Draw primitives using custom pipelines.
use crate::core::{self, Rectangle, Size}; use crate::core::{self, Rectangle, Size};
use rustc_hash::FxHashMap;
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
@ -82,7 +82,7 @@ impl Renderer for crate::Renderer {
/// Stores custom, user-provided pipelines. /// Stores custom, user-provided pipelines.
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct Storage { pub struct Storage {
pipelines: HashMap<TypeId, Box<dyn Any + Send>>, pipelines: FxHashMap<TypeId, Box<dyn Any + Send>>,
} }
impl Storage { impl Storage {

View file

@ -28,6 +28,7 @@ iced_renderer.workspace = true
iced_runtime.workspace = true iced_runtime.workspace = true
num-traits.workspace = true num-traits.workspace = true
rustc-hash.workspace = true
thiserror.workspace = true thiserror.workspace = true
unicode-segmentation.workspace = true unicode-segmentation.workspace = true

View file

@ -18,11 +18,12 @@ use crate::core::widget::tree::{self, Tree};
use crate::core::widget::{self, Widget}; use crate::core::widget::{self, Widget};
use crate::core::Element; use crate::core::Element;
use crate::core::{ use crate::core::{
self, Clipboard, Hasher, Length, Point, Rectangle, Shell, Size, Vector, self, Clipboard, Length, Point, Rectangle, Shell, Size, Vector,
}; };
use crate::runtime::overlay::Nested; use crate::runtime::overlay::Nested;
use ouroboros::self_referencing; use ouroboros::self_referencing;
use rustc_hash::FxHasher;
use std::cell::RefCell; use std::cell::RefCell;
use std::hash::{Hash, Hasher as H}; use std::hash::{Hash, Hasher as H};
use std::rc::Rc; use std::rc::Rc;
@ -106,9 +107,12 @@ where
} }
fn state(&self) -> tree::State { fn state(&self) -> tree::State {
let mut hasher = Hasher::default(); let hash = {
self.dependency.hash(&mut hasher); let mut hasher = FxHasher::default();
let hash = hasher.finish(); self.dependency.hash(&mut hasher);
hasher.finish()
};
let element = let element =
Rc::new(RefCell::new(Some((self.view)(&self.dependency).into()))); Rc::new(RefCell::new(Some((self.view)(&self.dependency).into())));
@ -127,9 +131,12 @@ where
.state .state
.downcast_mut::<Internal<Message, Theme, Renderer>>(); .downcast_mut::<Internal<Message, Theme, Renderer>>();
let mut hasher = Hasher::default(); let new_hash = {
self.dependency.hash(&mut hasher); let mut hasher = FxHasher::default();
let new_hash = hasher.finish(); self.dependency.hash(&mut hasher);
hasher.finish()
};
if current.hash != new_hash { if current.hash != new_hash {
current.hash = new_hash; current.hash = new_hash;

View file

@ -6,7 +6,7 @@ use crate::pane_grid::{
Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target, Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target,
}; };
use std::collections::HashMap; use rustc_hash::FxHashMap;
/// The state of a [`PaneGrid`]. /// The state of a [`PaneGrid`].
/// ///
@ -25,7 +25,7 @@ pub struct State<T> {
/// The panes of the [`PaneGrid`]. /// The panes of the [`PaneGrid`].
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub panes: HashMap<Pane, T>, pub panes: FxHashMap<Pane, T>,
/// The internal state of the [`PaneGrid`]. /// The internal state of the [`PaneGrid`].
/// ///
@ -52,7 +52,7 @@ impl<T> State<T> {
/// Creates a new [`State`] with the given [`Configuration`]. /// Creates a new [`State`] with the given [`Configuration`].
pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self { pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
let mut panes = HashMap::new(); let mut panes = FxHashMap::default();
let internal = let internal =
Internal::from_configuration(&mut panes, config.into(), 0); Internal::from_configuration(&mut panes, config.into(), 0);
@ -353,7 +353,7 @@ impl Internal {
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub fn from_configuration<T>( pub fn from_configuration<T>(
panes: &mut HashMap<Pane, T>, panes: &mut FxHashMap<Pane, T>,
content: Configuration<T>, content: Configuration<T>,
next_id: usize, next_id: usize,
) -> Self { ) -> Self {

View file

@ -26,6 +26,7 @@ iced_graphics.workspace = true
iced_runtime.workspace = true iced_runtime.workspace = true
log.workspace = true log.workspace = true
rustc-hash.workspace = true
thiserror.workspace = true thiserror.workspace = true
tracing.workspace = true tracing.workspace = true
window_clipboard.workspace = true window_clipboard.workspace = true

View file

@ -27,7 +27,7 @@ use crate::{Clipboard, Error, Proxy, Settings};
pub use crate::application::{default, Appearance, DefaultStyle}; pub use crate::application::{default, Appearance, DefaultStyle};
use std::collections::HashMap; use rustc_hash::FxHashMap;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant; use std::time::Instant;
@ -381,12 +381,12 @@ async fn run_instance<A, E, C>(
)] )]
}; };
let mut ui_caches = HashMap::new(); let mut ui_caches = FxHashMap::default();
let mut user_interfaces = ManuallyDrop::new(build_user_interfaces( let mut user_interfaces = ManuallyDrop::new(build_user_interfaces(
&application, &application,
&mut debug, &mut debug,
&mut window_manager, &mut window_manager,
HashMap::from_iter([( FxHashMap::from_iter([(
window::Id::MAIN, window::Id::MAIN,
user_interface::Cache::default(), user_interface::Cache::default(),
)]), )]),
@ -759,7 +759,7 @@ async fn run_instance<A, E, C>(
// TODO mw application update returns which window IDs to update // TODO mw application update returns which window IDs to update
if !messages.is_empty() || uis_stale { if !messages.is_empty() || uis_stale {
let mut cached_interfaces: HashMap< let mut cached_interfaces: FxHashMap<
window::Id, window::Id,
user_interface::Cache, user_interface::Cache,
> = ManuallyDrop::into_inner(user_interfaces) > = ManuallyDrop::into_inner(user_interfaces)
@ -849,7 +849,7 @@ fn update<A: Application, C, E: Executor>(
debug: &mut Debug, debug: &mut Debug,
messages: &mut Vec<A::Message>, messages: &mut Vec<A::Message>,
window_manager: &mut WindowManager<A, C>, window_manager: &mut WindowManager<A, C>,
ui_caches: &mut HashMap<window::Id, user_interface::Cache>, ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
) where ) where
C: Compositor<Renderer = A::Renderer> + 'static, C: Compositor<Renderer = A::Renderer> + 'static,
A::Theme: DefaultStyle, A::Theme: DefaultStyle,
@ -890,7 +890,7 @@ fn run_command<A, C, E>(
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
debug: &mut Debug, debug: &mut Debug,
window_manager: &mut WindowManager<A, C>, window_manager: &mut WindowManager<A, C>,
ui_caches: &mut HashMap<window::Id, user_interface::Cache>, ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
) where ) where
A: Application, A: Application,
E: Executor, E: Executor,
@ -1218,8 +1218,8 @@ pub fn build_user_interfaces<'a, A: Application, C: Compositor>(
application: &'a A, application: &'a A,
debug: &mut Debug, debug: &mut Debug,
window_manager: &mut WindowManager<A, C>, window_manager: &mut WindowManager<A, C>,
mut cached_user_interfaces: HashMap<window::Id, user_interface::Cache>, mut cached_user_interfaces: FxHashMap<window::Id, user_interface::Cache>,
) -> HashMap<window::Id, UserInterface<'a, A::Message, A::Theme, A::Renderer>> ) -> FxHashMap<window::Id, UserInterface<'a, A::Message, A::Theme, A::Renderer>>
where where
C: Compositor<Renderer = A::Renderer>, C: Compositor<Renderer = A::Renderer>,
A::Theme: DefaultStyle, A::Theme: DefaultStyle,