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

Use FNV hasher #276

Closed
wants to merge 4 commits into from
Closed

Use FNV hasher #276

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented May 5, 2018

The default HashMap uses SipHash, which is great for collision resistance, but can be slow in general cases where this isn't necessary. Specifically, it is much faster for small keys. This switches to the FNV hasher (using the very small fnv crate) for the HashMaps in .unique() and .into_group_map(). This results in faster Iterators with very little work.

@ghost
Copy link
Author

ghost commented May 5, 2018

To back up the claim that it is faster for small keys: on my MacBook the benchmark I added runs at 26,115 ns/iter (+/- 2,329) on the default SipHash HashMap and at 9,890 ns/iter (+/- 549) after switching to the FNV hasher.

Austin Kiekintveld added 2 commits May 5, 2018 16:14
Accidentally reformatted lib.rs using rustfmt. This undoes that.
@bluss
Copy link
Member

bluss commented May 10, 2018

Hi, thanks for this thoughtful improvement. We will not add more dependencies right now; itertools has a bunch of different functionality, and for most itertools users this will be a dependency that they don't have any benefit from. For that reason, I'll decline this PR.

@@ -1901,7 +1902,7 @@ pub trait Itertools : Iterator {
/// assert_eq!(lookup[&3], vec![13, 33]);
/// ```
#[cfg(feature = "use_std")]
fn into_group_map<K, V>(self) -> HashMap<K, Vec<V>>
fn into_group_map<K, V>(self) -> FnvHashMap<K, Vec<V>>
Copy link
Member

Choose a reason for hiding this comment

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

Now it won't matter in this PR, but we'd usually be mindful here - the function signature changes, so it's a breaking change.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review! I completely understand the aversion to adding more dependencies. I would like to note that FnvHashMap is just a type alias, so the change would technically be from HashMap<K, Vec<V>> to HashMap<K, Vec<V>, FnvBuildHasher>, so still a signature change but the hasher is more of an implementation detail.

@bluss bluss closed this May 11, 2018
@ghost ghost deleted the faster-unique branch May 12, 2018 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant