Use fixed seeds with ahash

This commit is contained in:
mo8it 2024-08-08 23:46:21 +02:00
parent 1b9faa4d61
commit e41c3a7c92
6 changed files with 18 additions and 19 deletions

12
Cargo.lock generated
View file

@ -9,7 +9,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"getrandom",
"once_cell", "once_cell",
"version_check", "version_check",
"zerocopy", "zerocopy",
@ -263,17 +262,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.5" version = "0.14.5"

View file

@ -46,7 +46,7 @@ include = [
] ]
[dependencies] [dependencies]
ahash = "0.8.11" ahash = { version = "0.8.11", default-features = false }
anyhow = "1.0.86" anyhow = "1.0.86"
clap = { version = "4.5.13", features = ["derive"] } clap = { version = "4.5.13", features = ["derive"] }
notify-debouncer-mini = { version = "0.4.1", default-features = false } notify-debouncer-mini = { version = "0.4.1", default-features = false }

View file

@ -1,4 +1,3 @@
use ahash::{HashSet, HashSetExt};
use anyhow::{bail, Context, Error, Result}; use anyhow::{bail, Context, Error, Result};
use std::{ use std::{
fs::{self, File}, fs::{self, File},
@ -11,6 +10,7 @@ use std::{
use crate::{ use crate::{
clear_terminal, clear_terminal,
cmd::CmdRunner, cmd::CmdRunner,
collections::hash_set_with_capacity,
embedded::EMBEDDED_FILES, embedded::EMBEDDED_FILES,
exercise::{Exercise, RunnableExercise}, exercise::{Exercise, RunnableExercise},
info_file::ExerciseInfo, info_file::ExerciseInfo,
@ -70,7 +70,7 @@ impl AppState {
return StateFileStatus::NotRead; 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 { for done_exerise_name in lines {
if done_exerise_name.is_empty() { if done_exerise_name.is_empty() {

10
src/collections.rs Normal file
View file

@ -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<T> = std::collections::HashSet<T, BuildHasherDefault<AHasher>>;
#[inline]
pub fn hash_set_with_capacity<T>(capacity: usize) -> HashSet<T> {
HashSet::with_capacity_and_hasher(capacity, BuildHasherDefault::<AHasher>::default())
}

View file

@ -1,4 +1,3 @@
use ahash::{HashSet, HashSetExt};
use anyhow::{anyhow, bail, Context, Error, Result}; use anyhow::{anyhow, bail, Context, Error, Result};
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
@ -12,6 +11,7 @@ use std::{
use crate::{ use crate::{
cargo_toml::{append_bins, bins_start_end_ind, BINS_BUFFER_CAPACITY}, cargo_toml::{append_bins, bins_start_end_ind, BINS_BUFFER_CAPACITY},
cmd::CmdRunner, cmd::CmdRunner,
collections::{hash_set_with_capacity, HashSet},
exercise::{RunnableExercise, OUTPUT_CAPACITY}, exercise::{RunnableExercise, OUTPUT_CAPACITY},
info_file::{ExerciseInfo, InfoFile}, info_file::{ExerciseInfo, InfoFile},
CURRENT_FORMAT_VERSION, CURRENT_FORMAT_VERSION,
@ -50,8 +50,8 @@ fn check_cargo_toml(
// Check the info of all exercises and return their paths in a set. // Check the info of all exercises and return their paths in a set.
fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> { fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
let mut names = HashSet::with_capacity(info_file.exercises.len()); let mut names = hash_set_with_capacity(info_file.exercises.len());
let mut paths = HashSet::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); let mut file_buf = String::with_capacity(1 << 14);
for exercise_info in &info_file.exercises { for exercise_info in &info_file.exercises {
@ -251,7 +251,7 @@ fn check_solutions(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
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"); let mut fmt_cmd = Command::new("rustfmt");
fmt_cmd fmt_cmd
.arg("--check") .arg("--check")

View file

@ -13,6 +13,7 @@ use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::Wa
mod app_state; mod app_state;
mod cargo_toml; mod cargo_toml;
mod cmd; mod cmd;
mod collections;
mod dev; mod dev;
mod embedded; mod embedded;
mod exercise; mod exercise;