Skip to content
New issue

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

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

Already on GitHub? # to your account

Drop aHash dependency in favor of hashbrown's choice of default hasher #17

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ maintenance = { status = "actively-developed" }


[dependencies]
ahash = "0.8.0"
hashbrown = { version = "0.15", default-features = false, features = ["inline-more"] }
hashbrown = { version = "0.15", default-features = false, features = ["default-hasher", "inline-more"] }

[features]
default = ["unified_diff"]
Expand Down
25 changes: 17 additions & 8 deletions src/intern.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::hash::Hash;
use std::hash::{BuildHasher as _, Hash, Hasher as _};
use std::ops::Index;

use ahash::RandomState;
use hashbrown::hash_table::{Entry, HashTable};
use hashbrown::DefaultHashBuilder as RandomState;

/// A token represented as an interned integer.
///
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<T: Hash + Eq> Interner<T> {
Interner {
tokens: Vec::with_capacity(capacity),
table: HashTable::with_capacity(capacity),
hasher: RandomState::new(),
hasher: RandomState::default(),
}
}

Expand All @@ -126,11 +126,11 @@ impl<T: Hash + Eq> Interner<T> {

/// Intern `token` and return a the interned integer
pub fn intern(&mut self, token: T) -> Token {
let hash = self.hasher.hash_one(&token);
let hash = hash_one(&self.hasher, &token);
match self.table.entry(
hash,
|&it| self.tokens[it.0 as usize] == token,
|&token| self.hasher.hash_one(&self.tokens[token.0 as usize]),
|&token| hash_one(&self.hasher, &self.tokens[token.0 as usize]),
) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
Expand All @@ -155,14 +155,14 @@ impl<T: Hash + Eq> Interner<T> {
if retained <= erased {
self.table.clear();
for (i, token) in self.tokens[0..retained].iter().enumerate() {
let hash = self.hasher.hash_one(token);
let hash = hash_one(&self.hasher, &token);
self.table.insert_unique(hash, Token(i as u32), |&token| {
self.hasher.hash_one(&self.tokens[token.0 as usize])
hash_one(&self.hasher, &self.tokens[token.0 as usize])
});
}
} else {
for (i, token) in self.tokens[retained..].iter().enumerate() {
let hash = self.hasher.hash_one(token);
let hash = hash_one(&self.hasher, &token);
match self
.table
.find_entry(hash, |token| token.0 == (retained + i) as u32)
Expand All @@ -182,3 +182,12 @@ impl<T: Hash + Eq> Index<Token> for Interner<T> {
&self.tokens[index.0 as usize]
}
}

// TODO: remove in favor of BuildHasher::hash_one once compilers older than 1.71
// no longer need to be supported.
Copy link
Owner

@pascalkuthe pascalkuthe Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with bunping msrv for this, I loosly follow firefox MSRV and that is on 1.76 at this point (up to you to you want to do that in this PR, if lot let me know and I will merge as-is)

// https://doc.rust-lang.org/std/hash/trait.BuildHasher.html#method.hash_one
fn hash_one<T: Hash>(hasher_parameters: &RandomState, token: &T) -> u64 {
let mut hasher = hasher_parameters.build_hasher();
token.hash(&mut hasher);
hasher.finish()
}
Loading