From b871f906d6f303053b654b7c5ff3e32efe8758d8 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 12 Dec 2023 13:35:22 +0100 Subject: [PATCH] Use `FxHashMap` and `FxHashSet` in hot code paths We can also use these maps and sets in place of `SeaHasher` because they are also deterministic. Note that we're not swapping std's `HashMap` and `HashSet` wholesale inside of `collections` because on the server we need cryptographically secure collections. --- Cargo.lock | 2 +- crates/collections/Cargo.toml | 4 +-- crates/collections/src/collections.rs | 18 +++----------- crates/gpui2/src/app.rs | 26 ++++++++++---------- crates/gpui2/src/platform/mac/metal_atlas.rs | 4 +-- crates/gpui2/src/taffy.rs | 18 +++++++------- crates/gpui2/src/text_system.rs | 10 ++++---- crates/gpui2/src/text_system/line_layout.rs | 10 ++++---- 8 files changed, 40 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfe8dc7529..86ae29e16b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1990,7 +1990,7 @@ dependencies = [ name = "collections" version = "0.1.0" dependencies = [ - "seahash", + "rustc-hash", ] [[package]] diff --git a/crates/collections/Cargo.toml b/crates/collections/Cargo.toml index dcbc642c4c..64db1161bb 100644 --- a/crates/collections/Cargo.toml +++ b/crates/collections/Cargo.toml @@ -9,7 +9,7 @@ path = "src/collections.rs" doctest = false [features] -test-support = ["seahash"] +test-support = [] [dependencies] -seahash = { version = "4.1", optional = true } +rustc-hash = "1.1" diff --git a/crates/collections/src/collections.rs b/crates/collections/src/collections.rs index bffa5c877a..7628349353 100644 --- a/crates/collections/src/collections.rs +++ b/crates/collections/src/collections.rs @@ -1,21 +1,8 @@ #[cfg(feature = "test-support")] -#[derive(Clone, Default)] -pub struct DeterministicState; +pub type HashMap = FxHashMap; #[cfg(feature = "test-support")] -impl std::hash::BuildHasher for DeterministicState { - type Hasher = seahash::SeaHasher; - - fn build_hasher(&self) -> Self::Hasher { - seahash::SeaHasher::new() - } -} - -#[cfg(feature = "test-support")] -pub type HashMap = std::collections::HashMap; - -#[cfg(feature = "test-support")] -pub type HashSet = std::collections::HashSet; +pub type HashSet = FxHashSet; #[cfg(not(feature = "test-support"))] pub type HashMap = std::collections::HashMap; @@ -23,6 +10,7 @@ pub type HashMap = std::collections::HashMap; #[cfg(not(feature = "test-support"))] pub type HashSet = std::collections::HashSet; +pub use rustc_hash::{FxHashMap, FxHashSet}; use std::any::TypeId; pub use std::collections::*; diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rs index 88c5b8211d..0325b8d7f6 100644 --- a/crates/gpui2/src/app.rs +++ b/crates/gpui2/src/app.rs @@ -24,7 +24,7 @@ use crate::{ WindowContext, WindowHandle, WindowId, }; use anyhow::{anyhow, Result}; -use collections::{HashMap, HashSet, VecDeque}; +use collections::{FxHashMap, FxHashSet, VecDeque}; use futures::{channel::oneshot, future::LocalBoxFuture, Future}; use parking_lot::Mutex; use slotmap::SlotMap; @@ -191,24 +191,24 @@ pub struct AppContext { pub(crate) actions: Rc, pub(crate) active_drag: Option, pub(crate) active_tooltip: Option, - pub(crate) next_frame_callbacks: HashMap>, - pub(crate) frame_consumers: HashMap>, + pub(crate) next_frame_callbacks: FxHashMap>, + pub(crate) frame_consumers: FxHashMap>, pub(crate) background_executor: BackgroundExecutor, pub(crate) foreground_executor: ForegroundExecutor, pub(crate) svg_renderer: SvgRenderer, asset_source: Arc, pub(crate) image_cache: ImageCache, pub(crate) text_style_stack: Vec, - pub(crate) globals_by_type: HashMap>, + pub(crate) globals_by_type: FxHashMap>, pub(crate) entities: EntityMap, pub(crate) new_view_observers: SubscriberSet, pub(crate) windows: SlotMap>, pub(crate) keymap: Arc>, pub(crate) global_action_listeners: - HashMap>>, + FxHashMap>>, pending_effects: VecDeque, - pub(crate) pending_notifications: HashSet, - pub(crate) pending_global_notifications: HashSet, + pub(crate) pending_notifications: FxHashSet, + pub(crate) pending_global_notifications: FxHashSet, pub(crate) observers: SubscriberSet, // TypeId is the type of the event that the listener callback expects pub(crate) event_listeners: SubscriberSet, @@ -253,23 +253,23 @@ impl AppContext { pending_updates: 0, active_drag: None, active_tooltip: None, - next_frame_callbacks: HashMap::default(), - frame_consumers: HashMap::default(), + next_frame_callbacks: FxHashMap::default(), + frame_consumers: FxHashMap::default(), background_executor: executor, foreground_executor, svg_renderer: SvgRenderer::new(asset_source.clone()), asset_source, image_cache: ImageCache::new(http_client), text_style_stack: Vec::new(), - globals_by_type: HashMap::default(), + globals_by_type: FxHashMap::default(), entities, new_view_observers: SubscriberSet::new(), windows: SlotMap::with_key(), keymap: Arc::new(Mutex::new(Keymap::default())), - global_action_listeners: HashMap::default(), + global_action_listeners: FxHashMap::default(), pending_effects: VecDeque::new(), - pending_notifications: HashSet::default(), - pending_global_notifications: HashSet::default(), + pending_notifications: FxHashSet::default(), + pending_global_notifications: FxHashSet::default(), observers: SubscriberSet::new(), event_listeners: SubscriberSet::new(), release_listeners: SubscriberSet::new(), diff --git a/crates/gpui2/src/platform/mac/metal_atlas.rs b/crates/gpui2/src/platform/mac/metal_atlas.rs index 777d97b37f..2110815d9f 100644 --- a/crates/gpui2/src/platform/mac/metal_atlas.rs +++ b/crates/gpui2/src/platform/mac/metal_atlas.rs @@ -3,7 +3,7 @@ use crate::{ Point, Size, }; use anyhow::Result; -use collections::HashMap; +use collections::FxHashMap; use derive_more::{Deref, DerefMut}; use etagere::BucketedAtlasAllocator; use metal::Device; @@ -53,7 +53,7 @@ struct MetalAtlasState { monochrome_textures: Vec, polychrome_textures: Vec, path_textures: Vec, - tiles_by_key: HashMap, + tiles_by_key: FxHashMap, } impl PlatformAtlas for MetalAtlas { diff --git a/crates/gpui2/src/taffy.rs b/crates/gpui2/src/taffy.rs index b4fc6c3abe..88fb8e8814 100644 --- a/crates/gpui2/src/taffy.rs +++ b/crates/gpui2/src/taffy.rs @@ -2,7 +2,7 @@ use crate::{ AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style, WindowContext, }; -use collections::{HashMap, HashSet}; +use collections::{FxHashMap, FxHashSet}; use smallvec::SmallVec; use std::fmt::Debug; use taffy::{ @@ -14,10 +14,10 @@ use taffy::{ pub struct TaffyLayoutEngine { taffy: Taffy, - children_to_parents: HashMap, - absolute_layout_bounds: HashMap>, - computed_layouts: HashSet, - nodes_to_measure: HashMap< + children_to_parents: FxHashMap, + absolute_layout_bounds: FxHashMap>, + computed_layouts: FxHashSet, + nodes_to_measure: FxHashMap< LayoutId, Box< dyn FnMut( @@ -36,10 +36,10 @@ impl TaffyLayoutEngine { pub fn new() -> Self { TaffyLayoutEngine { taffy: Taffy::new(), - children_to_parents: HashMap::default(), - absolute_layout_bounds: HashMap::default(), - computed_layouts: HashSet::default(), - nodes_to_measure: HashMap::default(), + children_to_parents: FxHashMap::default(), + absolute_layout_bounds: FxHashMap::default(), + computed_layouts: FxHashSet::default(), + nodes_to_measure: FxHashMap::default(), } } diff --git a/crates/gpui2/src/text_system.rs b/crates/gpui2/src/text_system.rs index b3f17bd057..ad82c41082 100644 --- a/crates/gpui2/src/text_system.rs +++ b/crates/gpui2/src/text_system.rs @@ -13,7 +13,7 @@ use crate::{ UnderlineStyle, }; use anyhow::anyhow; -use collections::HashMap; +use collections::FxHashMap; use core::fmt; use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; use smallvec::SmallVec; @@ -37,10 +37,10 @@ pub const SUBPIXEL_VARIANTS: u8 = 4; pub struct TextSystem { line_layout_cache: Arc, platform_text_system: Arc, - font_ids_by_font: RwLock>, - font_metrics: RwLock>, - raster_bounds: RwLock>>, - wrapper_pool: Mutex>>, + font_ids_by_font: RwLock>, + font_metrics: RwLock>, + raster_bounds: RwLock>>, + wrapper_pool: Mutex>>, font_runs_pool: Mutex>>, } diff --git a/crates/gpui2/src/text_system/line_layout.rs b/crates/gpui2/src/text_system/line_layout.rs index 2370aca83b..6506d7794c 100644 --- a/crates/gpui2/src/text_system/line_layout.rs +++ b/crates/gpui2/src/text_system/line_layout.rs @@ -1,9 +1,9 @@ use crate::{px, FontId, GlyphId, Pixels, PlatformTextSystem, Point, Size}; +use collections::FxHashMap; use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; use smallvec::SmallVec; use std::{ borrow::Borrow, - collections::HashMap, hash::{Hash, Hasher}, sync::Arc, }; @@ -236,10 +236,10 @@ impl WrappedLineLayout { } pub(crate) struct LineLayoutCache { - previous_frame: Mutex>>, - current_frame: RwLock>>, - previous_frame_wrapped: Mutex>>, - current_frame_wrapped: RwLock>>, + previous_frame: Mutex>>, + current_frame: RwLock>>, + previous_frame_wrapped: Mutex>>, + current_frame_wrapped: RwLock>>, platform_text_system: Arc, }