Skip to content

Commit 5863671

Browse files
authored
Merge pull request #1298 from Cheong-Lau/rustc-hash
perf: use `rustc-hash` for `HashMap` and `HashSet`
2 parents 7cc28a9 + c073640 commit 5863671

File tree

11 files changed

+68
-68
lines changed

11 files changed

+68
-68
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ notify-rust = { version = "4", optional = true }
2929
open = "5.3.2"
3030
paste = "1.0"
3131
regex = "1"
32+
rustc-hash = "2.1"
3233
serde = { version = "1", features = ["serde_derive"] }
3334
shlex = { version = "1.3" }
3435
tempfile = "3"

src/app.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ use notify_debouncer_full::{
4848
DebouncedEvent, Debouncer, RecommendedCache, new_debouncer,
4949
notify::{self, RecommendedWatcher},
5050
};
51+
use rustc_hash::{FxHashMap, FxHashSet};
5152
use slotmap::Key as SlotMapKey;
5253
use std::{
5354
any::TypeId,
54-
collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
55+
collections::{BTreeMap, BTreeSet, HashMap, VecDeque},
5556
env, fmt, fs,
5657
future::Future,
5758
io,
@@ -68,6 +69,7 @@ use trash::TrashItem;
6869
use wayland_client::{Proxy, protocol::wl_output::WlOutput};
6970

7071
use crate::{
72+
FxOrderMap,
7173
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
7274
config::{
7375
AppTheme, Config, DesktopConfig, Favorite, IconSizes, TIME_CONFIG_ID, TabConfig,
@@ -676,17 +678,17 @@ pub struct App {
676678
dialog_pages: DialogPages,
677679
dialog_text_input: widget::Id,
678680
key_binds: HashMap<KeyBind, Action>,
679-
margin: HashMap<window::Id, (f32, f32, f32, f32)>,
681+
margin: FxHashMap<window::Id, (f32, f32, f32, f32)>,
680682
mime_app_cache: MimeAppCache,
681683
modifiers: Modifiers,
682-
mounter_items: HashMap<MounterKey, MounterItems>,
684+
mounter_items: FxHashMap<MounterKey, MounterItems>,
683685
must_save_sort_names: bool,
684686
network_drive_connecting: Option<(MounterKey, String)>,
685687
network_drive_input: String,
686688
#[cfg(feature = "notify")]
687689
notification_opt: Option<Arc<Mutex<notify_rust::NotificationHandle>>>,
688690
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
689-
overlap: HashMap<String, (window::Id, Rectangle)>,
691+
overlap: FxHashMap<String, (window::Id, Rectangle)>,
690692
pending_operation_id: u64,
691693
pending_operations: BTreeMap<u64, (Operation, Controller)>,
692694
progress_operations: BTreeSet<u64>,
@@ -696,17 +698,17 @@ pub struct App {
696698
search_id: widget::Id,
697699
size: Option<Size>,
698700
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
699-
layer_sizes: HashMap<window::Id, Size>,
701+
layer_sizes: FxHashMap<window::Id, Size>,
700702
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
701-
surface_ids: HashMap<WlOutput, WindowId>,
703+
surface_ids: FxHashMap<WlOutput, WindowId>,
702704
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
703-
surface_names: HashMap<WindowId, String>,
705+
surface_names: FxHashMap<WindowId, String>,
704706
toasts: widget::toaster::Toasts<Message>,
705707
watcher_opt: Option<(
706708
Debouncer<RecommendedWatcher, RecommendedCache>,
707-
HashSet<PathBuf>,
709+
FxHashSet<PathBuf>,
708710
)>,
709-
windows: HashMap<window::Id, WindowKind>,
711+
windows: FxHashMap<window::Id, WindowKind>,
710712
nav_dnd_hover: Option<(Location, Instant)>,
711713
tab_dnd_hover: Option<(Entity, Instant)>,
712714
nav_drag_id: DragId,
@@ -731,7 +733,7 @@ impl App {
731733
// Associate all paths to its MIME type
732734
// This allows handling paths as groups if possible, such as launching a single video
733735
// player that is passed every path.
734-
let mut groups: HashMap<Mime, Vec<PathBuf>> = HashMap::new();
736+
let mut groups: FxHashMap<Mime, Vec<PathBuf>> = FxHashMap::default();
735737
let mut all_archives = true;
736738
let supported_archive_types = crate::archive::SUPPORTED_ARCHIVE_TYPES
737739
.iter()
@@ -948,7 +950,7 @@ impl App {
948950

949951
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
950952
fn handle_overlap(&mut self) {
951-
let mut overlaps: HashMap<_, _> = self
953+
let mut overlaps: FxHashMap<_, _> = self
952954
.windows
953955
.keys()
954956
.map(|k| (*k, (0., 0., 0., 0.)))
@@ -1609,7 +1611,7 @@ impl App {
16091611

16101612
fn update_watcher(&mut self) -> Task<Message> {
16111613
if let Some((mut watcher, old_paths)) = self.watcher_opt.take() {
1612-
let mut new_paths = HashSet::new();
1614+
let mut new_paths = FxHashSet::default();
16131615
for entity in self.tab_model.iter() {
16141616
if let Some(tab) = self.tab_model.data::<Tab>(entity) {
16151617
if let Some(path) = tab.location.path_opt() {
@@ -1978,7 +1980,7 @@ impl App {
19781980
fn get_apps_for_mime(&self, mime_type: &Mime) -> Vec<(&MimeApp, MimeAppMatch)> {
19791981
let mut results = Vec::new();
19801982

1981-
let mut dedupe = HashSet::new();
1983+
let mut dedupe = FxHashSet::default();
19821984

19831985
// start with exact matches
19841986
for mime_app in self.mime_app_cache.get(mime_type) {
@@ -2160,17 +2162,17 @@ impl Application for App {
21602162
dialog_pages: DialogPages::new(),
21612163
dialog_text_input: widget::Id::new("Dialog Text Input"),
21622164
key_binds,
2163-
margin: HashMap::new(),
2165+
margin: FxHashMap::default(),
21642166
mime_app_cache: MimeAppCache::new(),
21652167
modifiers: Modifiers::empty(),
2166-
mounter_items: HashMap::new(),
2168+
mounter_items: FxHashMap::default(),
21672169
must_save_sort_names: false,
21682170
network_drive_connecting: None,
21692171
network_drive_input: String::new(),
21702172
#[cfg(feature = "notify")]
21712173
notification_opt: None,
21722174
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
2173-
overlap: HashMap::new(),
2175+
overlap: FxHashMap::default(),
21742176
pending_operation_id: 0,
21752177
pending_operations: BTreeMap::new(),
21762178
progress_operations: BTreeSet::new(),
@@ -2180,20 +2182,20 @@ impl Application for App {
21802182
search_id: widget::Id::new("File Search"),
21812183
size: None,
21822184
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
2183-
surface_ids: HashMap::new(),
2185+
surface_ids: FxHashMap::default(),
21842186
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
2185-
surface_names: HashMap::new(),
2187+
surface_names: FxHashMap::default(),
21862188
toasts: widget::toaster::Toasts::new(Message::CloseToast),
21872189
watcher_opt: None,
2188-
windows: HashMap::new(),
2190+
windows: FxHashMap::default(),
21892191
nav_dnd_hover: None,
21902192
tab_dnd_hover: None,
21912193
nav_drag_id: DragId::new(),
21922194
tab_drag_id: DragId::new(),
21932195
auto_scroll_speed: None,
21942196
file_dialog_opt: None,
21952197
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
2196-
layer_sizes: HashMap::new(),
2198+
layer_sizes: FxHashMap::default(),
21972199
};
21982200

21992201
let mut commands = vec![app.update_config()];
@@ -3264,7 +3266,7 @@ impl Application for App {
32643266
Message::NotifyWatcher(mut watcher_wrapper) => match watcher_wrapper.watcher_opt.take()
32653267
{
32663268
Some(watcher) => {
3267-
self.watcher_opt = Some((watcher, HashSet::new()));
3269+
self.watcher_opt = Some((watcher, FxHashSet::default()));
32683270
return self.update_watcher();
32693271
}
32703272
None => {
@@ -4744,8 +4746,8 @@ impl Application for App {
47444746
Message::SaveSortNames => {
47454747
self.must_save_sort_names = false;
47464748
if let Some(state_handler) = self.state_handler.as_ref() {
4747-
if let Err(err) =
4748-
state_handler.set::<ordermap::OrderMap<String, (HeadingOptions, bool)>>(
4749+
if let Err(err) = state_handler
4750+
.set::<FxOrderMap<String, (HeadingOptions, bool)>>(
47494751
"sort_names",
47504752
self.state.sort_names.clone(),
47514753
)

src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use cosmic::{
88
iced::Subscription,
99
theme,
1010
};
11-
use ordermap::OrderMap;
1211
use serde::{Deserialize, Serialize};
1312

1413
use crate::{
14+
FxOrderMap,
1515
app::App,
1616
tab::{HeadingOptions, Location, View},
1717
};
@@ -115,13 +115,13 @@ pub enum TypeToSearch {
115115
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
116116
#[serde(default)]
117117
pub struct State {
118-
pub sort_names: ordermap::OrderMap<String, (HeadingOptions, bool)>,
118+
pub sort_names: FxOrderMap<String, (HeadingOptions, bool)>,
119119
}
120120

121121
impl Default for State {
122122
fn default() -> Self {
123123
Self {
124-
sort_names: OrderMap::from_iter(dirs::download_dir().into_iter().map(|dir| {
124+
sort_names: FxOrderMap::from_iter(dirs::download_dir().into_iter().map(|dir| {
125125
(
126126
Location::Path(dir).normalize().to_string(),
127127
(HeadingOptions::Modified, false),

src/dialog.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use notify_debouncer_full::{
2626
notify::{self, RecommendedWatcher},
2727
};
2828
use recently_used_xbel::update_recently_used;
29+
use rustc_hash::{FxHashMap, FxHashSet};
2930
use std::{
3031
any::TypeId,
31-
collections::{HashMap, HashSet, VecDeque},
32+
collections::{HashMap, VecDeque},
3233
env, fmt, fs,
3334
num::NonZeroU16,
3435
path::PathBuf,
@@ -513,15 +514,15 @@ struct App {
513514
filter_selected: Option<usize>,
514515
filename_id: widget::Id,
515516
modifiers: Modifiers,
516-
mounter_items: HashMap<MounterKey, MounterItems>,
517+
mounter_items: FxHashMap<MounterKey, MounterItems>,
517518
nav_model: segmented_button::SingleSelectModel,
518519
result_opt: Option<DialogResult>,
519520
search_id: widget::Id,
520521
tab: Tab,
521522
key_binds: HashMap<KeyBind, Action>,
522523
watcher_opt: Option<(
523524
Debouncer<RecommendedWatcher, RecommendedCache>,
524-
HashSet<PathBuf>,
525+
FxHashSet<PathBuf>,
525526
)>,
526527
auto_scroll_speed: Option<i16>,
527528
}
@@ -875,7 +876,7 @@ impl App {
875876

876877
fn update_watcher(&mut self) -> Task<Message> {
877878
if let Some((mut watcher, old_paths)) = self.watcher_opt.take() {
878-
let mut new_paths = HashSet::new();
879+
let mut new_paths = FxHashSet::default();
879880
if let Some(path) = &self.tab.location.path_opt() {
880881
new_paths.insert(path.to_path_buf());
881882
}
@@ -985,7 +986,7 @@ impl Application for App {
985986
filter_selected: None,
986987
filename_id: widget::Id::new("Dialog Filename"),
987988
modifiers: Modifiers::empty(),
988-
mounter_items: HashMap::new(),
989+
mounter_items: FxHashMap::default(),
989990
nav_model: segmented_button::ModelBuilder::default().build(),
990991
result_opt: None,
991992
search_id: widget::Id::new("Dialog File Search"),
@@ -1532,7 +1533,7 @@ impl Application for App {
15321533
Message::NotifyWatcher(mut watcher_wrapper) => match watcher_wrapper.watcher_opt.take()
15331534
{
15341535
Some(watcher) => {
1535-
self.watcher_opt = Some((watcher, HashSet::new()));
1536+
self.watcher_opt = Some((watcher, FxHashSet::default()));
15361537
return self.update_watcher();
15371538
}
15381539
None => {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub mod tab;
2727
mod thumbnail_cacher;
2828
mod thumbnailer;
2929

30+
pub(crate) type FxOrderMap<K, V> = ordermap::OrderMap<K, V, rustc_hash::FxBuildHasher>;
31+
3032
pub(crate) fn err_str<T: ToString>(err: T) -> String {
3133
err.to_string()
3234
}

src/mime_app.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use cosmic::desktop;
66
use cosmic::widget;
77
pub use mime_guess::Mime;
8+
use rustc_hash::FxHashMap;
89
use std::{
910
cmp::Ordering,
10-
collections::HashMap,
1111
env,
1212
ffi::OsStr,
1313
fs, io,
@@ -221,17 +221,17 @@ fn filename_eq(path_opt: &Option<PathBuf>, filename: &str) -> bool {
221221

222222
pub struct MimeAppCache {
223223
apps: Vec<MimeApp>,
224-
cache: HashMap<Mime, Vec<MimeApp>>,
225-
icons: HashMap<Mime, Vec<widget::icon::Handle>>,
224+
cache: FxHashMap<Mime, Vec<MimeApp>>,
225+
icons: FxHashMap<Mime, Vec<widget::icon::Handle>>,
226226
terminals: Vec<MimeApp>,
227227
}
228228

229229
impl MimeAppCache {
230230
pub fn new() -> Self {
231231
let mut mime_app_cache = Self {
232232
apps: Vec::new(),
233-
cache: HashMap::new(),
234-
icons: HashMap::new(),
233+
cache: FxHashMap::default(),
234+
icons: FxHashMap::default(),
235235
terminals: Vec::new(),
236236
};
237237
mime_app_cache.reload();

src/mime_icon.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use cosmic::widget::icon;
44
use mime_guess::Mime;
5+
use rustc_hash::FxHashMap;
56
use std::{
6-
collections::HashMap,
77
fs,
88
path::Path,
99
sync::{LazyLock, Mutex},
@@ -18,14 +18,14 @@ struct MimeIconKey {
1818
}
1919

2020
struct MimeIconCache {
21-
cache: HashMap<MimeIconKey, Option<icon::Handle>>,
21+
cache: FxHashMap<MimeIconKey, Option<icon::Handle>>,
2222
shared_mime_info: xdg_mime::SharedMimeInfo,
2323
}
2424

2525
impl MimeIconCache {
2626
pub fn new() -> Self {
2727
Self {
28-
cache: HashMap::new(),
28+
cache: FxHashMap::default(),
2929
shared_mime_info: xdg_mime::SharedMimeInfo::new(),
3030
}
3131
}

0 commit comments

Comments
 (0)