Skip to content

Commit

Permalink
Change hash function for podcast/episode hash maps
Browse files Browse the repository at this point in the history
Default hash map implementation uses a cryptographically secure and DoS
resistant hasher, which is unnecessary in this case. Switched it out
to just use the i64 value as the lookup key directly.
  • Loading branch information
jeff-hughes committed Apr 26, 2021
1 parent defdddd commit 8e2267a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sanitize-filename = "0.2.1"
shellexpand = "2.0.0"
dirs = { package = "dirs-next", version = "1.0.1" }
opml = "0.2.4"
nohash-hasher = "0.2.0"
unicode-segmentation = "1.6.0"
textwrap = "0.12.1"
escaper = "0.1.0"
Expand Down
15 changes: 11 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use unicode_segmentation::UnicodeSegmentation;

use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use nohash_hasher::BuildNoHashHasher;
use regex::Regex;

use crate::downloads::DownloadMsg;
Expand Down Expand Up @@ -273,14 +274,14 @@ impl Menuable for NewEpisode {
pub struct LockVec<T>
where T: Clone + Menuable
{
data: Arc<Mutex<HashMap<i64, T>>>,
data: Arc<Mutex<HashMap<i64, T, BuildNoHashHasher<i64>>>>,
order: Arc<Mutex<Vec<i64>>>,
}

impl<T: Clone + Menuable> LockVec<T> {
/// Create a new LockVec.
pub fn new(data: Vec<T>) -> LockVec<T> {
let mut hm = HashMap::new();
let mut hm = HashMap::with_hasher(BuildNoHashHasher::default());
let mut order = Vec::new();
for i in data.into_iter() {
let id = i.get_id();
Expand All @@ -295,7 +296,7 @@ impl<T: Clone + Menuable> LockVec<T> {
}

/// Lock the LockVec hashmap for reading/writing.
pub fn borrow_map(&self) -> MutexGuard<HashMap<i64, T>> {
pub fn borrow_map(&self) -> MutexGuard<HashMap<i64, T, BuildNoHashHasher<i64>>> {
return self.data.lock().unwrap();
}

Expand All @@ -305,7 +306,13 @@ impl<T: Clone + Menuable> LockVec<T> {
}

/// Lock the LockVec hashmap for reading/writing.
pub fn borrow(&self) -> (MutexGuard<HashMap<i64, T>>, MutexGuard<Vec<i64>>) {
#[allow(clippy::type_complexity)]
pub fn borrow(
&self,
) -> (
MutexGuard<HashMap<i64, T, BuildNoHashHasher<i64>>>,
MutexGuard<Vec<i64>>,
) {
return (self.data.lock().unwrap(), self.order.lock().unwrap());
}

Expand Down

0 comments on commit 8e2267a

Please sign in to comment.