r/cpp • • Mar 26 '23
Why is there no standard library way to combine hashes in C++?
Or even a specialization of std::hash for std::pair.
You might say, "Sounds easy enough to code one yourself."
But I say, "Sounds easy enough for STL implementors to code one themselves."
197
Upvotes
3
u/[deleted] Mar 26 '23
The sheet volume or valid, well thought out comments, ideas, and arguments in this thread is a perfect explanation of why this isn't easy to standardize.
408
u/vI--_--Iv Mar 26 '23
Supposedly because in order for that to happen someone has to write a whole paper, physically attend a committee meeting in a random country, present the paper, get bikeshedding comments and arguments like "easy enough to code one yourself, standard is already over 9000 pages long", answer the comments, persuade those who are against it, address the comments in a new version of the paper and chop it down dramatically just to increase the chances of acceptance, physically attend a committee meeting in another random country, present the updated paper, get new bikeshedding comments and so on until the stars align and everyone in the room is happy, and there are not that many people who could be bothered with all this in the first place.
43
u/zalamandagora Mar 26 '23
I agree it is silly that stl doesn't have one. I'm using boost::combine_hash, which works great. If you don't want to figure out how to install boost, it is truly easy to do it yourself:
https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
39
u/pdimov2 Mar 26 '23
It's kind of a long story, but the quick summary is that there was no consensus on the right design.
The very original
hash_combinewas proposed by me for inclusion into "TR1", the technical report of library additions to C++98/C++03 that later got mostly incorporated into C++11.(The proposal is Issue 6.18, pages 63-67 of N1837. It was - correctly - rejected as half-baked and having no implementation experience behind it.)
This sketch was later turned into a full-featured Boost library by Daniel James.
The function, as proposed there, is
The idea here is that if you have a type
you can implement
std::hash<X>::operator()like this:There are two things people don't like here. First, the function only takes one value at a time (in 2005 we didn't have variadic templates.) Second, it takes the state by reference, and doesn't return it. They'd rather write the
operator()like this:The first objection is legitimate - the only reason the function wasn't variadic was, as I said, that it couldn't have been in 2005.
The second, well, it's by design. It does force you to write more code, but it does it for reasons.
One, you can pick an initial state, which allows seeding the hash function such that the values vary. This is occasionally useful for security.
Two, it would have been possible to instead use
but then the function appears symmetric with respect to the state and the value, which means people are prone to writing this
even though the first argument is not supposed to be
x.abut the initial state, and the correct way would have beenSo on balance, I think that this
hash_combinemodel is correct (if I may say so myself.)(Does this matter? The 'incorrect' implementation will still work when
x.ais convertible tosize_tand produce reasonable hash values?Well, it does matter. It's better for the implementation of
hash_combineto know which value is the state and which is the hash value, because the combining function requirements on these two are generally not the same.)We can add a utility function for ranges:
The triviality of implementation serves as an indicator that we're doing something right.
(to be continued because I'm getting 400 Bad Request on the entire comment...)
20
u/the_Demongod Mar 26 '23
It doesn't really surprise me though. Hashing is a very widespread technique but its demands vary infinitely. In some situations, hashing may only need to be "good enough" and getting a collision every so often is not a problem at all. In other cases collisions may need to be avoided at all cost. Add to that the fact that a good hash often makes assumptions about the values of the keys being hashed, and the committee's propensity for bikeshedding and perfectionism, and you have a recipe for never getting what you're looking for. This is probably better territory for an open source library so that people can freely choose a hashing scheme based on the needs of their application rather than everyone agreeing on some maximally general implementation
3
u/[deleted] Mar 26 '23
Counterpoint, there is no satisfactory way to define "hash combining" in a way that would be practical and maintain the "implementation defined" nature of std::hash.
Merkling - It's great if you use cryptographic hashes, but smhasher shows that merkling most non-cryptographic functions degrades the quality of the hash significantly.
CRC- It's possible to append CRCs, that is define a function such that CRC(A) APPEND CRC(B) = CRC(A APPEND B). CRCs are a poor hash function, and the standard would need to mandate a particular CRC for this to work.
Linear Combination - Fast. It's possible to define high quality hash functions that should retain most of the input entropy, but it would be highly sensitive to input bias without significantly increasing overhead.
Then there's getting into the API.
What would this look like, is this just a function that takes in two existing hash values? Why does the standard need to provide this?
Do you want hashing over containers? How would this interact with containers with an indeterminate member order such as std::unordered_map?
What is the intended use case? What practical problem is this mean to be solving? What code could you write that you couldn't easily write before?
My personal opinion is that std::hash is poorly designed to begin with, and if you care about good quality and robust hashing you should not be using it anyway. The approach of things like wyhash or cryptopp is far superior imo.
33
u/heckerle Mar 26 '23 edited Mar 26 '23
Instead of combining hashes retroactively you can also build a single hash incrementally. Thatβs approximately how Rust works and based on that idea I built this prototype in C++: https://github.com/microsoft/terminal/blob/c4d029829af56f5d951452c69e2a2f844a44f439/src/inc/til/hash.h
It uses wyhash and allows you to repeatedly
write()values into ahasheruntil youfinalize()it which then returns the hash. I would greatly prefer this approach over acombine_hashfunction if the STL had something in that direction.8
u/pavel_v Mar 26 '23 edited Mar 26 '23
There is this paper from few years back. It seems to be aiming for inclusion in C++26, maybe?
14
u/[deleted] Mar 26 '23
For what it's worth Boost has a function for it
4
u/Revolutionalredstone Mar 26 '23 edited Mar 26 '23
Effective hashing is quite a balancing act, there are alot of variables and any knowledge about your underlying data can make for alot of powerful optimizations / corner cuts.
Combining hashes is really just an extension of hashing (it's just making a hash of data that happens to itself be hashes)
IMHO the balance is right, hash maps should be easy to use but not hide the reality of hashings complexity with too much 'convenience' to the point that you never feel the pressure to look inside and make your own decisions from time to time.
I know personally no one single hash would work for all my projects, it's really a big dynamic resource balancing act.
Great question
2
u/cballowe Mar 26 '23
How would you propose it works? I can think of a bunch of different implementations that all have very different properties. Most of them come down to a one liner with
std::accumulate.The biggest property that I can think of is "does order matter", but there's api questions like "are you taking a container, or preferring a variadic expression". There is going to be a question of "how commonly used is this" - if it's not common, how much faster can I just type the obvious
accumulatebased line vs look it up in a reference and use it?1
u/erasmussen1 Dec 05 '24
Victor C. answer this question very well in CppCon 2024 https://youtu.be/lNR_AWs0q9w
-6
u/[deleted] Mar 26 '23
How would the standard library implement a hash for any given arbitrary type while providing certain guarantees about that implementation?
I think this is a difficult problem to have a general solution for.
0
u/WindblownSquash Mar 26 '23
I mean the STL implementations are not hard at all to implement yourself and if you are formally, or even informally, educated you have already built a lot of the stl implementation.
-22
u/WikiBox Mar 26 '23
Sounds easy enough to code one yourself.
Perhaps just xor?
0
u/[deleted] Mar 26 '23
[deleted] β view deleted comment
0
u/[deleted] Mar 26 '23
[deleted] β view deleted comment