Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Avoid hashing for single-entry maps #316

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
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
40 changes: 27 additions & 13 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,13 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
None
} else {
let hash = self.hash(key);
self.core.get_index_of(hash, key)
match self.as_entries() {
[] => None,
[x] => key.equivalent(&x.key).then_some(0),
_ => {
let hash = self.hash(key);
self.core.get_index_of(hash, key)
}
}
}

Expand Down Expand Up @@ -676,11 +678,17 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
return None;
match self.as_entries() {
[x] if key.equivalent(&x.key) => {
let (k, v) = self.core.pop()?;
Some((0, k, v))
}
[_] | [] => None,
_ => {
let hash = self.hash(key);
self.core.swap_remove_full(hash, key)
}
}
let hash = self.hash(key);
self.core.swap_remove_full(hash, key)
}

/// Remove the key-value pair equivalent to `key` and return
Expand Down Expand Up @@ -733,11 +741,17 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
return None;
match self.as_entries() {
[x] if key.equivalent(&x.key) => {
let (k, v) = self.core.pop()?;
Some((0, k, v))
}
[_] | [] => None,
_ => {
let hash = self.hash(key);
self.core.shift_remove_full(hash, key)
}
}
let hash = self.hash(key);
self.core.shift_remove_full(hash, key)
}
}

Expand Down