From e41c3a7c925387ca2c2441b4f41c963b95bc828d Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 8 Aug 2024 23:46:21 +0200 Subject: Use fixed seeds with ahash --- src/app_state.rs | 4 ++-- src/collections.rs | 10 ++++++++++ src/dev/check.rs | 8 ++++---- src/main.rs | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/collections.rs (limited to 'src') diff --git a/src/app_state.rs b/src/app_state.rs index ac45bfc..b72469c 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -1,4 +1,3 @@ -use ahash::{HashSet, HashSetExt}; use anyhow::{bail, Context, Error, Result}; use std::{ fs::{self, File}, @@ -11,6 +10,7 @@ use std::{ use crate::{ clear_terminal, cmd::CmdRunner, + collections::hash_set_with_capacity, embedded::EMBEDDED_FILES, exercise::{Exercise, RunnableExercise}, info_file::ExerciseInfo, @@ -70,7 +70,7 @@ impl AppState { return StateFileStatus::NotRead; } - let mut done_exercises = HashSet::with_capacity(self.exercises.len()); + let mut done_exercises = hash_set_with_capacity(self.exercises.len()); for done_exerise_name in lines { if done_exerise_name.is_empty() { diff --git a/src/collections.rs b/src/collections.rs new file mode 100644 index 0000000..fa9e3fa --- /dev/null +++ b/src/collections.rs @@ -0,0 +1,10 @@ +use ahash::AHasher; +use std::hash::BuildHasherDefault; + +/// DOS attacks aren't a concern for Rustlings. Therefore, we use `ahash` with fixed seeds. +pub type HashSet = std::collections::HashSet>; + +#[inline] +pub fn hash_set_with_capacity(capacity: usize) -> HashSet { + HashSet::with_capacity_and_hasher(capacity, BuildHasherDefault::::default()) +} diff --git a/src/dev/check.rs b/src/dev/check.rs index 7b17274..ca1b30c 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -1,4 +1,3 @@ -use ahash::{HashSet, HashSetExt}; use anyhow::{anyhow, bail, Context, Error, Result}; use std::{ cmp::Ordering, @@ -12,6 +11,7 @@ use std::{ use crate::{ cargo_toml::{append_bins, bins_start_end_ind, BINS_BUFFER_CAPACITY}, cmd::CmdRunner, + collections::{hash_set_with_capacity, HashSet}, exercise::{RunnableExercise, OUTPUT_CAPACITY}, info_file::{ExerciseInfo, InfoFile}, CURRENT_FORMAT_VERSION, @@ -50,8 +50,8 @@ fn check_cargo_toml( // Check the info of all exercises and return their paths in a set. fn check_info_file_exercises(info_file: &InfoFile) -> Result> { - let mut names = HashSet::with_capacity(info_file.exercises.len()); - let mut paths = HashSet::with_capacity(info_file.exercises.len()); + let mut names = hash_set_with_capacity(info_file.exercises.len()); + let mut paths = hash_set_with_capacity(info_file.exercises.len()); let mut file_buf = String::with_capacity(1 << 14); for exercise_info in &info_file.exercises { @@ -251,7 +251,7 @@ fn check_solutions( }) .collect::>(); - let mut sol_paths = HashSet::with_capacity(info_file.exercises.len()); + let mut sol_paths = hash_set_with_capacity(info_file.exercises.len()); let mut fmt_cmd = Command::new("rustfmt"); fmt_cmd .arg("--check") diff --git a/src/main.rs b/src/main.rs index edb3e14..58cd8ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::Wa mod app_state; mod cargo_toml; mod cmd; +mod collections; mod dev; mod embedded; mod exercise; -- cgit v1.2.3