Skip to content

Commit

Permalink
[Turbopack] AutoMap, AutoSet and CountHashSet use FxHasher by default (
Browse files Browse the repository at this point in the history
…vercel#70691)

### What?

Use FxHasher instead of RandomState for better performance and less
memory

### Why?

There is no risk of a DoS attacks here
  • Loading branch information
sokra authored Oct 3, 2024
1 parent f5f424c commit 6f6ce41
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 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 turbopack/crates/turbo-tasks-auto-hash-map/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
workspace = true

[dependencies]
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
smallvec = { workspace = true }
9 changes: 5 additions & 4 deletions turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{
borrow::Borrow,
collections::{hash_map::RandomState, HashMap},
collections::HashMap,
fmt::{Debug, Formatter},
hash::{BuildHasher, Hash},
hash::{BuildHasher, BuildHasherDefault, Hash},
marker::PhantomData,
};

use rustc_hash::FxHasher;
use serde::{
de::{MapAccess, Visitor},
ser::SerializeMap,
Expand All @@ -16,7 +17,7 @@ use smallvec::SmallVec;
use crate::{MAX_LIST_SIZE, MIN_HASH_SIZE};

#[derive(Clone)]
pub enum AutoMap<K, V, H = RandomState, const I: usize = 0> {
pub enum AutoMap<K, V, H = BuildHasherDefault<FxHasher>, const I: usize = 0> {
List(SmallVec<[(K, V); I]>),
Map(Box<HashMap<K, V, H>>),
}
Expand All @@ -33,7 +34,7 @@ impl<K: Debug, V: Debug, H, const I: usize> Debug for AutoMap<K, V, H, I> {
}
}

impl<K, V> AutoMap<K, V, RandomState, 0> {
impl<K, V> AutoMap<K, V, BuildHasherDefault<FxHasher>, 0> {
/// see [HashMap::new](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.new)
pub const fn new() -> Self {
AutoMap::List(SmallVec::new_const())
Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbo-tasks-auto-hash-map/src/set.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::{
collections::hash_map::RandomState,
fmt::Debug,
hash::{BuildHasher, Hash},
hash::{BuildHasher, BuildHasherDefault, Hash},
marker::PhantomData,
};

use rustc_hash::FxHasher;
use serde::{Deserialize, Serialize};

use crate::AutoMap;

#[derive(Clone)]
pub struct AutoSet<K, H = RandomState, const I: usize = 0> {
pub struct AutoSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> {
map: AutoMap<K, (), H, I>,
}

Expand All @@ -28,7 +28,7 @@ impl<K: Debug, H, const I: usize> Debug for AutoSet<K, H, I> {
}
}

impl<K> AutoSet<K, RandomState, 0> {
impl<K> AutoSet<K, BuildHasherDefault<FxHasher>, 0> {
/// see [HashSet::new](https://doc.rust-lang.org/std/collections/hash_set/struct.HashSet.html#method.new)
pub const fn new() -> Self {
Self {
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbo-tasks-memory/src/count_hash_set.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::{
borrow::Borrow,
collections::hash_map::RandomState,
fmt::{Debug, Formatter},
hash::{BuildHasher, Hash},
hash::{BuildHasher, BuildHasherDefault, Hash},
iter::FilterMap,
};

use auto_hash_map::{
map::{Entry, Iter, RawEntry},
AutoMap,
};
use rustc_hash::FxHasher;

#[derive(Clone)]
pub struct CountHashSet<T, H = RandomState> {
pub struct CountHashSet<T, H = BuildHasherDefault<FxHasher>> {
inner: AutoMap<T, isize, H>,
negative_entries: usize,
}
Expand Down

0 comments on commit 6f6ce41

Please sign in to comment.