Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use FxHash instead of SipHash where it makes sense #237

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion benches/results_list_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use ratatui::layout::Alignment;
use ratatui::prelude::{Line, Style};
use ratatui::style::Color;
use ratatui::widgets::{Block, BorderType, Borders, ListDirection, Padding};
use rustc_hash::FxHashMap;
use television_channels::entry::merge_ranges;
use television_channels::entry::{Entry, PreviewType};
use television_screen::colors::ResultsColorscheme;
use television_screen::results::build_results_list;

pub fn results_list_benchmark(c: &mut Criterion) {
let mut icon_color_cache = std::collections::HashMap::default();
let mut icon_color_cache = FxHashMap::default();
// FIXME: there's probably a way to have this as a benchmark asset
// possible as a JSON file and to load it for the benchmark using Serde
// I don't know how exactly right now just having it here instead
Expand Down Expand Up @@ -634,6 +635,7 @@ pub fn results_list_benchmark(c: &mut Criterion) {
result_name_fg: Color::Indexed(222),
result_preview_fg: Color::Indexed(222),
result_line_number_fg: Color::Indexed(222),
result_selected_fg: Color::Indexed(222),
result_selected_bg: Color::Indexed(222),
match_foreground_color: Color::Indexed(222),
};
Expand All @@ -651,6 +653,7 @@ pub fn results_list_benchmark(c: &mut Criterion) {
.style(Style::default())
.padding(Padding::right(1)),
&entries,
None,
ListDirection::BottomToTop,
false,
&mut icon_color_cache,
Expand Down
1 change: 1 addition & 0 deletions crates/television-channels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ color-eyre = { workspace = true }
serde = { workspace = true }
lazy_static = { workspace = true }
toml = { workspace = true }
rustc-hash = { workspace = true }

devicons = "0.6.11"
ignore = "0.4.23"
Expand Down
6 changes: 3 additions & 3 deletions crates/television-channels/src/cable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hash::FxHashMap;
use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
ops::Deref,
};
Expand Down Expand Up @@ -27,10 +27,10 @@ impl Display for CableChannelPrototype {
}

#[derive(Debug, serde::Deserialize, Default)]
pub struct CableChannels(pub HashMap<String, CableChannelPrototype>);
pub struct CableChannels(pub FxHashMap<String, CableChannelPrototype>);

impl Deref for CableChannels {
type Target = HashMap<String, CableChannelPrototype>;
type Target = FxHashMap<String, CableChannelPrototype>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
5 changes: 2 additions & 3 deletions crates/television-channels/src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashSet;

use crate::entry::Entry;
use color_eyre::Result;
use rustc_hash::FxHashSet;
use television_derive::{Broadcast, ToCliChannel, ToUnitChannel};

mod alias;
Expand Down Expand Up @@ -68,7 +67,7 @@ pub trait OnAir: Send {
fn get_result(&self, index: u32) -> Option<Entry>;

/// Get the currently selected entries.
fn selected_entries(&self) -> &HashSet<Entry>;
fn selected_entries(&self) -> &FxHashSet<Entry>;

/// Toggles selection for the entry under the cursor.
fn toggle_selection(&mut self, entry: &Entry);
Expand Down
8 changes: 5 additions & 3 deletions crates/television-channels/src/channels/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::channels::OnAir;
use crate::entry::Entry;
use crate::entry::PreviewType;
use devicons::FileIcon;
use rustc_hash::FxBuildHasher;
use rustc_hash::FxHashSet;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::indices::sep_name_and_value_indices;
use tracing::debug;
Expand All @@ -23,7 +25,7 @@ impl Alias {
pub struct Channel {
matcher: Matcher<Alias>,
file_icon: FileIcon,
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

const NUM_THREADS: usize = 1;
Expand Down Expand Up @@ -55,7 +57,7 @@ impl Channel {
Self {
matcher,
file_icon: FileIcon::from(FILE_ICON_STR),
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -119,7 +121,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
10 changes: 6 additions & 4 deletions crates/television-channels/src/channels/cable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashSet;

use color_eyre::Result;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashSet;
use rustc_hash::{FxBuildHasher, FxHashSet};
use tracing::debug;

use crate::cable::{CableChannelPrototype, DEFAULT_DELIMITER};
Expand All @@ -26,7 +28,7 @@ pub struct Channel {
matcher: Matcher<String>,
entries_command: String,
preview_kind: PreviewKind,
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

impl Default for Channel {
Expand Down Expand Up @@ -94,7 +96,7 @@ impl Channel {
entries_command: entries_command.to_string(),
preview_kind,
name: name.to_string(),
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -164,7 +166,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
7 changes: 4 additions & 3 deletions crates/television-channels/src/channels/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::channels::{OnAir, TelevisionChannel};
use crate::entry::{Entry, PreviewCommand, PreviewType};
use devicons::FileIcon;
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::collections::HashSet;
use std::path::PathBuf;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
Expand All @@ -11,7 +12,7 @@ pub struct Channel {
crawl_handle: tokio::task::JoinHandle<()>,
// PERF: cache results (to make deleting characters smoother) with
// a shallow stack of sub-patterns as keys (e.g. "a", "ab", "abc")
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

impl Channel {
Expand All @@ -22,7 +23,7 @@ impl Channel {
Channel {
matcher,
crawl_handle,
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -114,7 +115,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
7 changes: 4 additions & 3 deletions crates/television-channels/src/channels/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;

use devicons::FileIcon;
use rustc_hash::{FxBuildHasher, FxHashSet};

use super::OnAir;
use crate::entry::{Entry, PreviewType};
Expand All @@ -17,7 +18,7 @@ struct EnvVar {
pub struct Channel {
matcher: Matcher<EnvVar>,
file_icon: FileIcon,
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

const NUM_THREADS: usize = 1;
Expand All @@ -35,7 +36,7 @@ impl Channel {
Channel {
matcher,
file_icon: FileIcon::from(FILE_ICON_STR),
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -99,7 +100,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
11 changes: 6 additions & 5 deletions crates/television-channels/src/channels/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::channels::{OnAir, TelevisionChannel};
use crate::entry::{Entry, PreviewType};
use devicons::FileIcon;
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::collections::HashSet;
use std::path::PathBuf;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
Expand All @@ -11,7 +12,7 @@ pub struct Channel {
crawl_handle: tokio::task::JoinHandle<()>,
// PERF: cache results (to make deleting characters smoother) with
// a shallow stack of sub-patterns as keys (e.g. "a", "ab", "abc")
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

impl Channel {
Expand All @@ -22,7 +23,7 @@ impl Channel {
Channel {
matcher,
crawl_handle,
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -72,7 +73,7 @@ impl From<&mut TelevisionChannel> for Channel {
entries
.iter()
.map(|entry| PathBuf::from(&entry.name))
.collect::<HashSet<_>>()
.collect::<FxHashSet<_>>()
.into_iter()
.collect(),
)
Expand All @@ -83,7 +84,7 @@ impl From<&mut TelevisionChannel> for Channel {
entries
.iter()
.map(|entry| PathBuf::from(&entry.name))
.collect::<HashSet<_>>()
.collect::<FxHashSet<_>>()
.into_iter()
.collect(),
)
Expand Down Expand Up @@ -120,7 +121,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
7 changes: 4 additions & 3 deletions crates/television-channels/src/channels/git_repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use devicons::FileIcon;
use directories::BaseDirs;
use ignore::overrides::OverrideBuilder;
use lazy_static::lazy_static;
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::collections::HashSet;
use std::path::PathBuf;
use tokio::task::JoinHandle;
Expand All @@ -16,7 +17,7 @@ pub struct Channel {
matcher: Matcher<String>,
icon: FileIcon,
crawl_handle: JoinHandle<()>,
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

impl Channel {
Expand All @@ -31,7 +32,7 @@ impl Channel {
matcher,
icon: FileIcon::from("git"),
crawl_handle,
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ impl OnAir for Channel {
})
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
7 changes: 4 additions & 3 deletions crates/television-channels/src/channels/remote_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use crate::entry::{Entry, PreviewType};
use clap::ValueEnum;
use color_eyre::Result;
use devicons::FileIcon;
use rustc_hash::{FxBuildHasher, FxHashSet};
use television_fuzzy::matcher::{config::Config, Matcher};

use super::cable;

pub struct RemoteControl {
matcher: Matcher<RCButton>,
cable_channels: Option<CableChannels>,
selected_entries: HashSet<Entry>,
selected_entries: FxHashSet<Entry>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -61,7 +62,7 @@ impl RemoteControl {
RemoteControl {
matcher,
cable_channels,
selected_entries: HashSet::new(),
selected_entries: HashSet::with_hasher(FxBuildHasher),
}
}

Expand Down Expand Up @@ -143,7 +144,7 @@ impl OnAir for RemoteControl {
.collect()
}

fn selected_entries(&self) -> &HashSet<Entry> {
fn selected_entries(&self) -> &FxHashSet<Entry> {
&self.selected_entries
}

Expand Down
Loading
Loading