From 3da3d9eef4f962c5c0e816a419be68c9a99777ad Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sat, 14 May 2022 11:51:24 -0400 Subject: [PATCH] More FxHashMap usage --- src/side_by_side.rs | 18 +++++++----------- src/style.rs | 10 ++++------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/side_by_side.rs b/src/side_by_side.rs index a5e94f03b..45d06bc85 100644 --- a/src/side_by_side.rs +++ b/src/side_by_side.rs @@ -1,10 +1,8 @@ //! Side-by-side (two column) display of diffs. use owo_colors::{OwoColorize, Style}; -use std::{ - cmp::max, - collections::{HashMap, HashSet}, -}; +use rustc_hash::FxHashMap; +use std::{cmp::max, collections::HashSet}; use crate::{ constants::Side, @@ -250,21 +248,19 @@ fn highlight_positions( lhs_mps: &[MatchedPos], rhs_mps: &[MatchedPos], ) -> ( - HashMap>, - HashMap>, + FxHashMap>, + FxHashMap>, ) { let lhs_positions = color_positions(true, background, syntax_highlight, lhs_mps); // Preallocate the hashmap assuming the average line will have 2 items on it. - let mut lhs_styles: HashMap> = - HashMap::with_capacity(lhs_positions.len() / 2); + let mut lhs_styles: FxHashMap> = FxHashMap::default(); for (span, style) in lhs_positions { let styles = lhs_styles.entry(span.line).or_insert_with(Vec::new); styles.push((span, style)); } let rhs_positions = color_positions(false, background, syntax_highlight, rhs_mps); - let mut rhs_styles: HashMap> = - HashMap::with_capacity(rhs_positions.len() / 2); + let mut rhs_styles: FxHashMap> = FxHashMap::default(); for (span, style) in rhs_positions { let styles = rhs_styles.entry(span.line).or_insert_with(Vec::new); styles.push((span, style)); @@ -367,7 +363,7 @@ pub fn print( rhs_mps, ) } else { - (HashMap::new(), HashMap::new()) + (FxHashMap::default(), FxHashMap::default()) }; let lhs_lines = split_on_newlines(lhs_src); diff --git a/src/style.rs b/src/style.rs index fc9b680f2..f5a4c243a 100644 --- a/src/style.rs +++ b/src/style.rs @@ -8,10 +8,8 @@ use crate::{ syntax::{AtomKind, MatchKind, MatchedPos, TokenKind}, }; use owo_colors::{OwoColorize, Style}; -use std::{ - cmp::{max, min}, - collections::HashMap, -}; +use rustc_hash::FxHashMap; +use std::cmp::{max, min}; #[derive(Clone, Copy, Debug)] pub enum BackgroundColor { @@ -196,8 +194,8 @@ fn apply_line(line: &str, styles: &[(SingleLineSpan, Style)]) -> String { fn group_by_line( ranges: &[(SingleLineSpan, Style)], -) -> HashMap> { - let mut ranges_by_line: HashMap<_, Vec<_>> = HashMap::with_capacity(ranges.len()); +) -> FxHashMap> { + let mut ranges_by_line: FxHashMap<_, Vec<_>> = FxHashMap::default(); for range in ranges { if let Some(matching_ranges) = ranges_by_line.get_mut(&range.0.line) { (*matching_ranges).push(*range);