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

Stop hashing a usize when hashing [u8; 1] #86131

Closed
wants to merge 1 commit into from

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jun 8, 2021

This changes Hash for arrays to be implemented via Hash::hash_slice instead of via <[T]>::hash. The latter calls the former, but only after also hashing the length, which is unnecessary overhead for arrays where it's always the same.

I was inspired to make this PR because I changed an old project from having a HashMap<(usize, usize), _> to having a HashMap<[usize; 2], _>, and was surprised to see the perf worsen from 3.62s to 4.30s.

(Yes, that project really is dominated by hash table lookups that badly.)

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2021
///
/// For example, the hash of the array `[x, y, z]` may or may not be the same as
/// the hash of the tuple `(x, y, z)`, and the hash of the array `[x, y, z]` may
/// or may not be the same as the hash of the vector `vec![x, y, z]`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be in conflict with requirements of the Borrow trait which is implemented for arrays and slices.

Copy link
Contributor

@camsteffen camsteffen Jun 8, 2021

Choose a reason for hiding this comment

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

Here's a playground. Shows the problem and a workaround.

@scottmcm
Copy link
Member Author

scottmcm commented Jun 8, 2021

🤦

Thanks, @petrochenkov. I'd checked Unsize for such a requirement, but forgot about Borrow.

That's definitely fatal to this change. I'll make a new PR so there's actually a test failure if the implementation is changed.

Edit: New PR up at #86140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants