From b847de45d358dd62ffd19b163eaff56c63065f4e Mon Sep 17 00:00:00 2001 From: Kyosuke Fujimoto Date: Sat, 17 Jan 2026 17:02:52 +0900 Subject: [PATCH] Use rustc_hash::FxHashMap instead of std::collections::HashMap --- src/app.rs | 5 ++--- src/config.rs | 14 +++++++------- src/git.rs | 32 +++++++++++++++++--------------- src/keybind.rs | 10 +++++----- src/widget/commit_list.rs | 11 +++++------ 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/app.rs b/src/app.rs index ae27b3e..82688bd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use ratatui::{ backend::Backend, crossterm::event::{KeyCode, KeyEvent}, @@ -9,6 +7,7 @@ use ratatui::{ widgets::{Block, Borders, Padding, Paragraph}, Frame, Terminal, }; +use rustc_hash::FxHashMap; use crate::{ color::{ColorTheme, GraphColorSet}, @@ -70,7 +69,7 @@ impl<'a> App<'a> { initial_selection: InitialSelection, tx: Sender, ) -> Self { - let mut ref_name_to_commit_index_map = HashMap::new(); + let mut ref_name_to_commit_index_map = FxHashMap::default(); let commits = graph .commits .iter() diff --git a/src/config.rs b/src/config.rs index d36a84c..386d63a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,10 @@ use std::{ - collections::HashMap, env, path::{Path, PathBuf}, }; use garde::Validate; +use rustc_hash::FxHashMap; use serde::Deserialize; use smart_default::SmartDefault; use umbra::optional; @@ -141,7 +141,7 @@ pub struct CoreSearchConfig { #[derive(Debug, Clone, PartialEq, Eq, SmartDefault, Validate)] pub struct CoreUserCommandConfig { #[garde(dive)] - #[default(HashMap::from([("1".into(), UserCommand { + #[default(FxHashMap::from_iter([("1".into(), UserCommand { name: "git diff".into(), commands: vec![ "git".into(), @@ -152,7 +152,7 @@ pub struct CoreUserCommandConfig { "{{target_hash}}".into(), ], })]))] - pub commands: HashMap, + pub commands: FxHashMap, #[garde(range(min = 0))] #[default = 4] pub tab_width: u16, @@ -179,7 +179,7 @@ impl<'de> Deserialize<'de> for OptionalCoreUserCommandConfig { where V: MapAccess<'de>, { - let mut commands = HashMap::new(); + let mut commands = FxHashMap::default(); let mut tab_width = None; while let Some(key) = map.next_key::()? { @@ -379,7 +379,7 @@ mod tests { fuzzy: false, }, user_command: CoreUserCommandConfig { - commands: HashMap::from([( + commands: FxHashMap::from_iter([( "1".into(), UserCommand { name: "git diff".into(), @@ -491,7 +491,7 @@ mod tests { fuzzy: true, }, user_command: CoreUserCommandConfig { - commands: HashMap::from([ + commands: FxHashMap::from_iter([ ( "1".into(), UserCommand { @@ -578,7 +578,7 @@ mod tests { fuzzy: false, }, user_command: CoreUserCommandConfig { - commands: HashMap::from([( + commands: FxHashMap::from_iter([( "1".into(), UserCommand { name: "git diff".into(), diff --git a/src/git.rs b/src/git.rs index 824bb77..dca8e68 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,5 +1,4 @@ use std::{ - collections::HashMap, hash::Hash, io::{BufRead, BufReader}, path::{Path, PathBuf}, @@ -7,6 +6,7 @@ use std::{ }; use chrono::{DateTime, FixedOffset}; +use rustc_hash::FxHashMap; use crate::Result; @@ -104,10 +104,10 @@ pub enum SortCommit { Topological, } -type CommitMap = HashMap; -type CommitsMap = HashMap>; +type CommitMap = FxHashMap; +type CommitsMap = FxHashMap>; -type RefMap = HashMap>; +type RefMap = FxHashMap>; #[derive(Debug)] pub struct Repository { @@ -381,8 +381,8 @@ fn parse_parent_commit_hashes(s: &str) -> Vec { } fn build_commits_maps(commits: &Vec) -> (CommitsMap, CommitsMap) { - let mut parents_map: CommitsMap = HashMap::new(); - let mut children_map: CommitsMap = HashMap::new(); + let mut parents_map: CommitsMap = FxHashMap::default(); + let mut children_map: CommitsMap = FxHashMap::default(); for commit in commits { let hash = &commit.commit_hash; for parent_hash in &commit.parent_commit_hashes { @@ -411,12 +411,14 @@ fn merge_stashes_to_commits(commits: Vec, stashes: Vec) -> Vec> = - stashes.into_iter().fold(HashMap::new(), |mut acc, commit| { - let parent = commit.parent_commit_hashes[0].clone(); - acc.entry(parent).or_default().push(commit); - acc - }); + let mut statsh_map: FxHashMap> = + stashes + .into_iter() + .fold(FxHashMap::default(), |mut acc, commit| { + let parent = commit.parent_commit_hashes[0].clone(); + acc.entry(parent).or_default().push(commit); + acc + }); for commit in commits { if let Some(stashes) = statsh_map.remove(&commit.commit_hash) { for stash in stashes { @@ -443,8 +445,8 @@ fn load_refs(path: &Path) -> (RefMap, Head) { let reader = BufReader::new(stdout); - let mut ref_map = RefMap::new(); - let mut tag_map: HashMap = HashMap::new(); + let mut ref_map = RefMap::default(); + let mut tag_map: FxHashMap = FxHashMap::default(); let mut head: Option = None; for line in reader.lines() { @@ -504,7 +506,7 @@ fn load_stashes_as_refs(path: &Path) -> RefMap { let reader = BufReader::new(stdout); - let mut ref_map = RefMap::new(); + let mut ref_map = RefMap::default(); for line in reader.lines() { let line = line.unwrap(); diff --git a/src/keybind.rs b/src/keybind.rs index 62120e0..6174755 100644 --- a/src/keybind.rs +++ b/src/keybind.rs @@ -1,7 +1,7 @@ -use std::collections::HashMap; use std::ops::{Deref, DerefMut}; use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; +use rustc_hash::FxHashMap; use serde::{de::Deserializer, Deserialize}; use crate::event::UserEvent; @@ -9,10 +9,10 @@ use crate::event::UserEvent; const DEFAULT_KEY_BIND: &str = include_str!("../assets/default-keybind.toml"); #[derive(Debug, Default, Clone, PartialEq, Eq)] -pub struct KeyBind(HashMap); +pub struct KeyBind(FxHashMap); impl Deref for KeyBind { - type Target = HashMap; + type Target = FxHashMap; fn deref(&self) -> &Self::Target { &self.0 @@ -70,8 +70,8 @@ impl<'de> Deserialize<'de> for KeyBind { where D: Deserializer<'de>, { - let parsed_map = HashMap::>::deserialize(deserializer)?; - let mut key_map = HashMap::::new(); + let parsed_map = FxHashMap::>::deserialize(deserializer)?; + let mut key_map = FxHashMap::::default(); for (user_event, key_events) in parsed_map { for key_event_str in key_events { let key_event = match parse_key_event(&key_event_str) { diff --git a/src/widget/commit_list.rs b/src/widget/commit_list.rs index b2107cd..1391c48 100644 --- a/src/widget/commit_list.rs +++ b/src/widget/commit_list.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use laurier::highlight::highlight_matched_text; use once_cell::sync::Lazy; @@ -11,6 +9,7 @@ use ratatui::{ text::{Line, Span}, widgets::{List, ListItem, StatefulWidget, Widget}, }; +use rustc_hash::FxHashMap; use tui_input::{backend::crossterm::EventHandler, Input}; use crate::{ @@ -78,7 +77,7 @@ pub enum TransientMessage { #[derive(Debug, Default, Clone)] struct SearchMatch { - refs: HashMap, + refs: FxHashMap, subject: Option, author_name: Option, commit_hash: Option, @@ -185,7 +184,7 @@ pub struct CommitListState<'a> { graph_cell_width: u16, head: &'a Head, - ref_name_to_commit_index_map: HashMap<&'a str, usize>, + ref_name_to_commit_index_map: FxHashMap<&'a str, usize>, search_state: SearchState, search_input: Input, @@ -206,7 +205,7 @@ impl<'a> CommitListState<'a> { graph_image_manager: GraphImageManager<'a>, graph_cell_width: u16, head: &'a Head, - ref_name_to_commit_index_map: HashMap<&'a str, usize>, + ref_name_to_commit_index_map: FxHashMap<&'a str, usize>, default_ignore_case: bool, default_fuzzy: bool, ) -> CommitListState<'a> { @@ -961,7 +960,7 @@ impl CommitList<'_> { fn refs_spans<'a>( commit_info: &'a CommitInfo, head: &'a Head, - refs_matches: &'a HashMap, + refs_matches: &'a FxHashMap, color_theme: &'a ColorTheme, ) -> Vec> { let refs = &commit_info.refs;