More FxHashMap usage

accurate_ancestors_with_fallback
Wilfred Hughes 2022-05-14 11:51:24 +07:00
parent 6a0858af11
commit 3da3d9eef4
2 changed files with 11 additions and 17 deletions

@ -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<LineNumber, Vec<(SingleLineSpan, Style)>>,
HashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
) {
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<LineNumber, Vec<(SingleLineSpan, Style)>> =
HashMap::with_capacity(lhs_positions.len() / 2);
let mut lhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = 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<LineNumber, Vec<(SingleLineSpan, Style)>> =
HashMap::with_capacity(rhs_positions.len() / 2);
let mut rhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = 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);

@ -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<LineNumber, Vec<(SingleLineSpan, Style)>> {
let mut ranges_by_line: HashMap<_, Vec<_>> = HashMap::with_capacity(ranges.len());
) -> FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> {
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);